Nuclear Slingshot Posted April 18, 2013 Posted April 18, 2013 I can't seem to find this in the LSL Wiki. What is the result of attempting to typecast a non-numeric string to an integer? My preliminary tests have show that no exception is thrown and the result of the cast is 0 (zero). Is this the expected behavior (i.e. can it be counted on)?
Miranda Umino Posted April 18, 2013 Posted April 18, 2013 Yes , it s the expected behaviour It looks be the same comportement than the C function atoi : i quote http://www.cplusplus.com/reference/cstdlib/atoi/ 1) The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many base-10 digits as possible, and interprets them as a numerical value.2) The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.3) If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned. With a test in world , we get integer x = (integer)" 123" => x value is 123 as in C with atoi function (1st point of the quote ) integer y = (integer)"123hello" => y value is 123 as in C with atoi function (2nd point of the quote ) integer z = (integer)"hello" => z value is 0 as in C with atoi function (3rd point of the quote ) Note : strangely it looks closer to the C atoi function than the Int32.TryParse C# function . // C see http://ideone.com/gErnA1 #include <stdio.h> /* printf, fgets */#include <stdlib.h> /* atoi */ int main (){ int x = atoi (" 123"); int y = atoi ("123hello"); int z = atoi ("hello"); printf ("%d %d %d.\n",x,y,z); return 0;} Output : 123 123 0 //C# see http://ideone.com/pGfUbU using System; class Program{ static void Main() { int x; int y ; int z; Int32.TryParse(" 123", out x); Int32.TryParse("123hello", out y); Int32.TryParse("hello", out z); Console.WriteLine("{0} {1} {2}", x, y, z); // Converted to -3 }} Output : 123 0 0 Probably for historic reasons because the lsl VM was done in C .
Nuclear Slingshot Posted April 18, 2013 Author Posted April 18, 2013 Thanks, Miranda. That answers my question. Although LSL is quite similar to other C-like languages, it has a few idiosyncrasies which make me hesitant to rely on expectant behavior of those other languages.
Miranda Umino Posted April 18, 2013 Posted April 18, 2013 Nevertheless an exception system in LSL will be great ! ( as too a collection system, a customized event system, a "library" system, a reference system, nested types ... ) But i doubt we ll get one soon . I even doubt we ll get one , in the future . ( because of the compatibilty with old outdated user contents) It s hopeless and very depressing
Rolig Loon Posted April 18, 2013 Posted April 18, 2013 That's why this test can always distinguish between an integer and a NAN......... if ( (string) ( (integer) data) == data) llOwnerSay("'" + data + "' contains a valid integer");
Miranda Umino Posted April 18, 2013 Posted April 18, 2013 It will give wrong negatives ( negative tests not negative numbers) : string data ="+3"; // worths 3 who is valid and the cast returns 3if ( (string) ( (integer) data) == data)llOwnerSay("'" + data + "' contains a valid integer");elsellOwnerSay("'" + data + "' doesn t contain a valid integer"); Output : '+3' doesn t contain a valid integer You will need to add an additional test : if ( ( (string) ( (integer) data) == data) || ( "+" + (string) ( (integer) data) == data) ) And probably other tests if your input can be in hexadecimal form : string data ="0x0045AB12"; // worths 4565778 who is valid and the cast returns 4565778 Output : '0x0045AB12' doesn t contain a valid integer Third problem : you will need to trim left-zeros string data ="03"; // worths 3 who is valid and the cast returns 3 Output : '03' doesn t contain a valid integer
Rolig Loon Posted April 18, 2013 Posted April 18, 2013 Thanks, Miranda. I suggest that you add your modifications to Strife's example in the LSL wiki at https://wiki.secondlife.com/wiki/Category:LSL_Integer .
Recommended Posts
Please take a moment to consider if this thread is worth bumping.
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now