StringTools.hx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (C)2005-2018 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 static function startsWith( s : String, start : String ) : Bool {
  37. return start == '' || Global.strpos(s, start) == 0;
  38. }
  39. public static function endsWith( s : String, end : String ) : Bool {
  40. return end == '' || Global.substr(s, -end.length) == end;
  41. }
  42. public static function isSpace( s : String, pos : Int ) : Bool {
  43. var c = s.charCodeAt( pos );
  44. return (c >= 9 && c <= 13) || c == 32;
  45. }
  46. public inline static function ltrim( s : String ) : String {
  47. return Global.ltrim(s);
  48. }
  49. public inline static function rtrim( s : String ) : String {
  50. return Global.rtrim(s);
  51. }
  52. public inline static function trim( s : String ) : String {
  53. return Global.trim(s);
  54. }
  55. public static function rpad( s : String, c : String, l : Int ) : String {
  56. var cLength = c.length;
  57. var sLength = s.length;
  58. if (cLength == 0 || sLength >= l) return s;
  59. var padLength = l - sLength;
  60. var padCount = Syntax.int(padLength / cLength);
  61. if(padCount > 0) {
  62. var result = Global.str_pad(s, Global.strlen(s) + padCount * Global.strlen(c), c, Const.STR_PAD_RIGHT);
  63. return (padCount * cLength >= padLength) ? result : Syntax.concat(result, c);
  64. } else {
  65. return Syntax.concat(s, c);
  66. }
  67. }
  68. public static function lpad( s : String, c : String, l : Int ) : String {
  69. var cLength = c.length;
  70. var sLength = s.length;
  71. if (cLength == 0 || sLength >= l) return s;
  72. var padLength = l - sLength;
  73. var padCount = Syntax.int(padLength / cLength);
  74. if(padCount > 0) {
  75. var result = Global.str_pad(s, Global.strlen(s) + padCount * Global.strlen(c), c, Const.STR_PAD_LEFT);
  76. return (padCount * cLength >= padLength) ? result : Syntax.concat(c, result);
  77. } else {
  78. return Syntax.concat(c, s);
  79. }
  80. }
  81. public static function replace( s : String, sub : String, by : String ) : String {
  82. if (sub == '') {
  83. return Global.implode(by, Global.preg_split('//u', s, -1, Const.PREG_SPLIT_NO_EMPTY));
  84. }
  85. return Global.str_replace(sub, by, s);
  86. }
  87. public static function hex( n : Int, ?digits : Int ) : String {
  88. var s = Global.dechex(n);
  89. var len = 8;
  90. if (Global.strlen(s) > (null == digits ? len : (len = digits > len ? digits : len)))
  91. s = s.substr(-len);
  92. else if ( digits != null )
  93. s = lpad(s, '0', digits);
  94. return s.toUpperCase();
  95. }
  96. public static function fastCodeAt( s : String, index : Int ) : Int {
  97. var char:NativeString = (index == 0 ? s : Global.mb_substr(s, index, 1));
  98. if(char == '') return 0;
  99. return Boot.unsafeOrd(char);
  100. }
  101. public static inline function isEof( c : Int ) : Bool {
  102. return c == 0;
  103. }
  104. /**
  105. Returns a String that can be used as a single command line argument
  106. on Unix.
  107. The input will be quoted, or escaped if necessary.
  108. */
  109. public static function quoteUnixArg(argument:String):String {
  110. // Based on cpython's shlex.quote().
  111. // https://hg.python.org/cpython/file/a3f076d4f54f/Lib/shlex.py#l278
  112. if (argument == "")
  113. return "''";
  114. if (!~/[^a-zA-Z0-9_@%+=:,.\/-]/.match(argument))
  115. return argument;
  116. // use single quotes, and put single quotes into double quotes
  117. // the string $'b is then quoted as '$'"'"'b'
  118. return "'" + replace(argument, "'", "'\"'\"'") + "'";
  119. }
  120. /**
  121. Character codes of the characters that will be escaped by `quoteWinArg(_, true)`.
  122. */
  123. public static var winMetaCharacters = [";".code, ",".code, " ".code, "(".code, ")".code, "%".code, "!".code, "^".code, "\"".code, "<".code, ">".code, "&".code, "|".code, "\n".code, "\r".code];
  124. /**
  125. Returns a String that can be used as a single command line argument
  126. on Windows.
  127. The input will be quoted, or escaped if necessary, such that the output
  128. will be parsed as a single argument using the rule specified in
  129. http://msdn.microsoft.com/en-us/library/ms880421
  130. Examples:
  131. ```
  132. quoteWinArg("abc") == "abc";
  133. quoteWinArg("ab c") == '"ab c"';
  134. ```
  135. */
  136. public static function quoteWinArg(argument:String, escapeMetaCharacters:Bool):String {
  137. // If there is no space, tab, back-slash, or double-quotes, and it is not an empty string.
  138. if (!~/^[^ \t\\"]+$/.match(argument)) {
  139. // Based on cpython's subprocess.list2cmdline().
  140. // https://hg.python.org/cpython/file/50741316dd3a/Lib/subprocess.py#l620
  141. var result = new StringBuf();
  142. var needquote = argument.indexOf(" ") != -1 || argument.indexOf("\t") != -1 || argument == "";
  143. if (needquote)
  144. result.add('"');
  145. var bs_buf = new StringBuf();
  146. for (i in 0...argument.length) {
  147. switch (argument.charCodeAt(i)) {
  148. case "\\".code:
  149. // Don't know if we need to double yet.
  150. bs_buf.add("\\");
  151. case '"'.code:
  152. // Double backslashes.
  153. var bs = bs_buf.toString();
  154. result.add(bs);
  155. result.add(bs);
  156. bs_buf = new StringBuf();
  157. result.add('\\"');
  158. case var c:
  159. // Normal char
  160. if (bs_buf.length > 0) {
  161. result.add(bs_buf.toString());
  162. bs_buf = new StringBuf();
  163. }
  164. result.addChar(c);
  165. }
  166. }
  167. // Add remaining backslashes, if any.
  168. result.add(bs_buf.toString());
  169. if (needquote) {
  170. result.add(bs_buf.toString());
  171. result.add('"');
  172. }
  173. argument = result.toString();
  174. }
  175. if (escapeMetaCharacters) {
  176. var result = new StringBuf();
  177. for (i in 0...argument.length) {
  178. var c = argument.charCodeAt(i);
  179. if (winMetaCharacters.indexOf(c) >= 0) {
  180. result.addChar("^".code);
  181. }
  182. result.addChar(c);
  183. }
  184. return result.toString();
  185. } else {
  186. return argument;
  187. }
  188. }
  189. }