EReg.hx 5.1 KB

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