StringTools.hx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #else
  47. return null;
  48. #end
  49. }
  50. /**
  51. Decode an URL using the standard format.
  52. **/
  53. public #if php inline #end static function urlDecode( s : String ) : String untyped {
  54. #if flash9
  55. return __global__["decodeURIComponent"](s.split("+").join(" "));
  56. #elseif flash
  57. return _global["unescape"](s);
  58. #elseif neko
  59. return new String(_urlDecode(s.__s));
  60. #elseif js
  61. return decodeURIComponent(s.split("+").join(" "));
  62. #elseif php
  63. return __call__("urldecode", s);
  64. #else
  65. return null;
  66. #end
  67. }
  68. /**
  69. Escape HTML special characters of the string.
  70. **/
  71. public static function htmlEscape( s : String ) : String {
  72. return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
  73. }
  74. /**
  75. Unescape HTML special characters of the string.
  76. **/
  77. public #if php inline #end static function htmlUnescape( s : String ) : String {
  78. #if php
  79. return untyped __call__("htmlspecialchars_decode", s);
  80. #else
  81. return s.split("&gt;").join(">").split("&lt;").join("<").split("&amp;").join("&");
  82. #end
  83. }
  84. /**
  85. Tells if the string [s] starts with the string [start].
  86. **/
  87. public static function startsWith( s : String, start : String ) {
  88. return( s.length >= start.length && s.substr(0,start.length) == start );
  89. }
  90. /**
  91. Tells if the string [s] ends with the string [end].
  92. **/
  93. public static function endsWith( s : String, end : String ) {
  94. var elen = end.length;
  95. var slen = s.length;
  96. return( slen >= elen && s.substr(slen-elen,elen) == end );
  97. }
  98. /**
  99. Tells if the character in the string [s] at position [pos] is a space.
  100. **/
  101. public static function isSpace( s : String, pos : Int ) : Bool {
  102. var c = s.charCodeAt( pos );
  103. return (c >= 9 && c <= 13) || c == 32;
  104. }
  105. /**
  106. Removes spaces at the left of the String [s].
  107. **/
  108. public #if php inline #end static function ltrim( s : String ) : String {
  109. #if php
  110. return untyped __call__("ltrim", s);
  111. #else
  112. var l = s.length;
  113. var r = 0;
  114. while( r < l && isSpace(s,r) ){
  115. r++;
  116. }
  117. if( r > 0 )
  118. return s.substr(r, l-r);
  119. else
  120. return s;
  121. #end
  122. }
  123. /**
  124. Removes spaces at the right of the String [s].
  125. **/
  126. public #if php inline #end static function rtrim( s : String ) : String {
  127. #if php
  128. return untyped __call__("rtrim", s);
  129. #else
  130. var l = s.length;
  131. var r = 0;
  132. while( r < l && isSpace(s,l-r-1) ){
  133. r++;
  134. }
  135. if( r > 0 ){
  136. return s.substr(0, l-r);
  137. }else{
  138. return s;
  139. }
  140. #end
  141. }
  142. /**
  143. Removes spaces at the beginning and the end of the String [s].
  144. **/
  145. public #if php inline #end static function trim( s : String ) : String {
  146. #if php
  147. return untyped __call__("trim", s);
  148. #else
  149. return ltrim(rtrim(s));
  150. #end
  151. }
  152. /**
  153. Pad the string [s] by appending [c] at its right until it reach [l] characters.
  154. **/
  155. public #if php inline #end static function rpad( s : String, c : String, l : Int ) : String {
  156. #if php
  157. return untyped __call__("str_pad", s, l, c, __php__("STR_PAD_RIGHT"));
  158. #else
  159. var sl = s.length;
  160. var cl = c.length;
  161. while( sl < l ){
  162. if( l - sl < cl ){
  163. s += c.substr(0,l-sl);
  164. sl = l;
  165. }else{
  166. s += c;
  167. sl += cl;
  168. }
  169. }
  170. return s;
  171. #end
  172. }
  173. /**
  174. Pad the string [s] by appending [c] at its left until it reach [l] characters.
  175. **/
  176. public #if php inline #end static function lpad( s : String, c : String, l : Int ) : String {
  177. #if php
  178. return untyped __call__("str_pad", s, l, c, __php__("STR_PAD_LEFT"));
  179. #else
  180. var ns = "";
  181. var sl = s.length;
  182. if( sl >= l ) return s;
  183. var cl = c.length;
  184. while( sl < l ){
  185. if( l - sl < cl ){
  186. ns += c.substr(0,l-sl);
  187. sl = l;
  188. }else{
  189. ns += c;
  190. sl += cl;
  191. }
  192. }
  193. return ns+s;
  194. #end
  195. }
  196. /**
  197. Replace all occurences of the string [sub] in the string [s] by the string [by].
  198. **/
  199. public #if php inline #end static function replace( s : String, sub : String, by : String ) : String {
  200. #if php
  201. return untyped __call__("str_replace", sub, by, s);
  202. #else
  203. return s.split(sub).join(by);
  204. #end
  205. }
  206. /**
  207. Encode a number into a hexadecimal representation, with an optional number of zeros for left padding.
  208. **/
  209. public static function hex( n : Int, ?digits : Int ) {
  210. var neg = false;
  211. if( n < 0 ) {
  212. neg = true;
  213. n = -n;
  214. }
  215. #if (flash || js)
  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%16) + s;
  223. n = Std.int(n/16);
  224. } while( n > 0 );
  225. #end
  226. if( digits != null )
  227. while( s.length < digits )
  228. s = "0"+s;
  229. if( neg )
  230. s = "-"+s;
  231. return s;
  232. }
  233. #if neko
  234. private static var _urlEncode = neko.Lib.load("std","url_encode",1);
  235. private static var _urlDecode = neko.Lib.load("std","url_decode",1);
  236. #end
  237. }