Difference between revisions of "EscapeXML"
Jump to navigation
Jump to search
(New page: =====EscapeXML===== <lsl> // Escape standard reserved XML chars with entities // Copyright 2008, JB Kraft // Released under BSD license // http://www.opensource.org/licenses/bsd-license.p...) |
m (<lsl> tag to <source>) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
=====EscapeXML===== | =====EscapeXML===== | ||
< | <source lang="lsl2">// Escape standard reserved XML chars with entities | ||
// Escape standard reserved XML chars with entities | // Copyright 2008, JB Kraft & Strife Onizuka | ||
// Copyright 2008, JB Kraft | |||
// 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 12: | ||
string escapeXML( string str ) | 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 = | str = llInsertString( llDeleteSubString( str, i, i), i, llList2String( escapedChars, ndx)); | ||
while(i); | |||
} | } | ||
return str; | return str; | ||
Line 48: | Line 36: | ||
llSay(0, escapeXML("<Black> & \"White\" & 'Red' & <Green>")); | llSay(0, escapeXML("<Black> & \"White\" & 'Red' & <Green>")); | ||
} | } | ||
} | }</source> | ||
[[Category:LSL Examples]] | [[Category:LSL Examples]] |
Latest revision as of 13:50, 24 January 2015
EscapeXML
// 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>"));
}
}