StringTools.hx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #else
  49. return null;
  50. #end
  51. }
  52. /**
  53. Decode an URL using the standard format.
  54. **/
  55. public #if php inline #end static function urlDecode( s : String ) : String untyped {
  56. #if flash9
  57. return __global__["decodeURIComponent"](s.split("+").join(" "));
  58. #elseif flash
  59. return _global["unescape"](s);
  60. #elseif neko
  61. return new String(_urlDecode(s.__s));
  62. #elseif js
  63. return decodeURIComponent(s.split("+").join(" "));
  64. #elseif php
  65. return __call__("urldecode", s);
  66. #elseif cpp
  67. return s.__URLDecode();
  68. #else
  69. return null;
  70. #end
  71. }
  72. /**
  73. Escape HTML special characters of the string.
  74. **/
  75. public static function htmlEscape( s : String ) : String {
  76. return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
  77. }
  78. /**
  79. Unescape HTML special characters of the string.
  80. **/
  81. public #if php inline #end static function htmlUnescape( s : String ) : String {
  82. #if php
  83. return untyped __call__("htmlspecialchars_decode", s);
  84. #else
  85. return s.split("&gt;").join(">").split("&lt;").join("<").split("&amp;").join("&");
  86. #end
  87. }
  88. /**
  89. Tells if the string [s] starts with the string [start].
  90. **/
  91. public static function startsWith( s : String, start : String ) {
  92. return( s.length >= start.length && s.substr(0,start.length) == start );
  93. }
  94. /**
  95. Tells if the string [s] ends with the string [end].
  96. **/
  97. public static function endsWith( s : String, end : String ) {
  98. var elen = end.length;
  99. var slen = s.length;
  100. return( slen >= elen && s.substr(slen-elen,elen) == end );
  101. }
  102. /**
  103. Tells if the character in the string [s] at position [pos] is a space.
  104. **/
  105. public static function isSpace( s : String, pos : Int ) : Bool {
  106. var c = s.charCodeAt( pos );
  107. return (c >= 9 && c <= 13) || c == 32;
  108. }
  109. /**
  110. Removes spaces at the left of the String [s].
  111. **/
  112. public #if php inline #end static function ltrim( s : String ) : String {
  113. #if php
  114. return untyped __call__("ltrim", s);
  115. #else
  116. var l = s.length;
  117. var r = 0;
  118. while( r < l && isSpace(s,r) ){
  119. r++;
  120. }
  121. if( r > 0 )
  122. return s.substr(r, l-r);
  123. else
  124. return s;
  125. #end
  126. }
  127. /**
  128. Removes spaces at the right of the String [s].
  129. **/
  130. public #if php inline #end static function rtrim( s : String ) : String {
  131. #if php
  132. return untyped __call__("rtrim", s);
  133. #else
  134. var l = s.length;
  135. var r = 0;
  136. while( r < l && isSpace(s,l-r-1) ){
  137. r++;
  138. }
  139. if( r > 0 ){
  140. return s.substr(0, l-r);
  141. }else{
  142. return s;
  143. }
  144. #end
  145. }
  146. /**
  147. Removes spaces at the beginning and the end of the String [s].
  148. **/
  149. public #if php inline #end static function trim( s : String ) : String {
  150. #if php
  151. return untyped __call__("trim", s);
  152. #else
  153. return ltrim(rtrim(s));
  154. #end
  155. }
  156. /**
  157. Pad the string [s] by appending [c] at its right until it reach [l] characters.
  158. **/
  159. public #if php inline #end static function rpad( s : String, c : String, l : Int ) : String {
  160. #if php
  161. return untyped __call__("str_pad", s, l, c, __php__("STR_PAD_RIGHT"));
  162. #else
  163. var sl = s.length;
  164. var cl = c.length;
  165. while( sl < l ){
  166. if( l - sl < cl ){
  167. s += c.substr(0,l-sl);
  168. sl = l;
  169. }else{
  170. s += c;
  171. sl += cl;
  172. }
  173. }
  174. return s;
  175. #end
  176. }
  177. /**
  178. Pad the string [s] by appending [c] at its left until it reach [l] characters.
  179. **/
  180. public #if php inline #end static function lpad( s : String, c : String, l : Int ) : String {
  181. #if php
  182. return untyped __call__("str_pad", s, l, c, __php__("STR_PAD_LEFT"));
  183. #else
  184. var ns = "";
  185. var sl = s.length;
  186. if( sl >= l ) return s;
  187. var cl = c.length;
  188. while( sl < l ){
  189. if( l - sl < cl ){
  190. ns += c.substr(0,l-sl);
  191. sl = l;
  192. }else{
  193. ns += c;
  194. sl += cl;
  195. }
  196. }
  197. return ns+s;
  198. #end
  199. }
  200. /**
  201. Replace all occurences of the string [sub] in the string [s] by the string [by].
  202. **/
  203. public #if php inline #end static function replace( s : String, sub : String, by : String ) : String {
  204. #if php
  205. return untyped __call__("str_replace", sub, by, s);
  206. #else
  207. return s.split(sub).join(by);
  208. #end
  209. }
  210. /**
  211. Encode a number into a hexadecimal representation, with an optional number of zeros for left padding.
  212. **/
  213. public static function hex( n : Int, ?digits : Int ) {
  214. #if flash9
  215. var n : UInt = n;
  216. var s : String = untyped n.toString(16);
  217. s = s.toUpperCase();
  218. #else
  219. var s = "";
  220. var hexChars = "0123456789ABCDEF";
  221. do {
  222. s = hexChars.charAt(n&15) + s;
  223. n >>>= 4;
  224. } while( n > 0 );
  225. #end
  226. if( digits != null )
  227. while( s.length < digits )
  228. s = "0"+s;
  229. return s;
  230. }
  231. /**
  232. Provides a fast native string charCodeAt access. Since the EOF value might vary depending on the platforms, always test with StringTools.isEOF.
  233. Only guaranteed to work if index in [0,s.length] range. Might not work with strings containing \0 char.
  234. **/
  235. public static inline function fastCodeAt( s : String, index : Int ) : Int untyped {
  236. #if neko
  237. return untyped __dollar__sget(s.__s, index);
  238. #elseif cpp
  239. return s.cca(index);
  240. #elseif flash9
  241. return s.cca(index);
  242. #elseif flash
  243. return s["cca"](index);
  244. #else
  245. return s.cca(index);
  246. #end
  247. }
  248. /*
  249. Only to use together with fastCodeAt.
  250. */
  251. public static inline function isEOF( c : Int ) : Bool {
  252. #if (flash9 || cpp)
  253. return c == 0;
  254. #elseif flash8
  255. return c <= 0; // fast NaN
  256. #elseif js
  257. return c != c; // fast NaN
  258. #elseif neko
  259. return c == null;
  260. #else
  261. return false;
  262. #end
  263. }
  264. #if neko
  265. private static var _urlEncode = neko.Lib.load("std","url_encode",1);
  266. private static var _urlDecode = neko.Lib.load("std","url_decode",1);
  267. #end
  268. }