Cerise Sorbet
Registered User
Join date: 8 Jun 2008
Posts: 254
|
11-27-2009 05:31
Some of the LSL functions count bytes and not characters. ASCII is easy but wide characters take more work to measure. I started with this. It is short but lists like to munch on memory. integer GetStringBytes(string s) { list chopped = llParseString2List(llEscapeURL("*" + s), ["%"], []); return llStringLength((string)chopped) - llGetListLength(chopped) - 1; }
Then I tried this. It is slow on long strings. integer GetStringBytes2(string s) { s = llEscapeURL(s); integer i = 0; integer j; for (j = llStringLength(s); j > -1; j--) { i += (llGetSubString(s, j, j) == "%"); } return llStringLength(s) - i - i; }
Now I have this but I think it is ugly. integer GetStringBytes3(string s) { s = llEscapeURL(s); integer L = llStringLength(s); integer i = 0; integer p = 0; while (p < L) { integer q = llSubStringIndex(llGetSubString(s, p, -1), "%"); if (q == -1) { p = L; } else { p += q + 1; i++; } } return L - i - i; }
Are there better ideas out there?
|
Kaluura Boa
Polygon Project
Join date: 27 Mar 2007
Posts: 194
|
11-27-2009 07:11
Hmmm... Deep thinking... integer uuGetByteLength(string zz) { if (zz == ""  { return 0; } // Quick exit zz = llEscapeURL(zz); x = (llParseString2List(zz, ["%"], []) != []); // Faster llGetListLength y = llStringLength(zz) - (3 * x); y += 2 * (y <= 0); return x + y; } Some (really needed) explanations: Let's take an escaped string 'esc' composed of 'x' escaped characters and 'y' regular ones. Its length in bytes is x+y but its ASCII length is: esc_len = (3 * x) + y We know its length with llStringLength() and: x = length of list ( llParseString2List(esc, ["%"], []) ) We can write and calculate: y = esc_len - (3 * x) Depending on the number and positions of the escaped characters, 'y' can be lower than zero and lead to a wrong result. We use a roll of duct tape to fix that: y += 2 * (y <= 0); // The same as: if (y <= 0) { y += 2; } And finally: return x + y; That's it. It tested some corner cases and it looks like to be working.
|
Haravikk Mistral
Registered User
Join date: 8 Oct 2005
Posts: 2,482
|
11-28-2009 03:36
This is some good work, and much needed as the output of llStringLength() is just annoyingly confusing when dealing with other languages. It is true however that few unicode implementations properly count characters, for example most UTF-16 implementations count how many 16-bit sequences there are, but not all of these will be individual characters, which just makes things extra confusing!
_____________________
Computer (Mac Pro): 2 x Quad Core 3.2ghz Xeon 10gb DDR2 800mhz FB-DIMMS 4 x 750gb, 32mb cache hard-drives (RAID-0/striped) NVidia GeForce 8800GT (512mb)
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
11-28-2009 10:05
use a separate script, and get free memory along with link message? have it spit back the difference between the pre calculated memory and the incoming message? can't test but it seems feasible. (well for shorter messages I'm not sure what the string limit is on link messages)
_____________________
| | . "Cat-Like Typing Detected" | . This post may contain errors in logic, spelling, and | . grammar known to the SL populace to cause confusion | | - Please Use PHP tags when posting scripts/code, Thanks. | - Can't See PHP or URL Tags Correctly? Check Out This Link... | - 
|
Lee Ponzu
What Would Steve Do?
Join date: 28 Jun 2006
Posts: 1,770
|
Badger LL...
11-28-2009 10:17
... to add llSizeOf()
_____________________
So many monkeys, so little Shakespeare.
|