StringTools.hx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. /**
  23. This class provides advanced methods on Strings. It is ideally used with
  24. 'using StringTools' and then acts as an extension to the String class.
  25. If the first argument to any of the methods is null, the result is
  26. unspecified.
  27. **/
  28. #if cpp
  29. using cpp.NativeString;
  30. #end
  31. #if cs
  32. @:keep
  33. #end
  34. class StringTools {
  35. /**
  36. Encode an URL by using the standard format.
  37. **/
  38. #if (!java && !cpp) inline #end public static function urlEncode( s : String ) : String {
  39. #if flash9
  40. return untyped __global__["encodeURIComponent"](s);
  41. #elseif flash
  42. return untyped _global["escape"](s);
  43. #elseif neko
  44. return untyped new String(_urlEncode(s.__s));
  45. #elseif js
  46. return untyped encodeURIComponent(s);
  47. #elseif cpp
  48. return untyped s.__URLEncode();
  49. #elseif java
  50. try
  51. return untyped __java__("java.net.URLEncoder.encode(s, \"UTF-8\")")
  52. catch (e:Dynamic) throw e;
  53. #elseif cs
  54. return untyped cs.system.Uri.EscapeUriString(s);
  55. #elseif python
  56. return python.lib.urllib.Parse.quote(s);
  57. #else
  58. return null;
  59. #end
  60. }
  61. /**
  62. Decode an URL using the standard format.
  63. **/
  64. #if (!java && !cpp) inline #end public static function urlDecode( s : String ) : String {
  65. #if flash9
  66. return untyped __global__["decodeURIComponent"](s.split("+").join(" "));
  67. #elseif flash
  68. return untyped _global["unescape"](s);
  69. #elseif neko
  70. return untyped new String(_urlDecode(s.__s));
  71. #elseif js
  72. return untyped decodeURIComponent(s.split("+").join(" "));
  73. #elseif cpp
  74. return untyped s.__URLDecode();
  75. #elseif java
  76. try
  77. return untyped __java__("java.net.URLDecoder.decode(s, \"UTF-8\")")
  78. catch (e:Dynamic) throw e;
  79. #elseif cs
  80. return untyped cs.system.Uri.UnescapeDataString(s);
  81. #elseif python
  82. return python.lib.urllib.Parse.unquote(s);
  83. #else
  84. return null;
  85. #end
  86. }
  87. /**
  88. Escapes HTML special characters of the string `s`.
  89. The following replacements are made:
  90. - `&` becomes `&amp`;
  91. - `<` becomes `&lt`;
  92. - `>` becomes `&gt`;
  93. If `quotes` is true, the following characters are also replaced:
  94. - `"` becomes `&quot`;
  95. - `'` becomes `&#039`;
  96. **/
  97. public static function htmlEscape( s : String, ?quotes : Bool ) : String {
  98. s = s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
  99. return quotes ? s.split('"').join("&quot;").split("'").join("&#039;") : s;
  100. }
  101. /**
  102. Unescapes HTML special characters of the string `s`.
  103. This is the inverse operation to htmlEscape, i.e. the following always
  104. holds: htmlUnescape(htmlEscape(s)) == s
  105. The replacements follow:
  106. - `&amp;` becomes `&`
  107. - `&lt;` becomes `<`
  108. - `&gt;` becomes `>`
  109. - `&quot;` becomes `"`
  110. - `&#039;` becomes `'`
  111. **/
  112. public static function htmlUnescape( s : String ) : String {
  113. return s.split("&gt;").join(">").split("&lt;").join("<").split("&quot;").join('"').split("&#039;").join("'").split("&amp;").join("&");
  114. }
  115. /**
  116. Tells if the string `s` starts with the string `start`.
  117. If `start` is null, the result is unspecified.
  118. If `start` is the empty String "", the result is true.
  119. **/
  120. public static #if (cs || java) inline #end function startsWith( s : String, start : String ) : Bool {
  121. #if java
  122. return untyped s.startsWith(start);
  123. #elseif cs
  124. return untyped s.StartsWith(start);
  125. #elseif cpp
  126. if (s.length<start.length)
  127. return false;
  128. var p0 = s.c_str();
  129. var p1 = start.c_str();
  130. for(i in 0...start.length)
  131. if ( p0.at(i) != p1.at(i) )
  132. return false;
  133. return true;
  134. #else
  135. return( s.length >= start.length && s.substr(0, start.length) == start );
  136. #end
  137. }
  138. /**
  139. Tells if the string `s` ends with the string `end`.
  140. If `end` is null, the result is unspecified.
  141. If `end` is the empty String "", the result is true.
  142. **/
  143. public static #if (cs || java) inline #end function endsWith( s : String, end : String ) : Bool {
  144. #if java
  145. return untyped s.endsWith(end);
  146. #elseif cs
  147. return untyped s.EndsWith(end);
  148. #elseif cpp
  149. if (s.length<end.length)
  150. return false;
  151. var p0 = s.c_str().add( s.length-end.length );
  152. var p1 = end.c_str();
  153. for(i in 0...end.length)
  154. if ( p0.at(i) != p1.at(i) )
  155. return false;
  156. return true;
  157. #else
  158. var elen = end.length;
  159. var slen = s.length;
  160. return( slen >= elen && s.substr(slen - elen, elen) == end );
  161. #end
  162. }
  163. /**
  164. Tells if the character in the string `s` at position `pos` is a space.
  165. A character is considered to be a space character if its character code
  166. is 9,10,11,12,13 or 32.
  167. If `s` is the empty String "", or if pos is not a valid position within
  168. `s`, the result is false.
  169. **/
  170. public static function isSpace( s : String, pos : Int ) : Bool {
  171. #if python
  172. if (s.length == 0 || pos < 0 || pos >= s.length) return false;
  173. #end
  174. var c = s.charCodeAt( pos );
  175. return (c > 8 && c < 14) || c == 32;
  176. }
  177. /**
  178. Removes leading space characters of `s`.
  179. This function internally calls isSpace() to decide which characters to
  180. remove.
  181. If `s` is the empty String "" or consists only of space characters, the
  182. result is the empty String "".
  183. **/
  184. public #if cs inline #end static function ltrim( s : String ) : String {
  185. #if cs
  186. return untyped s.TrimStart();
  187. #else
  188. var l = s.length;
  189. var r = 0;
  190. while( r < l && isSpace(s,r) ){
  191. r++;
  192. }
  193. if( r > 0 )
  194. return s.substr(r, l-r);
  195. else
  196. return s;
  197. #end
  198. }
  199. /**
  200. Removes trailing space characters of `s`.
  201. This function internally calls isSpace() to decide which characters to
  202. remove.
  203. If `s` is the empty String "" or consists only of space characters, the
  204. result is the empty String "".
  205. **/
  206. public #if cs inline #end static function rtrim( s : String ) : String {
  207. #if cs
  208. return untyped s.TrimEnd();
  209. #else
  210. var l = s.length;
  211. var r = 0;
  212. while( r < l && isSpace(s,l-r-1) ){
  213. r++;
  214. }
  215. if( r > 0 ){
  216. return s.substr(0, l-r);
  217. }else{
  218. return s;
  219. }
  220. #end
  221. }
  222. /**
  223. Removes leading and trailing space characters of `s`.
  224. This is a convenience function for ltrim(rtrim(s)).
  225. **/
  226. public #if (cs || java) inline #end static function trim( s : String ) : String {
  227. #if cs
  228. return untyped s.Trim();
  229. #elseif java
  230. return untyped s.trim();
  231. #else
  232. return ltrim(rtrim(s));
  233. #end
  234. }
  235. /**
  236. Concatenates `c` to `s` until `s.length` is at least `l`.
  237. If `c` is the empty String "" or if `l` does not exceed `s.length`,
  238. `s` is returned unchanged.
  239. If `c.length` is 1, the resulting String length is exactly `l`.
  240. Otherwise the length may exceed `l`.
  241. If `c` is null, the result is unspecified.
  242. **/
  243. public static function lpad( s : String, c : String, l : Int ) : String {
  244. if (c.length <= 0)
  245. return s;
  246. while (s.length < l) {
  247. s = c + s;
  248. }
  249. return s;
  250. }
  251. /**
  252. Appends `c` to `s` until `s.length` is at least `l`.
  253. If `c` is the empty String "" or if `l` does not exceed `s.length`,
  254. `s` is returned unchanged.
  255. If `c.length` is 1, the resulting String length is exactly `l`.
  256. Otherwise the length may exceed `l`.
  257. If `c` is null, the result is unspecified.
  258. **/
  259. public static function rpad( s : String, c : String, l : Int ) : String {
  260. if (c.length <= 0)
  261. return s;
  262. while (s.length < l) {
  263. s = s + c;
  264. }
  265. return s;
  266. }
  267. /**
  268. Replace all occurences of the String `sub` in the String `s` by the
  269. String `by`.
  270. If `sub` is the empty String "", `by` is inserted after each character
  271. of `s`. If `by` is also the empty String "", `s` remains unchanged.
  272. This is a convenience function for `s.split(sub).join(by)`.
  273. If `sub` or `by` are null, the result is unspecified.
  274. **/
  275. public static function replace( s : String, sub : String, by : String ) : String {
  276. #if java
  277. if (sub.length == 0)
  278. return s.split(sub).join(by);
  279. else
  280. return untyped s.replace(sub, by);
  281. #elseif cs
  282. if (sub.length == 0)
  283. return s.split(sub).join(by);
  284. else
  285. return untyped s.Replace(sub, by);
  286. #else
  287. return s.split(sub).join(by);
  288. #end
  289. }
  290. /**
  291. Encodes `n` into a hexadecimal representation.
  292. If `digits` is specified, the resulting String is padded with "0" until
  293. its length equals `digits`.
  294. **/
  295. public static function hex( n : Int, ?digits : Int ) {
  296. #if flash9
  297. var n : UInt = n;
  298. var s : String = untyped n.toString(16);
  299. s = s.toUpperCase();
  300. #else
  301. var s = "";
  302. var hexChars = "0123456789ABCDEF";
  303. do {
  304. s = hexChars.charAt(n&15) + s;
  305. n >>>= 4;
  306. } while( n > 0 );
  307. #end
  308. #if python
  309. if (digits != null && s.length < digits) {
  310. var diff = digits - s.length;
  311. for (_ in 0...diff) {
  312. s = "0" + s;
  313. }
  314. }
  315. #else
  316. if( digits != null )
  317. while( s.length < digits )
  318. s = "0"+s;
  319. #end
  320. return s;
  321. }
  322. /**
  323. Returns the character code at position `index` of String `s`, or an
  324. end-of-file indicator at if `position` equals `s.length`.
  325. This method is faster than String.charCodeAt() on some platforms, but
  326. the result is unspecified if `index` is negative or greater than
  327. `s.length`.
  328. End of file status can be checked by calling `StringTools.isEof` with
  329. the returned value as argument.
  330. This operation is not guaranteed to work if `s` contains the \0
  331. character.
  332. **/
  333. public static inline function fastCodeAt( s : String, index : Int ) : Int {
  334. #if neko
  335. return untyped __dollar__sget(s.__s, index);
  336. #elseif cpp
  337. return untyped s.cca(index);
  338. #elseif flash9
  339. return untyped s.cca(index);
  340. #elseif flash
  341. return untyped s["cca"](index);
  342. #elseif java
  343. return ( index < s.length ) ? cast(_charAt(s, index), Int) : -1;
  344. #elseif cs
  345. return ( cast(index, UInt) < s.length ) ? cast(s[index], Int) : -1;
  346. #elseif js
  347. return (untyped s).charCodeAt(index);
  348. #elseif python
  349. return if (index >= s.length) -1 else python.internal.UBuiltins.ord(python.Syntax.arrayAccess(s, index));
  350. #else
  351. return untyped s.cca(index);
  352. #end
  353. }
  354. /*
  355. Tells if `c` represents the end-of-file (EOF) character.
  356. */
  357. @:noUsing public static inline function isEof( c : Int ) : Bool {
  358. #if (flash9 || cpp)
  359. return c == 0;
  360. #elseif flash8
  361. return c <= 0; // fast NaN
  362. #elseif js
  363. return c != c; // fast NaN
  364. #elseif neko
  365. return c == null;
  366. #elseif cs
  367. return c == -1;
  368. #elseif java
  369. return c == -1;
  370. #elseif python
  371. return c == -1;
  372. #else
  373. return false;
  374. #end
  375. }
  376. #if java
  377. private static inline function _charAt(str:String, idx:Int):java.StdTypes.Char16 return untyped str._charAt(idx);
  378. #end
  379. #if neko
  380. private static var _urlEncode = neko.Lib.load("std","url_encode",1);
  381. private static var _urlDecode = neko.Lib.load("std","url_decode",1);
  382. #end
  383. }