StringTools.hx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 cs
  29. @:keep
  30. #end
  31. class StringTools {
  32. /**
  33. Returns a string with backslashes before characters that need to be quoted.
  34. **/
  35. public static function addSlashes( s : String ) : String {
  36. return s.split("\n").join("\\n").split("\t").join("\\t").split("'").join("\\'").split('"').join("\\\"") #if sys .split("\x00").join("\\x00") #end;
  37. }
  38. /**
  39. Encode an URL by using the standard format.
  40. **/
  41. public static function urlEncode( s : String ) : String untyped {
  42. #if flash9
  43. return __global__["encodeURIComponent"](s);
  44. #elseif flash
  45. return _global["escape"](s);
  46. #elseif neko
  47. return new String(_urlEncode(s.__s));
  48. #elseif js
  49. return encodeURIComponent(s);
  50. #elseif cpp
  51. return s.__URLEncode();
  52. #elseif java
  53. try
  54. return untyped __java__("java.net.URLEncoder.encode(s, \"UTF-8\")")
  55. catch (e:Dynamic) throw e;
  56. #elseif cs
  57. return untyped __cs__("System.Uri.EscapeUriString(s)");
  58. #else
  59. return null;
  60. #end
  61. }
  62. /**
  63. Decode an URL using the standard format.
  64. **/
  65. public static function urlDecode( s : String ) : String untyped {
  66. #if flash9
  67. return __global__["decodeURIComponent"](s.split("+").join(" "));
  68. #elseif flash
  69. return _global["unescape"](s);
  70. #elseif neko
  71. return new String(_urlDecode(s.__s));
  72. #elseif js
  73. return decodeURIComponent(s.split("+").join(" "));
  74. #elseif cpp
  75. return s.__URLDecode();
  76. #elseif java
  77. try
  78. return untyped __java__("java.net.URLDecoder.decode(s, \"UTF-8\")")
  79. catch (e:Dynamic) throw e;
  80. #elseif cs
  81. return untyped __cs__("System.Uri.UnescapeDataString(s)");
  82. #else
  83. return null;
  84. #end
  85. }
  86. /**
  87. Escapes HTML special characters of the string `s`.
  88. The following replacements are made:
  89. - `&` becomes `&amp`;
  90. - `<` becomes `&lt`;
  91. - `>` becomes `&gt`;
  92. If `quotes` is true, the following characters are also replaced:
  93. - `"` becomes `&quot`;
  94. - `'` becomes `&#039`;
  95. **/
  96. public static function htmlEscape( s : String, ?quotes : Bool ) : String {
  97. s = s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
  98. return quotes ? s.split('"').join("&quot;").split("'").join("&#039;") : s;
  99. }
  100. /**
  101. Unescapes HTML special characters of the string `s`.
  102. This is the inverse operation to htmlEscape, i.e. the following always
  103. holds: htmlUnescape(htmlEscape(s)) == s
  104. The replacements follow:
  105. - `&amp;` becomes `&`
  106. - `&lt;` becomes `<`
  107. - `&gt;` becomes `>`
  108. - `&quot;` becomes `"`
  109. - `&#039;` becomes `'`
  110. **/
  111. public static function htmlUnescape( s : String ) : String {
  112. return s.split("&gt;").join(">").split("&lt;").join("<").split("&quot;").join('"').split("&#039;").join("'").split("&amp;").join("&");
  113. }
  114. /**
  115. Tells if the string `s` starts with the string `start`.
  116. If `start` is null, the result is unspecified.
  117. If `start` is the empty String "", the result is true.
  118. **/
  119. public static #if (cs || java) inline #end function startsWith( s : String, start : String ) : Bool {
  120. #if java
  121. return untyped s.startsWith(start);
  122. #elseif cs
  123. return untyped s.StartsWith(start);
  124. #else
  125. return( s.length >= start.length && s.substr(0, start.length) == start );
  126. #end
  127. }
  128. /**
  129. Tells if the string `s` ends with the string `end`.
  130. If `end` is null, the result is unspecified.
  131. If `end` is the empty String "", the result is true.
  132. **/
  133. public static #if (cs || java) inline #end function endsWith( s : String, end : String ) : Bool {
  134. #if java
  135. return untyped s.endsWith(end);
  136. #elseif cs
  137. return untyped s.EndsWith(end);
  138. #else
  139. var elen = end.length;
  140. var slen = s.length;
  141. return( slen >= elen && s.substr(slen - elen, elen) == end );
  142. #end
  143. }
  144. /**
  145. Tells if the character in the string `s` at position `pos` is a space.
  146. A character is considered to be a space character if its character code
  147. is 9,10,11,12,13 or 32.
  148. If `s` is the empty String "", or if pos is not a valid position within
  149. `s`, the result is false.
  150. **/
  151. public static function isSpace( s : String, pos : Int ) : Bool {
  152. var c = s.charCodeAt( pos );
  153. return (c > 8 && c < 14) || c == 32;
  154. }
  155. /**
  156. Removes leading space characters of `s`.
  157. This function internally calls isSpace() to decide which characters to
  158. remove.
  159. If `s` is the empty String "" or consists only of space characters, the
  160. result is the empty String "".
  161. **/
  162. public #if cs inline #end static function ltrim( s : String ) : String {
  163. #if cs
  164. return untyped s.TrimStart();
  165. #else
  166. var l = s.length;
  167. var r = 0;
  168. while( r < l && isSpace(s,r) ){
  169. r++;
  170. }
  171. if( r > 0 )
  172. return s.substr(r, l-r);
  173. else
  174. return s;
  175. #end
  176. }
  177. /**
  178. Removes trailing 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 rtrim( s : String ) : String {
  185. #if cs
  186. return untyped s.TrimEnd();
  187. #else
  188. var l = s.length;
  189. var r = 0;
  190. while( r < l && isSpace(s,l-r-1) ){
  191. r++;
  192. }
  193. if( r > 0 ){
  194. return s.substr(0, l-r);
  195. }else{
  196. return s;
  197. }
  198. #end
  199. }
  200. /**
  201. Removes leading and trailing space characters of `s`.
  202. This is a convenience function for ltrim(rtrim(s)).
  203. **/
  204. public #if (cs || java) inline #end static function trim( s : String ) : String {
  205. #if cs
  206. return untyped s.Trim();
  207. #elseif java
  208. return untyped s.trim();
  209. #else
  210. return ltrim(rtrim(s));
  211. #end
  212. }
  213. /**
  214. Concatenates `c` to `s` until `s.length` is at least `l`.
  215. If `c` is the empty String "" or if `l` does not exceed `s.length`,
  216. `s` is returned unchanged.
  217. If `c.length` is 1, the resulting String length is exactly `l`.
  218. Otherwise the length may exceed `l`.
  219. If `c` is null, the result is unspecified.
  220. **/
  221. public static function lpad( s : String, c : String, l : Int ) : String {
  222. if (c.length <= 0)
  223. return s;
  224. while (s.length < l) {
  225. s = c + s;
  226. }
  227. return s;
  228. }
  229. /**
  230. Appends `c` to `s` until `s.length` is at least `l`.
  231. If `c` is the empty String "" or if `l` does not exceed `s.length`,
  232. `s` is returned unchanged.
  233. If `c.length` is 1, the resulting String length is exactly `l`.
  234. Otherwise the length may exceed `l`.
  235. If `c` is null, the result is unspecified.
  236. **/
  237. public static function rpad( s : String, c : String, l : Int ) : String {
  238. if (c.length <= 0)
  239. return s;
  240. while (s.length < l) {
  241. s = s + c;
  242. }
  243. return s;
  244. }
  245. /**
  246. Replace all occurences of the String `sub` in the String `s` by the
  247. String `by`.
  248. If `sub` is the empty String "", `by` is inserted after each character
  249. of `s`. If `by` is also the empty String "", `s` remains unchanged.
  250. This is a convenience function for `s.split(sub).join(by)`.
  251. If `sub` or `by` are null, the result is unspecified.
  252. **/
  253. public static function replace( s : String, sub : String, by : String ) : String {
  254. #if java
  255. if (sub.length == 0)
  256. return s.split(sub).join(by);
  257. else
  258. return untyped s.replace(sub, by);
  259. #elseif cs
  260. if (sub.length == 0)
  261. return s.split(sub).join(by);
  262. else
  263. return untyped s.Replace(sub, by);
  264. #else
  265. return s.split(sub).join(by);
  266. #end
  267. }
  268. /**
  269. Encodes `n` into a hexadecimal representation.
  270. If `digits` is specified, the resulting String is padded with "0" until
  271. its length equals `digits`.
  272. **/
  273. public static function hex( n : Int, ?digits : Int ) {
  274. #if flash9
  275. var n : UInt = n;
  276. var s : String = untyped n.toString(16);
  277. s = s.toUpperCase();
  278. #else
  279. var s = "";
  280. var hexChars = "0123456789ABCDEF";
  281. do {
  282. s = hexChars.charAt(n&15) + s;
  283. n >>>= 4;
  284. } while( n > 0 );
  285. #end
  286. if( digits != null )
  287. while( s.length < digits )
  288. s = "0"+s;
  289. return s;
  290. }
  291. /**
  292. Returns the character code at position `index` of String `s`.
  293. This method is faster than String.charCodeAt() on most platforms.
  294. However, unlike String.charCodeAt(), the result is unspecified if
  295. `index` is negative or exceeds `s.length`.
  296. This operation is not guaranteed to work if `s` contains the \0
  297. character.
  298. **/
  299. public static inline function fastCodeAt( s : String, index : Int ) : Int untyped {
  300. #if neko
  301. return untyped __dollar__sget(s.__s, index);
  302. #elseif cpp
  303. return s.cca(index);
  304. #elseif flash9
  305. return s.cca(index);
  306. #elseif flash
  307. return s["cca"](index);
  308. #elseif java
  309. return ( index < s.length ) ? cast(_charAt(s, index), Int) : -1;
  310. #elseif cs
  311. return ( cast(index, UInt) < s.length ) ? cast(untyped s[index], Int) : -1;
  312. #elseif js
  313. #if mt
  314. return (untyped s).cca(index);
  315. #else
  316. return (untyped s).charCodeAt(index);
  317. #end
  318. #else
  319. return s.cca(index);
  320. #end
  321. }
  322. /*
  323. Tells if `c` represents the end-of-file (EOF) character.
  324. */
  325. @:noUsing public static inline function isEof( c : Int ) : Bool {
  326. #if (flash9 || cpp)
  327. return c == 0;
  328. #elseif flash8
  329. return c <= 0; // fast NaN
  330. #elseif js
  331. return c != c; // fast NaN
  332. #elseif neko
  333. return c == null;
  334. #elseif cs
  335. return c == -1;
  336. #elseif java
  337. return c == -1;
  338. #else
  339. return false;
  340. #end
  341. }
  342. #if java
  343. private static inline function _charAt(str:String, idx:Int):java.StdTypes.Char16 return untyped str._charAt(idx);
  344. #end
  345. #if neko
  346. private static var _urlEncode = neko.Lib.load("std","url_encode",1);
  347. private static var _urlDecode = neko.Lib.load("std","url_decode",1);
  348. #end
  349. }