StringTools.hx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. class StringTools {
  29. /**
  30. Encode an URL by using the standard format.
  31. **/
  32. public static function urlEncode( s : String ) : String untyped {
  33. __python__("from urllib.parse import quote");
  34. return untyped quote(s);
  35. }
  36. /**
  37. Decode an URL using the standard format.
  38. **/
  39. public static function urlDecode( s : String ) : String untyped {
  40. __python__("from urllib.parse import unquote");
  41. return untyped unquote(s);
  42. }
  43. /**
  44. Escapes HTML special characters of the string `s`.
  45. The following replacements are made:
  46. - `&` becomes `&amp`;
  47. - `<` becomes `&lt`;
  48. - `>` becomes `&gt`;
  49. If `quotes` is true, the following characters are also replaced:
  50. - `"` becomes `&quot`;
  51. - `'` becomes `&#039`;
  52. **/
  53. public static function htmlEscape( s : String, ?quotes : Bool ) : String {
  54. s = s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
  55. return quotes ? s.split('"').join("&quot;").split("'").join("&#039;") : s;
  56. }
  57. /**
  58. Unescapes HTML special characters of the string `s`.
  59. This is the inverse operation to htmlEscape, i.e. the following always
  60. holds: htmlUnescape(htmlEscape(s)) == s
  61. The replacements follow:
  62. - `&amp;` becomes `&`
  63. - `&lt;` becomes `<`
  64. - `&gt;` becomes `>`
  65. - `&quot;` becomes `"`
  66. - `&#039;` becomes `'`
  67. **/
  68. public static function htmlUnescape( s : String ) : String {
  69. return s.split("&gt;").join(">").split("&lt;").join("<").split("&quot;").join('"').split("&#039;").join("'").split("&amp;").join("&");
  70. }
  71. /**
  72. Tells if the string `s` starts with the string `start`.
  73. If `start` is null, the result is unspecified.
  74. If `start` is the empty String "", the result is true.
  75. **/
  76. public static #if (cs || java) inline #end function startsWith( s : String, start : String ) : Bool {
  77. return( s.length >= start.length && s.substr(0, start.length) == start );
  78. }
  79. /**
  80. Tells if the string `s` ends with the string `end`.
  81. If `end` is null, the result is unspecified.
  82. If `end` is the empty String "", the result is true.
  83. **/
  84. public static #if (cs || java) inline #end function endsWith( s : String, end : String ) : Bool {
  85. var elen = end.length;
  86. var slen = s.length;
  87. return( slen >= elen && s.substr(slen - elen, elen) == end );
  88. }
  89. /**
  90. Tells if the character in the string `s` at position `pos` is a space.
  91. A character is considered to be a space character if its character code
  92. is 9,10,11,12,13 or 32.
  93. If `s` is the empty String "", or if pos is not a valid position within
  94. `s`, the result is false.
  95. **/
  96. public static function isSpace( s : String, pos : Int ) : Bool {
  97. if (s.length == 0 || pos < 0 || pos >= s.length) return false;
  98. var c = s.charCodeAt( pos );
  99. return (c > 8 && c < 14) || c == 32;
  100. }
  101. /**
  102. Removes leading space characters of `s`.
  103. This function internally calls isSpace() to decide which characters to
  104. remove.
  105. If `s` is the empty String "" or consists only of space characters, the
  106. result is the empty String "".
  107. **/
  108. public #if cs inline #end static function ltrim( s : String ) : String {
  109. var l = s.length;
  110. var r = 0;
  111. while( r < l && isSpace(s,r) ){
  112. r++;
  113. }
  114. if( r > 0 )
  115. return s.substr(r, l-r);
  116. else
  117. return s;
  118. }
  119. /**
  120. Removes trailing space characters of `s`.
  121. This function internally calls isSpace() to decide which characters to
  122. remove.
  123. If `s` is the empty String "" or consists only of space characters, the
  124. result is the empty String "".
  125. **/
  126. public #if cs inline #end static function rtrim( s : String ) : String {
  127. var l = s.length;
  128. var r = 0;
  129. while( r < l && isSpace(s,l-r-1) ){
  130. r++;
  131. }
  132. if( r > 0 ){
  133. return s.substr(0, l-r);
  134. }else{
  135. return s;
  136. }
  137. }
  138. /**
  139. Removes leading and trailing space characters of `s`.
  140. This is a convenience function for ltrim(rtrim(s)).
  141. **/
  142. public static function trim( s : String ) : String {
  143. return ltrim(rtrim(s));
  144. }
  145. /**
  146. Concatenates `c` to `s` until `s.length` is at least `l`.
  147. If `c` is the empty String "" or if `l` does not exceed `s.length`,
  148. `s` is returned unchanged.
  149. If `c.length` is 1, the resulting String length is exactly `l`.
  150. Otherwise the length may exceed `l`.
  151. If `c` is null, the result is unspecified.
  152. **/
  153. public static function lpad( s : String, c : String, l : Int ) : String {
  154. if (c.length <= 0)
  155. return s;
  156. while (s.length < l) {
  157. s = c + s;
  158. }
  159. return s;
  160. }
  161. /**
  162. Appends `c` to `s` until `s.length` is at least `l`.
  163. If `c` is the empty String "" or if `l` does not exceed `s.length`,
  164. `s` is returned unchanged.
  165. If `c.length` is 1, the resulting String length is exactly `l`.
  166. Otherwise the length may exceed `l`.
  167. If `c` is null, the result is unspecified.
  168. **/
  169. public static function rpad( s : String, c : String, l : Int ) : String {
  170. if (c.length <= 0)
  171. return s;
  172. while (s.length < l) {
  173. s = s + c;
  174. }
  175. return s;
  176. }
  177. /**
  178. Replace all occurences of the String `sub` in the String `s` by the
  179. String `by`.
  180. If `sub` is the empty String "", `by` is inserted after each character
  181. of `s`. If `by` is also the empty String "", `s` remains unchanged.
  182. This is a convenience function for `s.split(sub).join(by)`.
  183. If `sub` or `by` are null, the result is unspecified.
  184. **/
  185. public static function replace( s : String, sub : String, by : String ) : String {
  186. return s.split(sub).join(by);
  187. }
  188. /**
  189. Encodes `n` into a hexadecimal representation.
  190. If `digits` is specified, the resulting String is padded with "0" until
  191. its length equals `digits`.
  192. **/
  193. public static function hex( n : Int, ?digits : Int ) {
  194. var s = "";
  195. var hexChars = "0123456789ABCDEF";
  196. do {
  197. s = hexChars.charAt(n&15) + s;
  198. n >>>= 4;
  199. } while( n > 0 );
  200. if (digits != null && s.length < digits) {
  201. var diff = digits - s.length;
  202. for (_ in 0...diff) {
  203. s = "0" + s;
  204. }
  205. }
  206. return s;
  207. }
  208. /**
  209. Returns the character code at position `index` of String `s`.
  210. This method is faster than String.charCodeAt() on most platforms.
  211. However, unlike String.charCodeAt(), the result is unspecified if
  212. `index` is negative or exceeds `s.length`.
  213. This operation is not guaranteed to work if `s` contains the \0
  214. character.
  215. **/
  216. public static inline function fastCodeAt( s : String, index : Int ) : Int untyped {
  217. return if (index >= s.length) -1 else untyped(ord(untyped s[index]));
  218. }
  219. /*
  220. Tells if `c` represents the end-of-file (EOF) character.
  221. */
  222. @:noUsing public static inline function isEof( c : Int ) : Bool {
  223. return c == -1;
  224. }
  225. }