Difference between revisions of "EscapeXML"

From Second Life Wiki
Jump to navigation Jump to search
(→‎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.)
Line 3: Line 3:
<lsl>
<lsl>
// Escape standard reserved XML chars with entities
// Escape standard reserved XML chars with entities
// Copyright 2008, JB Kraft
// Copyright 2008, JB Kraft & Strife Onizuka
// Released under BSD license
// Released under BSD license
// http://www.opensource.org/licenses/bsd-license.php
// http://www.opensource.org/licenses/bsd-license.php
Line 13: Line 13:
string escapeXML( string str )
string escapeXML( string str )
{
{
     list escapeChars = ["&","&amp;","\"","&quot;","'","&#039;","<","&lt;",">","&gt;"];
     integer i = llStringLength( str );
    integer i;
    if(i)
    integer ndx;
    {
        string unescapedChars = "&\"'<>";
        list escapedChars = ["&amp;", "&quot;", "&#39;", "&lt;", "&gt;"];
        integer ndx;


    for( i = 0; i < llStringLength( str ); i++ ) {
        do
        ndx = llListFindList( escapeChars, [llGetSubString(str, i, i)]);
            if( ~(ndx = llSubStringIndex( unescapedChars, llGetSubString(str, --i, i))) )
        if( ndx != -1 ) {
                 str = llInsertString( llDeleteSubString( str, i, i), i, llList2String( escapedChars, ndx));
            // start
        while(i);
            if( i == 0 ) {
                 str = llList2String( escapeChars, ndx+1) + llGetSubString( str, i+1, -1 );
            }
            // end
            else if( i == llStringLength( str ) - 1) {
                str = llGetSubString( str, 0, -2) + llList2String( escapeChars, ndx+1);
            }
            //  middle
            else {
                str = llGetSubString( str, 0, i-1) + llList2String( escapeChars, ndx+1)
                    + llGetSubString( str, i+1, -1 );
            }
            i += llStringLength( llList2String( escapeChars, ndx+1));
        }
     }
     }
     return str;
     return str;

Revision as of 00:14, 9 March 2008

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>