StringTools.hx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. import haxe.iterators.StringIterator;
  24. import haxe.iterators.StringKeyValueIterator;
  25. @:coreApi class StringTools {
  26. public inline static function urlEncode( s : String ) : String {
  27. return Global.rawurlencode(s);
  28. }
  29. public inline static function urlDecode( s : String ) : String {
  30. return Global.urldecode(s);
  31. }
  32. public inline static function htmlEscape( s : String, ?quotes : Bool ) : String {
  33. return Global.htmlspecialchars(s, (quotes ? Const.ENT_QUOTES | Const.ENT_HTML401 : Const.ENT_NOQUOTES));
  34. }
  35. public inline static function htmlUnescape( s : String ) : String {
  36. return Global.htmlspecialchars_decode(s, Const.ENT_QUOTES);
  37. }
  38. public inline static function contains(s : String, value : String) : Bool {
  39. return s.indexOf(value) != -1;
  40. }
  41. public static function startsWith( s : String, start : String ) : Bool {
  42. return start == '' || Global.substr(s, 0, Global.strlen(start)) == start;
  43. }
  44. public static function endsWith( s : String, end : String ) : Bool {
  45. return end == '' || Global.substr(s, -Global.strlen(end)) == end;
  46. }
  47. public static function isSpace( s : String, pos : Int ) : Bool {
  48. var c = s.charCodeAt( pos );
  49. return (c >= 9 && c <= 13) || c == 32;
  50. }
  51. public inline static function ltrim( s : String ) : String {
  52. return Global.ltrim(s);
  53. }
  54. public inline static function rtrim( s : String ) : String {
  55. return Global.rtrim(s);
  56. }
  57. public inline static function trim( s : String ) : String {
  58. return Global.trim(s);
  59. }
  60. public static function rpad( s : String, c : String, l : Int ) : String {
  61. var cLength = c.length;
  62. var sLength = s.length;
  63. if (cLength == 0 || sLength >= l) return s;
  64. var padLength = l - sLength;
  65. var padCount = Syntax.int(padLength / cLength);
  66. if(padCount > 0) {
  67. var result = Global.str_pad(s, Global.strlen(s) + padCount * Global.strlen(c), c, Const.STR_PAD_RIGHT);
  68. return (padCount * cLength >= padLength) ? result : Syntax.concat(result, c);
  69. } else {
  70. return Syntax.concat(s, c);
  71. }
  72. }
  73. public static function lpad( s : String, c : String, l : Int ) : String {
  74. var cLength = c.length;
  75. var sLength = s.length;
  76. if (cLength == 0 || sLength >= l) return s;
  77. var padLength = l - sLength;
  78. var padCount = Syntax.int(padLength / cLength);
  79. if(padCount > 0) {
  80. var result = Global.str_pad(s, Global.strlen(s) + padCount * Global.strlen(c), c, Const.STR_PAD_LEFT);
  81. return (padCount * cLength >= padLength) ? result : Syntax.concat(c, result);
  82. } else {
  83. return Syntax.concat(c, s);
  84. }
  85. }
  86. public static function replace( s : String, sub : String, by : String ) : String {
  87. if (sub == '') {
  88. return Global.implode(by, Global.preg_split('//u', s, -1, Const.PREG_SPLIT_NO_EMPTY));
  89. }
  90. return Global.str_replace(sub, by, s);
  91. }
  92. public static function hex( n : Int, ?digits : Int ) : String {
  93. var s = Global.dechex(n);
  94. var len = 8;
  95. if (Global.strlen(s) > (null == digits ? len : (len = digits > len ? digits : len)))
  96. s = s.substr(-len);
  97. else if ( digits != null )
  98. s = lpad(s, '0', digits);
  99. return s.toUpperCase();
  100. }
  101. public static function fastCodeAt( s : String, index : Int ) : Int {
  102. var char:NativeString = (index == 0 ? s : Global.mb_substr(s, index, 1));
  103. if(char == '') return 0;
  104. return Boot.unsafeOrd(char);
  105. }
  106. /**
  107. Returns an iterator of the char codes.
  108. Note that char codes may differ across platforms because of different
  109. internal encoding of strings in different runtimes.
  110. For the consistent cross-platform UTF8 char codes see `haxe.iterators.StringIteratorUnicode`.
  111. **/
  112. public static inline function iterator( s : String ) : StringIterator {
  113. return new StringIterator(s);
  114. }
  115. /**
  116. Returns an iterator of the char indexes and codes.
  117. Note that char codes may differ across platforms because of different
  118. internal encoding of strings in different of runtimes.
  119. For the consistent cross-platform UTF8 char codes see `haxe.iterators.StringKeyValueIteratorUnicode`.
  120. **/
  121. public static inline function keyValueIterator( s : String ) : StringKeyValueIterator {
  122. return new StringKeyValueIterator(s);
  123. }
  124. @:noUsing public static inline function isEof( c : Int ) : Bool {
  125. return c == 0;
  126. }
  127. @:noCompletion
  128. @:deprecated('StringTools.quoteUnixArg() is deprecated. Use haxe.SysTools.quoteUnixArg() instead.')
  129. public static function quoteUnixArg(argument:String):String {
  130. return inline haxe.SysTools.quoteUnixArg(argument);
  131. }
  132. @:noCompletion
  133. @:deprecated('StringTools.winMetaCharacters is deprecated. Use haxe.SysTools.winMetaCharacters instead.')
  134. public static var winMetaCharacters:Array<Int> = cast haxe.SysTools.winMetaCharacters;
  135. @:noCompletion
  136. @:deprecated('StringTools.quoteWinArg() is deprecated. Use haxe.SysTools.quoteWinArg() instead.')
  137. public static function quoteWinArg(argument:String, escapeMetaCharacters:Bool):String {
  138. return inline haxe.SysTools.quoteWinArg(argument, escapeMetaCharacters);
  139. }
  140. }