StringTools.hx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (C)2005-2017 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. /**
  82. Returns a String that can be used as a single command line argument
  83. on Unix.
  84. The input will be quoted, or escaped if necessary.
  85. */
  86. public static function quoteUnixArg(argument:String):String {
  87. // Based on cpython's shlex.quote().
  88. // https://hg.python.org/cpython/file/a3f076d4f54f/Lib/shlex.py#l278
  89. if (argument == "")
  90. return "''";
  91. if (!~/[^a-zA-Z0-9_@%+=:,.\/-]/.match(argument))
  92. return argument;
  93. // use single quotes, and put single quotes into double quotes
  94. // the string $'b is then quoted as '$'"'"'b'
  95. return "'" + replace(argument, "'", "'\"'\"'") + "'";
  96. }
  97. /**
  98. Character codes of the characters that will be escaped by `quoteWinArg(_, true)`.
  99. */
  100. public static var winMetaCharacters = [";".code, ",".code, " ".code, "(".code, ")".code, "%".code, "!".code, "^".code, "\"".code, "<".code, ">".code, "&".code, "|".code, "\n".code, "\r".code];
  101. /**
  102. Returns a String that can be used as a single command line argument
  103. on Windows.
  104. The input will be quoted, or escaped if necessary, such that the output
  105. will be parsed as a single argument using the rule specified in
  106. http://msdn.microsoft.com/en-us/library/ms880421
  107. Examples:
  108. ```
  109. quoteWinArg("abc") == "abc";
  110. quoteWinArg("ab c") == '"ab c"';
  111. ```
  112. */
  113. public static function quoteWinArg(argument:String, escapeMetaCharacters:Bool):String {
  114. // If there is no space, tab, back-slash, or double-quotes, and it is not an empty string.
  115. if (!~/^[^ \t\\"]+$/.match(argument)) {
  116. // Based on cpython's subprocess.list2cmdline().
  117. // https://hg.python.org/cpython/file/50741316dd3a/Lib/subprocess.py#l620
  118. var result = new StringBuf();
  119. var needquote = argument.indexOf(" ") != -1 || argument.indexOf("\t") != -1 || argument == "";
  120. if (needquote)
  121. result.add('"');
  122. var bs_buf = new StringBuf();
  123. for (i in 0...argument.length) {
  124. switch (argument.charCodeAt(i)) {
  125. case "\\".code:
  126. // Don't know if we need to double yet.
  127. bs_buf.add("\\");
  128. case '"'.code:
  129. // Double backslashes.
  130. var bs = bs_buf.toString();
  131. result.add(bs);
  132. result.add(bs);
  133. bs_buf = new StringBuf();
  134. result.add('\\"');
  135. case c:
  136. // Normal char
  137. if (bs_buf.length > 0) {
  138. result.add(bs_buf.toString());
  139. bs_buf = new StringBuf();
  140. }
  141. result.addChar(c);
  142. }
  143. }
  144. // Add remaining backslashes, if any.
  145. result.add(bs_buf.toString());
  146. if (needquote) {
  147. result.add(bs_buf.toString());
  148. result.add('"');
  149. }
  150. argument = result.toString();
  151. }
  152. if (escapeMetaCharacters) {
  153. var result = new StringBuf();
  154. for (i in 0...argument.length) {
  155. var c = argument.charCodeAt(i);
  156. if (winMetaCharacters.indexOf(c) >= 0) {
  157. result.addChar("^".code);
  158. }
  159. result.addChar(c);
  160. }
  161. return result.toString();
  162. } else {
  163. return argument;
  164. }
  165. }
  166. }