HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Criticism of the function

07-19-2009, 01:17 AM#1
ToukoAozaki
Collapse JASS:
function IsStringNumeric takes string s returns boolean    
    if s == "" or s == null then
        return false
    elseif S2I(s) != 0 then
        return true
    endif
    
    return S2I(s + "1") != 0
endfunction

I just came up with this simple function, which detects whether the string is S2I compatible.

Pros:
  • Properly does its job without checking individual characters in Jass, which is quite a performance loss.

Cons:
  • Does not support hexadecimal numbers. If so, implementation will be inefficient.
  • Might be too short to be a single submission.

Any other suggestions or ideas?
07-19-2009, 01:44 AM#2
Anitarf
Wouldn't this do the same?
Collapse JASS:
function IsStringNumeric takes string s returns boolean
    return S2I(s)!=0 or s=="0"
endfunction
07-19-2009, 02:14 AM#3
Sven2
Quote:
Originally Posted by Anitarf
Wouldn't this do the same?
Collapse JASS:
function IsStringNumeric takes string s returns boolean
    return S2I(s)!=0 or s=="0"
endfunction

What if s is "00", "+0", "-0", etc.?

Thinking of it: Isn't there a maximum string length? At least for S2I? If so, the original poster's function would fail if the string consisted of maximum string length number of zeros ;)
07-19-2009, 02:17 AM#4
Sven2
Quote:
Originally Posted by ToukoAozaki
Collapse JASS:
function IsStringNumeric takes string s returns boolean    
    if s == "" or s == null then
        return false
    elseif S2I(s) != 0 then
        return true
    endif
    
    return S2I(s + "1") != 0
endfunction

I just came up with this simple function, which detects whether the string is S2I compatible.

Pros:
  • Properly does its job without checking individual characters in Jass, which is quite a performance loss.

Cons:
  • Does not support hexadecimal numbers. If so, implementation will be inefficient.
  • Might be too short to be a single submission.

Any other suggestions or ideas?

I just realized: The function would return true if the string were "+" or "-". Is this intended?
07-19-2009, 06:39 AM#5
ToukoAozaki
Quote:
Originally Posted by Sven2
I just realized: The function would return true if the string were "+" or "-". Is this intended?

Oh, I didn't realize that. Thanks for letting me know.

Added: blank spaces seem to mess up if I add checks for + or -... lol... also S2I(" 10 asdf 19") is 10; means " 00 " will return false :(
07-19-2009, 07:36 AM#6
DioD
function IsStringNumeric takes string s returns boolean
return I2S(S2I(s)) == s
endfunction