EReg.hx 5.9 KB

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