StringTools.hx 3.5 KB

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