comparison dwt/dwthelper/XmlTranscode.d @ 212:ab60f3309436

reverted the char[] to String and use the an alias.
author Frank Benoit <benoit@tionex.de>
date Mon, 05 May 2008 00:12:38 +0200
parents be4ce760802a
children
comparison
equal deleted inserted replaced
211:ff59aeb96cac 212:ab60f3309436
7 + Decode XML entities into UTF8 string. 7 + Decode XML entities into UTF8 string.
8 + Eg. "&amp;" -> "&", "&#38;" -> "&", "&#x26;" -> "&" 8 + Eg. "&amp;" -> "&", "&#38;" -> "&", "&#x26;" -> "&"
9 + Throws TextException on failure 9 + Throws TextException on failure
10 + The given string is modified. 10 + The given string is modified.
11 +/ 11 +/
12 char[] xmlUnescape( char[] str ){ 12 String xmlUnescape( String str ){
13 13
14 void error(){ 14 void error(){
15 throw new TextException( "xmlUnescape" ); 15 throw new TextException( "xmlUnescape" );
16 } 16 }
17 // &lt; ... 17 // &lt; ...
18 // &#1234; 18 // &#1234;
19 // &#x12AF; 19 // &#x12AF;
20 char[] src = str; 20 String src = str;
21 char[] trg = str; 21 String trg = str;
22 while( src.length ){ 22 while( src.length ){
23 if( src[0] !is '&' ){ 23 if( src[0] !is '&' ){
24 trg[0] = src[0]; 24 trg[0] = src[0];
25 trg = trg[1..$]; 25 trg = trg[1..$];
26 src = src[1..$]; 26 src = src[1..$];
32 // search semi 32 // search semi
33 int len = Math.min( src.length, 10 ); // limit semi search to possible longest entityname 33 int len = Math.min( src.length, 10 ); // limit semi search to possible longest entityname
34 int semi = tango.text.Util.locate( src[0 .. len ], ';' ); 34 int semi = tango.text.Util.locate( src[0 .. len ], ';' );
35 if( semi is len ) error(); // no semi found 35 if( semi is len ) error(); // no semi found
36 36
37 char[] entityName = src[ 0 .. semi ]; // name without semi 37 String entityName = src[ 0 .. semi ]; // name without semi
38 dchar entityValue = 0; 38 dchar entityValue = 0;
39 switch( entityName ){ 39 switch( entityName ){
40 case "lt": entityValue = '<'; break; 40 case "lt": entityValue = '<'; break;
41 case "gt": entityValue = '>'; break; 41 case "gt": entityValue = '>'; break;
42 case "amp": entityValue = '&'; break; 42 case "amp": entityValue = '&'; break;
77 } 77 }
78 } 78 }
79 dchar[1] arr; 79 dchar[1] arr;
80 arr[0] = entityValue; 80 arr[0] = entityValue;
81 uint ate = 0; 81 uint ate = 0;
82 char[] res = tango.text.convert.Utf.toString( arr, trg, &ate ); 82 String res = tango.text.convert.Utf.toString( arr, trg, &ate );
83 trg = trg[ res.length .. $ ]; 83 trg = trg[ res.length .. $ ];
84 src = src[ semi +1 .. $ ]; // go past semi 84 src = src[ semi +1 .. $ ]; // go past semi
85 } 85 }
86 } 86 }
87 return str[ 0 .. trg.ptr-str.ptr ]; 87 return str[ 0 .. trg.ptr-str.ptr ];
92 + Encode XML entities into UTF8 string. 92 + Encode XML entities into UTF8 string.
93 + First checks if processing is needed. 93 + First checks if processing is needed.
94 + If not, the original string is returned. 94 + If not, the original string is returned.
95 + If processing is needed, a new string is allocated. 95 + If processing is needed, a new string is allocated.
96 +/ 96 +/
97 char[] xmlEscape( char[] xml ){ 97 String xmlEscape( String xml ){
98 bool needsReplacement( dchar c ){ 98 bool needsReplacement( dchar c ){
99 switch( c ){ 99 switch( c ){
100 case '<': 100 case '<':
101 case '>': 101 case '>':
102 case '&': 102 case '&':
119 } 119 }
120 return xml; 120 return xml;
121 Lprocess: 121 Lprocess:
122 122
123 // yes, do a new string, start with +20 chars 123 // yes, do a new string, start with +20 chars
124 char[] res = new char[ xml.length + 20 ]; 124 String res = new char[ xml.length + 20 ];
125 res.length = 0; 125 res.length = 0;
126 126
127 foreach( dchar c; xml ){ 127 foreach( dchar c; xml ){
128 128
129 if( !needsReplacement( c )){ 129 if( !needsReplacement( c )){