GetLiteralString

jaynabonne
27 Oct 2012, 20:41
Not sure if this is interesting to anyone or not, but I was experimenting with having the player enter his/her name, and naturally it turns out that if they enter special characters like "<", ">", "&", etc, then bad things happen (or good things - I was able to make my name blue!)

Instead of filtering out odd characters, I just decided to encode them all, so that the string prints literally as they entered it. And rather than only encoding certain characters, I just decided to encode them all. This is the resulting straightforward function:

  <function name="GetLiteralString" parameters="s" type="string">
<![CDATA[
outs = ""
for (i, 1, LengthOf(s)) {
outs = outs + "&#" + Asc(Mid(s, i, 1)) + ";"
}
return (outs)
]]>
</function>


If you print out the result with msg, you'll see the same as what was input, but none of the special characters will kick in, since they're all encoded. It does mean that if you look at itin the debugger, you'll just see a bunch of encoded characters. :)