StringTools.hx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. The StringTools class contains some extra functionalities for [String]
  24. manipulation. It's stored in a different class in order to prevent
  25. the standard [String] of being bloated and thus increasing the size of
  26. each application using it.
  27. **/
  28. #if cs
  29. @:keep
  30. #end
  31. class StringTools {
  32. /**
  33. Encode an URL by using the standard format.
  34. **/
  35. public #if php inline #end static function urlEncode( s : String ) : String untyped {
  36. #if flash9
  37. return __global__["encodeURIComponent"](s);
  38. #elseif flash
  39. return _global["escape"](s);
  40. #elseif neko
  41. return new String(_urlEncode(s.__s));
  42. #elseif js
  43. return encodeURIComponent(s);
  44. #elseif php
  45. return __call__("rawurlencode", s);
  46. #elseif cpp
  47. return s.__URLEncode();
  48. #elseif java
  49. try
  50. return untyped __java__("java.net.URLEncoder.encode(s, \"UTF-8\")")
  51. catch (e:Dynamic) throw e;
  52. #elseif cs
  53. return untyped __cs__("System.Uri.EscapeUriString(s)");
  54. #else
  55. return null;
  56. #end
  57. }
  58. /**
  59. Decode an URL using the standard format.
  60. **/
  61. public #if php inline #end static function urlDecode( s : String ) : String untyped {
  62. #if flash9
  63. return __global__["decodeURIComponent"](s.split("+").join(" "));
  64. #elseif flash
  65. return _global["unescape"](s);
  66. #elseif neko
  67. return new String(_urlDecode(s.__s));
  68. #elseif js
  69. return decodeURIComponent(s.split("+").join(" "));
  70. #elseif php
  71. return __call__("urldecode", s);
  72. #elseif cpp
  73. return s.__URLDecode();
  74. #elseif java
  75. try
  76. return untyped __java__("java.net.URLDecoder.decode(s, \"UTF-8\")")
  77. catch (e:Dynamic) throw e;
  78. #elseif cs
  79. return untyped __cs__("System.Uri.UnescapeDataString(s)");
  80. #else
  81. return null;
  82. #end
  83. }
  84. /**
  85. Escape HTML special characters of the string.
  86. **/
  87. public static function htmlEscape( s : String ) : String {
  88. return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;").split('"').join("&quot;").split("'").join("&#039;");
  89. }
  90. /**
  91. Unescape HTML special characters of the string.
  92. **/
  93. public #if php inline #end static function htmlUnescape( s : String ) : String {
  94. #if php
  95. return untyped __call__("htmlspecialchars_decode", s);
  96. #else
  97. return s.split("&gt;").join(">").split("&lt;").join("<").split("&quot;").join('"').split("&#039;").join("'").split("&amp;").join("&");
  98. #end
  99. }
  100. /**
  101. Tells if the string [s] starts with the string [start].
  102. **/
  103. public static #if (cs || java) inline #end function startsWith( s : String, start : String ) : Bool {
  104. #if java
  105. return untyped s.startsWith(start);
  106. #elseif cs
  107. return untyped s.StartsWith(start);
  108. #else
  109. return( s.length >= start.length && s.substr(0, start.length) == start );
  110. #end
  111. }
  112. /**
  113. Tells if the string [s] ends with the string [end].
  114. **/
  115. public static #if (cs || java) inline #end function endsWith( s : String, end : String ) : Bool {
  116. #if java
  117. return untyped s.endsWith(end);
  118. #elseif cs
  119. return untyped s.EndsWith(end);
  120. #else
  121. var elen = end.length;
  122. var slen = s.length;
  123. return( slen >= elen && s.substr(slen - elen, elen) == end );
  124. #end
  125. }
  126. /**
  127. Tells if the character in the string [s] at position [pos] is a space.
  128. **/
  129. public static function isSpace( s : String, pos : Int ) : Bool {
  130. var c = s.charCodeAt( pos );
  131. return (c >= 9 && c <= 13) || c == 32;
  132. }
  133. /**
  134. Removes spaces at the left of the String [s].
  135. **/
  136. public #if (php || cs) inline #end static function ltrim( s : String ) : String {
  137. #if php
  138. return untyped __call__("ltrim", s);
  139. #elseif cs
  140. return untyped s.TrimStart();
  141. #else
  142. var l = s.length;
  143. var r = 0;
  144. while( r < l && isSpace(s,r) ){
  145. r++;
  146. }
  147. if( r > 0 )
  148. return s.substr(r, l-r);
  149. else
  150. return s;
  151. #end
  152. }
  153. /**
  154. Removes spaces at the right of the String [s].
  155. **/
  156. public #if (php || cs) inline #end static function rtrim( s : String ) : String {
  157. #if php
  158. return untyped __call__("rtrim", s);
  159. #elseif cs
  160. return untyped s.TrimEnd();
  161. #else
  162. var l = s.length;
  163. var r = 0;
  164. while( r < l && isSpace(s,l-r-1) ){
  165. r++;
  166. }
  167. if( r > 0 ){
  168. return s.substr(0, l-r);
  169. }else{
  170. return s;
  171. }
  172. #end
  173. }
  174. /**
  175. Removes spaces at the beginning and the end of the String [s].
  176. **/
  177. public #if (php || cs || java) inline #end static function trim( s : String ) : String {
  178. #if php
  179. return untyped __call__("trim", s);
  180. #elseif cs
  181. return untyped s.Trim();
  182. #elseif java
  183. return untyped s.trim();
  184. #else
  185. return ltrim(rtrim(s));
  186. #end
  187. }
  188. /**
  189. Pad the string [s] by appending [c] at its right until it reach [l] characters.
  190. **/
  191. public #if php inline #end static function rpad( s : String, c : String, l : Int ) : String {
  192. #if php
  193. return untyped __call__("str_pad", s, l, c, __php__("STR_PAD_RIGHT"));
  194. #else
  195. var sl = s.length;
  196. var cl = c.length;
  197. while( sl < l ){
  198. if( l - sl < cl ){
  199. s += c.substr(0,l-sl);
  200. sl = l;
  201. }else{
  202. s += c;
  203. sl += cl;
  204. }
  205. }
  206. return s;
  207. #end
  208. }
  209. /**
  210. Pad the string [s] by appending [c] at its left until it reach [l] characters.
  211. **/
  212. public #if php inline #end static function lpad( s : String, c : String, l : Int ) : String {
  213. #if php
  214. return untyped __call__("str_pad", s, l, c, __php__("STR_PAD_LEFT"));
  215. #else
  216. var ns = "";
  217. var sl = s.length;
  218. if( sl >= l ) return s;
  219. var cl = c.length;
  220. while( sl < l ){
  221. if( l - sl < cl ){
  222. ns += c.substr(0,l-sl);
  223. sl = l;
  224. }else{
  225. ns += c;
  226. sl += cl;
  227. }
  228. }
  229. return ns+s;
  230. #end
  231. }
  232. /**
  233. Replace all occurences of the string [sub] in the string [s] by the string [by].
  234. **/
  235. public #if (php || java || cs) inline #end static function replace( s : String, sub : String, by : String ) : String {
  236. #if php
  237. return untyped __call__("str_replace", sub, by, s);
  238. #elseif java
  239. return untyped s.replace(sub, by);
  240. #elseif cs
  241. return untyped s.Replace(sub, by);
  242. #else
  243. return s.split(sub).join(by);
  244. #end
  245. }
  246. /**
  247. Encode a number into a hexadecimal representation, with an optional number of zeros for left padding.
  248. **/
  249. public static function hex( n : Int, ?digits : Int ) {
  250. #if flash9
  251. var n : UInt = n;
  252. var s : String = untyped n.toString(16);
  253. s = s.toUpperCase();
  254. #else
  255. var s = "";
  256. var hexChars = "0123456789ABCDEF";
  257. do {
  258. s = hexChars.charAt(n&15) + s;
  259. n >>>= 4;
  260. } while( n > 0 );
  261. #end
  262. if( digits != null )
  263. while( s.length < digits )
  264. s = "0"+s;
  265. return s;
  266. }
  267. /**
  268. Provides a fast native string charCodeAt access. Since the EOF value might vary depending on the platforms, always test with StringTools.isEOF.
  269. Only guaranteed to work if index in [0,s.length] range. Might not work with strings containing \0 char.
  270. **/
  271. public static inline function fastCodeAt( s : String, index : Int ) : Int untyped {
  272. #if neko
  273. return untyped __dollar__sget(s.__s, index);
  274. #elseif cpp
  275. return s.cca(index);
  276. #elseif flash9
  277. return s.cca(index);
  278. #elseif flash
  279. return s["cca"](index);
  280. #elseif java
  281. return ( index < s.length ) ? cast(_charAt(s, index), Int) : -1;
  282. #elseif cs
  283. return ( cast(index, UInt) < s.length ) ? cast(untyped s[index], Int) : -1;
  284. #elseif js
  285. #if mt
  286. return (untyped s).cca(index);
  287. #else
  288. return (untyped s).charCodeAt(index);
  289. #end
  290. #else
  291. return s.cca(index);
  292. #end
  293. }
  294. #if java
  295. private static inline function _charAt(str:String, idx:Int):java.StdTypes.Char16 return untyped str._charAt(idx)
  296. #end
  297. /*
  298. Only to use together with fastCodeAt.
  299. */
  300. public static inline function isEOF( c : Int ) : Bool {
  301. #if (flash9 || cpp)
  302. return c == 0;
  303. #elseif flash8
  304. return c <= 0; // fast NaN
  305. #elseif js
  306. return c != c; // fast NaN
  307. #elseif neko
  308. return c == null;
  309. #elseif cs
  310. return c == -1;
  311. #elseif java
  312. return c == -1;
  313. #else
  314. return false;
  315. #end
  316. }
  317. #if neko
  318. private static var _urlEncode = neko.Lib.load("std","url_encode",1);
  319. private static var _urlDecode = neko.Lib.load("std","url_decode",1);
  320. #end
  321. }