EReg.hx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. private typedef ERegValue = hl.Abstract<"ereg">;
  23. @:access(String)
  24. @:coreApi final class EReg {
  25. var r:ERegValue;
  26. var last:String;
  27. var global:Bool;
  28. public function new(r:String, opt:String):Void {
  29. var a = opt.split("g");
  30. global = a.length > 1;
  31. if (global)
  32. opt = a.join("");
  33. this.r = regexp_new_options(r.bytes, opt.bytes);
  34. }
  35. public function match(s:String):Bool {
  36. var p = regexp_match(r, s.bytes, 0, s.length);
  37. if (p)
  38. last = s;
  39. else
  40. last = null;
  41. return p;
  42. }
  43. public function matched(n:Int):String {
  44. var len = 0;
  45. var m = regexp_matched_pos(r, n, len);
  46. return m < 0 ? null : last.substr(m, len);
  47. }
  48. public function matchedLeft():String {
  49. var p = regexp_matched_pos(r, 0, null);
  50. return last.substr(0, p);
  51. }
  52. public function matchedRight():String {
  53. var len = 0;
  54. var p = regexp_matched_pos(r, 0, len);
  55. return last.substr(p + len);
  56. }
  57. public function matchedPos():{pos:Int, len:Int} {
  58. var len = 0;
  59. var p = regexp_matched_pos(r, 0, len);
  60. if (p < 0)
  61. return null;
  62. return {pos: p, len: len};
  63. }
  64. public function matchedNum():Int {
  65. if(last == null) return 0;
  66. #if (hl_ver >= version("1.12.0"))
  67. return regexp_matched_num(r);
  68. #else
  69. var i = 0;
  70. var num = 0;
  71. try {
  72. while (true) {
  73. if (regexp_matched_pos(r, i, null) >= 0) num++;
  74. i++;
  75. }
  76. } catch (_:String) {}
  77. return num;
  78. #end
  79. }
  80. public function matchSub(s:String, pos:Int, len:Int = -1):Bool {
  81. var p = regexp_match(r, s.bytes, pos, len < 0 ? s.length - pos : len);
  82. if (p)
  83. last = s;
  84. else
  85. last = null;
  86. return p;
  87. }
  88. public function split(s:String):Array<String> {
  89. var pos = 0;
  90. var len = s.length;
  91. var a = new Array();
  92. var first = true;
  93. do {
  94. if (!regexp_match(r, s.bytes, pos, len))
  95. break;
  96. var plen = 0;
  97. var p = regexp_matched_pos(r, 0, plen);
  98. if (plen == 0 && !first) {
  99. if (p == s.length)
  100. break;
  101. p++;
  102. }
  103. a.push(s.substr(pos, p - pos));
  104. var tot = p + plen - pos;
  105. pos += tot;
  106. len -= tot;
  107. first = false;
  108. } while (global);
  109. a.push(s.substr(pos, len));
  110. return a;
  111. }
  112. public function replace(s:String, by:String):String {
  113. var b = new StringBuf();
  114. var pos = 0;
  115. var len = s.length;
  116. var a = by.split("$");
  117. var first = true;
  118. do {
  119. if (!regexp_match(r, s.bytes, pos, len))
  120. break;
  121. var plen = 0;
  122. var p = regexp_matched_pos(r, 0, plen);
  123. if (plen == 0 && !first) {
  124. if (p == s.length)
  125. break;
  126. p++;
  127. }
  128. b.addSub(s, pos, p - pos);
  129. if (a.length > 0)
  130. b.add(a[0]);
  131. var i = 1;
  132. while (i < a.length) {
  133. var k = a[i];
  134. var c = StringTools.fastCodeAt(k, 0);
  135. // 1...9
  136. if (c >= 49 && c <= 57) {
  137. var plen = 0;
  138. var p = try regexp_matched_pos(r, Std.int(c) - 48, plen) catch (e:String) -1;
  139. if (p < 0) {
  140. b.add("$");
  141. b.add(k);
  142. } else {
  143. if (p >= 0)
  144. b.addSub(s, p, plen);
  145. b.addSub(k, 1, k.length - 1);
  146. }
  147. } else if (c == 0) {
  148. b.add("$");
  149. i++;
  150. var k2 = a[i];
  151. if (k2 != null && k2.length > 0)
  152. b.add(k2);
  153. } else
  154. b.add("$" + k);
  155. i++;
  156. }
  157. var tot = p + plen - pos;
  158. pos += tot;
  159. len -= tot;
  160. first = false;
  161. } while (global);
  162. b.addSub(s, pos, len);
  163. return b.toString();
  164. }
  165. public function map(s:String, f:EReg->String):String {
  166. var offset = 0;
  167. var buf = new StringBuf();
  168. do {
  169. if (offset >= s.length)
  170. break;
  171. else if (!matchSub(s, offset)) {
  172. buf.add(s.substr(offset));
  173. break;
  174. }
  175. var plen = 0;
  176. var p = regexp_matched_pos(r, 0, plen);
  177. buf.add(s.substr(offset, p - offset));
  178. buf.add(f(this));
  179. if (plen == 0) {
  180. buf.add(s.substr(p, 1));
  181. offset = p + 1;
  182. } else
  183. offset = p + plen;
  184. } while (global);
  185. if (!global && offset > 0 && offset < s.length)
  186. buf.add(s.substr(offset));
  187. return buf.toString();
  188. }
  189. public static function escape(s:String):String {
  190. return escapeRegExpRe.map(s, function(r) return "\\" + r.matched(0));
  191. }
  192. static var escapeRegExpRe = ~/[\[\]{}()*+?.\\\^$|]/g;
  193. @:hlNative("std", "regexp_new_options") static function regexp_new_options(bytes:hl.Bytes, options:hl.Bytes):ERegValue {
  194. return null;
  195. }
  196. @:hlNative("std", "regexp_match") static function regexp_match(r:ERegValue, str:hl.Bytes, pos:Int, size:Int):Bool {
  197. return false;
  198. }
  199. @:hlNative("std", "regexp_matched_pos") static function regexp_matched_pos(r:ERegValue, n:Int, size:hl.Ref<Int>):Int {
  200. return 0;
  201. }
  202. #if (hl_ver >= version("1.12.0"))
  203. @:hlNative("std", "regexp_matched_num") static function regexp_matched_num(r:ERegValue):Int {
  204. return 0;
  205. }
  206. #end
  207. }