EscapeXML

From Second Life Wiki
Revision as of 00:14, 9 March 2008 by Strife Onizuka (talk | contribs) (→‎EscapeXML: the LSL compiler sucks, the function was slow, we parse it backwords so we don't have to fudge the indexes, this isn't the fastest solution.)
Jump to navigation Jump to search
EscapeXML

<lsl> // Escape standard reserved XML chars with entities // Copyright 2008, JB Kraft & Strife Onizuka // Released under BSD license // http://www.opensource.org/licenses/bsd-license.php


// ------------------------------------------------------------------------ // escape standard reserved XML chars with entities // ------------------------------------------------------------------------ string escapeXML( string str ) {

   integer i = llStringLength( str );
   if(i)
   {
       string unescapedChars = "&\"'<>"; 
       list escapedChars = ["&", """, "'", "<", ">"];
       integer ndx;
       do
           if( ~(ndx = llSubStringIndex( unescapedChars, llGetSubString(str, --i, i))) )
               str = llInsertString( llDeleteSubString( str, i, i), i, llList2String( escapedChars, ndx));
       while(i);
   }
   return str;

}

// ------------------------------------------------------------------------ // D E F A U L T // ------------------------------------------------------------------------ default {

   state_entry()
   {
       llSay(0, escapeXML("<Black> & \"White\" & 'Red' & <Green>"));
   }

}


</lsl>