2
0

StringTools.hx 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (C)2005-2015 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. @:coreApi class StringTools {
  23. public inline static function urlEncode( s : String ) : String untyped {
  24. return __call__("rawurlencode", s);
  25. }
  26. public inline static function urlDecode( s : String ) : String untyped {
  27. return __call__("urldecode", s);
  28. }
  29. public static function htmlEscape( s : String, ?quotes : Bool ) : String {
  30. s = s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
  31. return quotes ? s.split('"').join("&quot;").split("'").join("&#039;") : s;
  32. }
  33. public inline static function htmlUnescape( s : String ) : String {
  34. return untyped __call__("htmlspecialchars_decode", s ,__php__("ENT_QUOTES"));
  35. }
  36. public static function startsWith( s : String, start : String ) : Bool {
  37. return( s.length >= start.length && s.substr(0,start.length) == start );
  38. }
  39. public static function endsWith( s : String, end : String ) : Bool {
  40. var elen = end.length;
  41. var slen = s.length;
  42. return( slen >= elen && s.substr(slen-elen,elen) == end );
  43. }
  44. public static function isSpace( s : String, pos : Int ) : Bool {
  45. var c = s.charCodeAt( pos );
  46. return (c >= 9 && c <= 13) || c == 32;
  47. }
  48. public inline static function ltrim( s : String ) : String {
  49. return untyped __call__("ltrim", s);
  50. }
  51. public inline static function rtrim( s : String ) : String {
  52. return untyped __call__("rtrim", s);
  53. }
  54. public inline static function trim( s : String ) : String {
  55. return untyped __call__("trim", s);
  56. }
  57. public inline static function rpad( s : String, c : String, l : Int ) : String {
  58. return c.length == 0 || s.length >= l ? s : untyped __call__("str_pad", s, Math.ceil((l-s.length) / c.length) * c.length + s.length , c, __php__("STR_PAD_RIGHT"));
  59. }
  60. public inline static function lpad( s : String, c : String, l : Int ) : String {
  61. return c.length == 0 || s.length >= l ? s : untyped __call__("str_pad", s, Math.ceil((l-s.length) / c.length) * c.length + s.length , c, __php__("STR_PAD_LEFT"));
  62. }
  63. public inline static function replace( s : String, sub : String, by : String ) : String {
  64. return untyped sub=="" ? __call__("implode", __call__("str_split ", s) , by) : __call__("str_replace", sub, by, s);
  65. }
  66. public static function hex( n : Int, ?digits : Int ) : String {
  67. var s : String = untyped __call__("dechex", n),
  68. len = 8;
  69. if (s.length > (null == digits ? len : (len = digits > len ? digits : len)))
  70. s = s.substr(-len);
  71. else if ( digits != null )
  72. s = lpad(s, '0', digits);
  73. return s.toUpperCase();
  74. }
  75. public static inline function fastCodeAt( s : String, index : Int ) : Int {
  76. return untyped s.cca(index);
  77. }
  78. public static inline function isEof( c : Int ) : Bool {
  79. return untyped __physeq__(c, 0);
  80. }
  81. }