Jump to content

Trying to choose a "special character" to use as a flag. How can you compare to a special character?


Love Zhaoying
 Share

You are about to reply to a thread that has been inactive for 3220 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

I need to choose a "relatively unused" special character that I can use as a "flag" in messages sent between my scripts.

I've been studying the use of UTF-8 via llUnescapeURL(), etc.

From this, I had the idea that I could perhaps use a UTF-8 character..

However, how would I compare for it?

Idea: Can you compare two strings that have the same special UTF-8 value?

For instance, I could use the Chr() function from the library (http://wiki.secondlife.com/wiki/Chr) to encode 0x2665 as the "heart" character and save that in a string variable:

   string sHeart=Chr(0x2665);

a)  Is it then valid to compare this using normal string methods? For example:

  if (llSubStringIndex(otherstring, sHeart)==0) // Found heart at the start!

b) Can you suggest another way to do this without having to use the Chr() functions to "build" the character into a string in the first place?

Thanks!

 

Link to comment
Share on other sites

That certainly works.  The best way to tell, of course, is to try it yourself...

// Chr() function, written by Pedro Oval, 2010-05-28 // Auxiliary function UrlCode, which returns the hex code of the given integer// (which must be in range 0-255) with a "%" prepended.string UrlCode(integer b){    string hexd = "0123456789ABCDEF";    return "%" + llGetSubString(hexd, b>>4, b>>4)               + llGetSubString(hexd, b&15, b&15);} // Chr function itself, which implements Unicode to UTF-8 conversion and uses// llUnescapeURL to do its job.string Chr(integer ord){    if (ord <= 0)        return "";    if (ord < 0x80)        return llUnescapeURL(UrlCode(ord));    if (ord < 0x800)        return llUnescapeURL(UrlCode((ord >> 6) | 0xC0)                            +UrlCode(ord & 0x3F | 0x80));    if (ord < 0x10000)        return llUnescapeURL(UrlCode((ord >> 12) | 0xE0)                            +UrlCode((ord >> 6) & 0x3F | 0x80)                            +UrlCode(ord & 0x3F | 0x80));    return llUnescapeURL(UrlCode((ord >> 18) | 0xF0)                        +UrlCode((ord >> 12) & 0x3F | 0x80)                        +UrlCode((ord >> 6) & 0x3F | 0x80)                        +UrlCode(ord & 0x3F | 0x80));}string heart;string test;default{    state_entry()    {        heart = Chr(0x2665);        test = "This string contains a "+heart+ " in it.";    }        touch_start(integer num)    {        integer idx = llSubStringIndex(test,heart);        if (~idx)        {            llSay(0,heart + " is at position " + (string)idx);        }    }}

which returns

Test Object: ♥ is at position 23

Other than the novelty of using a character like , however, I can't see a particular advantage of going to all this trouble.  Personally, I'd just stick with the character set on the keyboard if all I needed was a flag.  But that's just me.

Link to comment
Share on other sites

Thanks, I have ideas at my office when I can't try them. 

The reason I want a "special" character is, to be a real and true "flag" I have to choose something that can't be part of normal data.  This is because I am coding something that needs to be able to handle any value for the data (for strings at least). The "heart" was just an example I chose from the link.  If it were me, I would choose an odd ASCII character but I haven't figured out if those are actually available, or not available for use in LSL. 

The fact that only a few "special characters" are defined under "escape codes" (tab, newline, double quote, backslash) and I could not find any examples of using "control characters" (low/high ASCII) let me to think that the UTF-8 is the best chance at a "flag" that I will get: the point being to select a character that will almost definitely never be part of the data; I can select some strange symbol that is hopefully ugly enough noone will choose it on purpose. But, I'll make it "changeable" in my code just in case.

Thanks, as always!

 

 

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3220 days.

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
 Share

×
×
  • Create New...