I compared your original version, the Ates Horals hash, the improved hash, the switch version and the simple solution. Winner? A simple solution!
( 85 )
original simple hash switch ag-hash
FF3 194 188 250 240 424
IE7 188 172 641 906 2203
Chrome1 161 156 165 165 225
Opera9 640 625 531 515 734
( 85 ):
original simple hash switch ag-hash
FF3 39 4 34 34 39
IE7 125 15 125 125 156
Chrome1 45 2 54 54 57
Opera9 156 15 156 156 156
( xp, 1,7 , ymmv)
:
function XMLToString(str) {
return (str.indexOf("\\")<0 && str.indexOf("&")<0) ? str :
str
.replace(/\\34/g,'"')
.replace(/\\39/g,"'")
.replace(/\\62/g,">")
.replace(/\\60/g,"<")
.replace(/\\13\\10/g,"\n")
.replace(/\\09/g,"\t")
.replace(/\\92/g,"\\")
.replace(/\&/g,"&");
}
:
, ( indexOf ). , , . . , , .
, , . , regexp . .
Ates Horals, ( ), , , .
UPDATE 1 Bugfix: .
2
, , , , .
"\ dd", dd - , "% xx", xx - . decodeURIComponent, , , unicode.
matching non match
FF3 44 3
IE7 93 16
Chrome1 132 1
Opera9 109 16
.
function XMLToString_S1(str) {
return (str.indexOf("%")<0) ? str : decodeURIComponent(str).replace(/\x0D\x0A/g,"\n")
}
, "\ 09test\60\34string\34\62\13\10 \" "% 09test% 3c% 22string% 22% 3e% 0d% 0a".