StringTools.hx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. /**
  26. The StringTools class contains some extra functionalities for [String]
  27. manipulation. It's stored in a different class in order to prevent
  28. the standard [String] of being bloated and thus increasing the size of
  29. each application using it.
  30. **/
  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 jvm
  49. return untyped __java__("java.net.URLEncoder.encode(s)");
  50. #else
  51. return null;
  52. #end
  53. }
  54. /**
  55. Decode an URL using the standard format.
  56. **/
  57. public #if php inline #end static function urlDecode( s : String ) : String untyped {
  58. #if flash9
  59. return __global__["decodeURIComponent"](s.split("+").join(" "));
  60. #elseif flash
  61. return _global["unescape"](s);
  62. #elseif neko
  63. return new String(_urlDecode(s.__s));
  64. #elseif js
  65. return decodeURIComponent(s.split("+").join(" "));
  66. #elseif php
  67. return __call__("urldecode", s);
  68. #elseif cpp
  69. return s.__URLDecode();
  70. #elseif jvm
  71. return untyped __java__("java.net.URLDecoder.decode(s)");
  72. #else
  73. return null;
  74. #end
  75. }
  76. /**
  77. Escape HTML special characters of the string.
  78. **/
  79. public static function htmlEscape( s : String ) : String {
  80. return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
  81. }
  82. /**
  83. Unescape HTML special characters of the string.
  84. **/
  85. public #if php inline #end static function htmlUnescape( s : String ) : String {
  86. #if php
  87. return untyped __call__("htmlspecialchars_decode", s);
  88. #else
  89. return s.split("&gt;").join(">").split("&lt;").join("<").split("&amp;").join("&");
  90. #end
  91. }
  92. /**
  93. Tells if the string [s] starts with the string [start].
  94. **/
  95. public static #if jvm inline #end function startsWith( s : String, start : String ) {
  96. #if jvm
  97. return untyped s.startsWith(start);
  98. #else
  99. return( s.length >= start.length && s.substr(0, start.length) == start );
  100. #end
  101. }
  102. /**
  103. Tells if the string [s] ends with the string [end].
  104. **/
  105. public static #if jvm inline #end function endsWith( s : String, end : String ) {
  106. #if jvm
  107. return untyped s.endsWith(end);
  108. #else
  109. var elen = end.length;
  110. var slen = s.length;
  111. return( slen >= elen && s.substr(slen - elen, elen) == end );
  112. #end
  113. }
  114. /**
  115. Tells if the character in the string [s] at position [pos] is a space.
  116. **/
  117. public static function isSpace( s : String, pos : Int ) : Bool {
  118. var c = s.charCodeAt( pos );
  119. return (c >= 9 && c <= 13) || c == 32;
  120. }
  121. /**
  122. Removes spaces at the left of the String [s].
  123. **/
  124. public #if php inline #end static function ltrim( s : String ) : String {
  125. #if php
  126. return untyped __call__("ltrim", s);
  127. #else
  128. var l = s.length;
  129. var r = 0;
  130. while( r < l && isSpace(s,r) ){
  131. r++;
  132. }
  133. if( r > 0 )
  134. return s.substr(r, l-r);
  135. else
  136. return s;
  137. #end
  138. }
  139. /**
  140. Removes spaces at the right of the String [s].
  141. **/
  142. public #if php inline #end static function rtrim( s : String ) : String {
  143. #if php
  144. return untyped __call__("rtrim", s);
  145. #else
  146. var l = s.length;
  147. var r = 0;
  148. while( r < l && isSpace(s,l-r-1) ){
  149. r++;
  150. }
  151. if( r > 0 ){
  152. return s.substr(0, l-r);
  153. }else{
  154. return s;
  155. }
  156. #end
  157. }
  158. /**
  159. Removes spaces at the beginning and the end of the String [s].
  160. **/
  161. public #if php inline #end static function trim( s : String ) : String {
  162. #if php
  163. return untyped __call__("trim", s);
  164. #else
  165. return ltrim(rtrim(s));
  166. #end
  167. }
  168. /**
  169. Pad the string [s] by appending [c] at its right until it reach [l] characters.
  170. **/
  171. public #if php inline #end static function rpad( s : String, c : String, l : Int ) : String {
  172. #if php
  173. return untyped __call__("str_pad", s, l, c, __php__("STR_PAD_RIGHT"));
  174. #else
  175. var sl = s.length;
  176. var cl = c.length;
  177. while( sl < l ){
  178. if( l - sl < cl ){
  179. s += c.substr(0,l-sl);
  180. sl = l;
  181. }else{
  182. s += c;
  183. sl += cl;
  184. }
  185. }
  186. return s;
  187. #end
  188. }
  189. /**
  190. Pad the string [s] by appending [c] at its left until it reach [l] characters.
  191. **/
  192. public #if php inline #end static function lpad( s : String, c : String, l : Int ) : String {
  193. #if php
  194. return untyped __call__("str_pad", s, l, c, __php__("STR_PAD_LEFT"));
  195. #else
  196. var ns = "";
  197. var sl = s.length;
  198. if( sl >= l ) return s;
  199. var cl = c.length;
  200. while( sl < l ){
  201. if( l - sl < cl ){
  202. ns += c.substr(0,l-sl);
  203. sl = l;
  204. }else{
  205. ns += c;
  206. sl += cl;
  207. }
  208. }
  209. return ns+s;
  210. #end
  211. }
  212. /**
  213. Replace all occurences of the string [sub] in the string [s] by the string [by].
  214. **/
  215. public #if (php || jvm) inline #end static function replace( s : String, sub : String, by : String ) : String {
  216. #if php
  217. return untyped __call__("str_replace", sub, by, s);
  218. #elseif jvm
  219. return untyped s.replace(sub, by);
  220. #else
  221. return s.split(sub).join(by);
  222. #end
  223. }
  224. /**
  225. Encode a number into a hexadecimal representation, with an optional number of zeros for left padding.
  226. **/
  227. public static function hex( n : Int, ?digits : Int ) {
  228. #if flash9
  229. var n : UInt = n;
  230. var s : String = untyped n.toString(16);
  231. s = s.toUpperCase();
  232. #else
  233. var s = "";
  234. var hexChars = "0123456789ABCDEF";
  235. do {
  236. s = hexChars.charAt(n&15) + s;
  237. n >>>= 4;
  238. } while( n > 0 );
  239. #end
  240. if( digits != null )
  241. while( s.length < digits )
  242. s = "0"+s;
  243. return s;
  244. }
  245. /**
  246. Provides a fast native string charCodeAt access. Since the EOF value might vary depending on the platforms, always test with StringTools.isEOF.
  247. Only guaranteed to work if index in [0,s.length] range. Might not work with strings containing \0 char.
  248. **/
  249. public static inline function fastCodeAt( s : String, index : Int ) : Int untyped {
  250. #if neko
  251. return untyped __dollar__sget(s.__s, index);
  252. #elseif cpp
  253. return s.cca(index);
  254. #elseif flash9
  255. return s.cca(index);
  256. #elseif flash
  257. return s["cca"](index);
  258. #else
  259. return s.cca(index);
  260. #end
  261. }
  262. /*
  263. Only to use together with fastCodeAt.
  264. */
  265. public static inline function isEOF( c : Int ) : Bool {
  266. #if (flash9 || cpp)
  267. return c == 0;
  268. #elseif flash8
  269. return c <= 0; // fast NaN
  270. #elseif js
  271. return c != c; // fast NaN
  272. #elseif neko
  273. return c == null;
  274. #else
  275. return false;
  276. #end
  277. }
  278. #if neko
  279. private static var _urlEncode = neko.Lib.load("std","url_encode",1);
  280. private static var _urlDecode = neko.Lib.load("std","url_decode",1);
  281. #end
  282. }