EReg.hx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 split(s:String):Array<String> {
  66. var pos = 0;
  67. var len = s.length;
  68. var a = new Array();
  69. var first = true;
  70. do {
  71. if (!regexp_match(r, untyped s.__s, pos, len))
  72. break;
  73. var p = regexp_matched_pos(r, 0);
  74. if (p.len == 0 && !first) {
  75. if (p.pos == s.length)
  76. break;
  77. p.pos += 1;
  78. }
  79. a.push(s.substr(pos, p.pos - pos));
  80. var tot = p.pos + p.len - pos;
  81. pos += tot;
  82. len -= tot;
  83. first = false;
  84. } while (global);
  85. a.push(s.substr(pos, len));
  86. return a;
  87. }
  88. public function replace(s:String, by:String):String {
  89. var b = new StringBuf();
  90. var pos = 0;
  91. var len = s.length;
  92. var a = by.split("$");
  93. var first = true;
  94. do {
  95. if (!regexp_match(r, untyped s.__s, pos, len))
  96. break;
  97. var p = regexp_matched_pos(r, 0);
  98. if (p.len == 0 && !first) {
  99. if (p.pos == s.length)
  100. break;
  101. p.pos += 1;
  102. }
  103. b.addSub(s, pos, p.pos - pos);
  104. if (a.length > 0)
  105. b.add(a[0]);
  106. var i = 1;
  107. while (i < a.length) {
  108. var k = a[i];
  109. var c = k.charCodeAt(0);
  110. // 1...9
  111. if (c >= 49 && c <= 57) {
  112. var p = try regexp_matched_pos(r, Std.int(c) - 48) catch (e:String) null;
  113. if (p == null) {
  114. b.add("$");
  115. b.add(k);
  116. } else {
  117. if (p.pos >= 0)
  118. b.addSub(s, p.pos, p.len);
  119. b.addSub(k, 1, k.length - 1);
  120. }
  121. } else if (c == null) {
  122. b.add("$");
  123. i++;
  124. var k2 = a[i];
  125. if (k2 != null && k2.length > 0)
  126. b.add(k2);
  127. } else
  128. b.add("$" + k);
  129. i++;
  130. }
  131. var tot = p.pos + p.len - pos;
  132. pos += tot;
  133. len -= tot;
  134. first = false;
  135. } while (global);
  136. b.addSub(s, pos, len);
  137. return b.toString();
  138. }
  139. // public function map( s : String, f : EReg -> String ) : String {
  140. // var b = new StringBuf();
  141. // var pos = 0;
  142. // var len = s.length;
  143. // var first = true;
  144. // last = s;
  145. // do {
  146. // if( !regexp_match(r,untyped s.__s,pos,len) )
  147. // break;
  148. // var p = regexp_matched_pos(r,0);
  149. // if( p.len == 0 && !first ) {
  150. // if( p.pos == s.length )
  151. // break;
  152. // p.pos += 1;
  153. // }
  154. // b.addSub(s,pos,p.pos-pos);
  155. // b.add(f(this));
  156. // var tot = p.pos + p.len - pos;
  157. // pos += tot;
  158. // len -= tot;
  159. // first = false;
  160. // } while( global );
  161. // b.addSub(s,pos,len);
  162. // return b.toString();
  163. // }
  164. public function map(s:String, f:EReg->String):String {
  165. var offset = 0;
  166. var buf = new StringBuf();
  167. do {
  168. if (offset >= s.length)
  169. break;
  170. else if (!matchSub(s, offset)) {
  171. buf.add(s.substr(offset));
  172. break;
  173. }
  174. var p = regexp_matched_pos(r, 0);
  175. buf.add(s.substr(offset, p.pos - offset));
  176. buf.add(f(this));
  177. if (p.len == 0) {
  178. buf.add(s.substr(p.pos, 1));
  179. offset = p.pos + 1;
  180. } else
  181. offset = p.pos + p.len;
  182. } while (global);
  183. if (!global && offset > 0 && offset < s.length)
  184. buf.add(s.substr(offset));
  185. return buf.toString();
  186. }
  187. public static function escape(s:String):String {
  188. return escapeRegExpRe.map(s, function(r) return "\\" + r.matched(0));
  189. }
  190. static var escapeRegExpRe = ~/[\[\]{}()*+?.\\\^$|]/g;
  191. static var regexp_new_options = neko.Lib.load("regexp", "regexp_new_options", 2);
  192. static var regexp_match = neko.Lib.load("regexp", "regexp_match", 4);
  193. static var regexp_matched = neko.Lib.load("regexp", "regexp_matched", 2);
  194. static var regexp_matched_pos:Dynamic->Int->{pos: Int, len: Int} = neko.Lib.load("regexp", "regexp_matched_pos", 2);
  195. }