StringTools.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C)2005-2019 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. import php.*;
  23. @:coreApi class StringTools {
  24. public inline static function urlEncode( s : String ) : String {
  25. return Global.rawurlencode(s);
  26. }
  27. public inline static function urlDecode( s : String ) : String {
  28. return Global.urldecode(s);
  29. }
  30. public inline static function htmlEscape( s : String, ?quotes : Bool ) : String {
  31. return Global.htmlspecialchars(s, (quotes ? Const.ENT_QUOTES | Const.ENT_HTML401 : Const.ENT_NOQUOTES));
  32. }
  33. public inline static function htmlUnescape( s : String ) : String {
  34. return Global.htmlspecialchars_decode(s, Const.ENT_QUOTES);
  35. }
  36. public inline static function contains(s : String, value : String) : Bool {
  37. return s.indexOf(value) != -1;
  38. }
  39. public static function startsWith( s : String, start : String ) : Bool {
  40. return start == '' || Global.strpos(s, start) == 0;
  41. }
  42. public static function endsWith( s : String, end : String ) : Bool {
  43. return end == '' || Global.substr(s, -end.length) == end;
  44. }
  45. public static function isSpace( s : String, pos : Int ) : Bool {
  46. var c = s.charCodeAt( pos );
  47. return (c >= 9 && c <= 13) || c == 32;
  48. }
  49. public inline static function ltrim( s : String ) : String {
  50. return Global.ltrim(s);
  51. }
  52. public inline static function rtrim( s : String ) : String {
  53. return Global.rtrim(s);
  54. }
  55. public inline static function trim( s : String ) : String {
  56. return Global.trim(s);
  57. }
  58. public static function rpad( s : String, c : String, l : Int ) : String {
  59. var cLength = c.length;
  60. var sLength = s.length;
  61. if (cLength == 0 || sLength >= l) return s;
  62. var padLength = l - sLength;
  63. var padCount = Syntax.int(padLength / cLength);
  64. if(padCount > 0) {
  65. var result = Global.str_pad(s, Global.strlen(s) + padCount * Global.strlen(c), c, Const.STR_PAD_RIGHT);
  66. return (padCount * cLength >= padLength) ? result : Syntax.concat(result, c);
  67. } else {
  68. return Syntax.concat(s, c);
  69. }
  70. }
  71. public static function lpad( s : String, c : String, l : Int ) : String {
  72. var cLength = c.length;
  73. var sLength = s.length;
  74. if (cLength == 0 || sLength >= l) return s;
  75. var padLength = l - sLength;
  76. var padCount = Syntax.int(padLength / cLength);
  77. if(padCount > 0) {
  78. var result = Global.str_pad(s, Global.strlen(s) + padCount * Global.strlen(c), c, Const.STR_PAD_LEFT);
  79. return (padCount * cLength >= padLength) ? result : Syntax.concat(c, result);
  80. } else {
  81. return Syntax.concat(c, s);
  82. }
  83. }
  84. public static function replace( s : String, sub : String, by : String ) : String {
  85. if (sub == '') {
  86. return Global.implode(by, Global.preg_split('//u', s, -1, Const.PREG_SPLIT_NO_EMPTY));
  87. }
  88. return Global.str_replace(sub, by, s);
  89. }
  90. public static function hex( n : Int, ?digits : Int ) : String {
  91. var s = Global.dechex(n);
  92. var len = 8;
  93. if (Global.strlen(s) > (null == digits ? len : (len = digits > len ? digits : len)))
  94. s = s.substr(-len);
  95. else if ( digits != null )
  96. s = lpad(s, '0', digits);
  97. return s.toUpperCase();
  98. }
  99. public static function fastCodeAt( s : String, index : Int ) : Int {
  100. var char:NativeString = (index == 0 ? s : Global.mb_substr(s, index, 1));
  101. if(char == '') return 0;
  102. return Boot.unsafeOrd(char);
  103. }
  104. @:noUsing public static inline function isEof( c : Int ) : Bool {
  105. return c == 0;
  106. }
  107. @:noCompletion
  108. @:deprecated('StringTools.quoteUnixArg() is deprecated. Use haxe.SysTools.quoteUnixArg() instead.')
  109. public static function quoteUnixArg(argument:String):String {
  110. return inline haxe.SysTools.quoteUnixArg(argument);
  111. }
  112. @:noCompletion
  113. @:deprecated('StringTools.winMetaCharacters is deprecated. Use haxe.SysTools.winMetaCharacters instead.')
  114. public static var winMetaCharacters:Array<Int> = cast haxe.SysTools.winMetaCharacters;
  115. @:noCompletion
  116. @:deprecated('StringTools.quoteWinArg() is deprecated. Use haxe.SysTools.quoteWinArg() instead.')
  117. public static function quoteWinArg(argument:String, escapeMetaCharacters:Bool):String {
  118. return inline haxe.SysTools.quoteWinArg(argument, escapeMetaCharacters);
  119. }
  120. }