EReg.hx 5.3 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. @:buildXml('<include name="${HXCPP}/src/hx/libs/regexp/Build.xml"/>')
  23. @:coreApi class EReg {
  24. var r:Dynamic;
  25. var last:String;
  26. var global:Bool;
  27. public function new(r:String, opt:String):Void {
  28. var a = opt.split("g");
  29. global = a.length > 1;
  30. if (global)
  31. opt = a.join("");
  32. this.r = _hx_regexp_new_options(r, opt);
  33. }
  34. public function match(s:String):Bool {
  35. var p = _hx_regexp_match(r, s, 0, s.length);
  36. if (p)
  37. last = s;
  38. else
  39. last = null;
  40. return p;
  41. }
  42. public function matched(n:Int):String {
  43. var m = _hx_regexp_matched(r, n);
  44. return m;
  45. }
  46. public function matchedLeft():String {
  47. var p = _hx_regexp_matched_pos(r, 0);
  48. return last.substr(0, p.pos);
  49. }
  50. public function matchedRight():String {
  51. var p = _hx_regexp_matched_pos(r, 0);
  52. var sz = p.pos + p.len;
  53. return last.substr(sz, last.length - sz);
  54. }
  55. public function matchedPos():{pos:Int, len:Int} {
  56. return _hx_regexp_matched_pos(r, 0);
  57. }
  58. public function matchSub(s:String, pos:Int, len:Int = -1):Bool {
  59. var p = _hx_regexp_match(r, s, pos, len < 0 ? s.length - pos : len);
  60. if (p)
  61. last = s;
  62. else
  63. last = null;
  64. return p;
  65. }
  66. public function matchedNum():Int {
  67. var num = _hx_regexp_matched_num(r);
  68. if (num == -1) return 0;
  69. return num;
  70. }
  71. public function split(s:String):Array<String> {
  72. var pos = 0;
  73. var len = s.length;
  74. var a = new Array();
  75. var first = true;
  76. do {
  77. if (!_hx_regexp_match(r, s, pos, len))
  78. break;
  79. var p = _hx_regexp_matched_pos(r, 0);
  80. if (p.len == 0 && !first) {
  81. if (p.pos == s.length)
  82. break;
  83. p.pos += 1;
  84. }
  85. a.push(s.substr(pos, p.pos - pos));
  86. var tot = p.pos + p.len - pos;
  87. pos += tot;
  88. len -= tot;
  89. first = false;
  90. } while (global);
  91. a.push(s.substr(pos, len));
  92. return a;
  93. }
  94. public function replace(s:String, by:String):String {
  95. var b = new StringBuf();
  96. var pos = 0;
  97. var len = s.length;
  98. var a = by.split("$");
  99. var first = true;
  100. do {
  101. if (!_hx_regexp_match(r, s, pos, len))
  102. break;
  103. var p = _hx_regexp_matched_pos(r, 0);
  104. if (p.len == 0 && !first) {
  105. if (p.pos == s.length)
  106. break;
  107. p.pos += 1;
  108. }
  109. b.addSub(s, pos, p.pos - pos);
  110. if (a.length > 0)
  111. b.add(a[0]);
  112. var i = 1;
  113. while (i < a.length) {
  114. var k = a[i];
  115. var c = k.charCodeAt(0);
  116. // 1...9
  117. if (c >= 49 && c <= 57) {
  118. var p = try _hx_regexp_matched_pos(r, Std.int(c) - 48) catch (e:String) null;
  119. if (p == null) {
  120. b.add("$");
  121. b.add(k);
  122. } else {
  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 offset = 0;
  146. var buf = new StringBuf();
  147. do {
  148. if (offset >= s.length)
  149. break;
  150. else if (!matchSub(s, offset)) {
  151. buf.add(s.substr(offset));
  152. break;
  153. }
  154. var p = _hx_regexp_matched_pos(r, 0);
  155. buf.add(s.substr(offset, p.pos - offset));
  156. buf.add(f(this));
  157. if (p.len == 0) {
  158. buf.add(s.substr(p.pos, 1));
  159. offset = p.pos + 1;
  160. } else
  161. offset = p.pos + p.len;
  162. } while (global);
  163. if (!global && offset > 0 && offset < s.length)
  164. buf.add(s.substr(offset));
  165. return buf.toString();
  166. }
  167. public static function escape(s:String):String {
  168. return escapeRegExpRe.map(s, function(r) return "\\" + r.matched(0));
  169. }
  170. static var escapeRegExpRe = ~/[\[\]{}()*+?.\\\^$|]/g;
  171. function toString():String
  172. return 'EReg($r)';
  173. @:native("_hx_regexp_new_options")
  174. extern static function _hx_regexp_new_options(s:String, options:String):Dynamic;
  175. @:native("_hx_regexp_match")
  176. extern static function _hx_regexp_match(handler:Dynamic, string:String, pos:Int, len:Int):Bool;
  177. @:native("_hx_regexp_matched")
  178. extern static function _hx_regexp_matched(handle:Dynamic, pos:Int):String;
  179. @:native("_hx_regexp_matched_pos")
  180. extern static function _hx_regexp_matched_pos(handle:Dynamic, match:Int):{pos:Int, len:Int};
  181. @:native("_hx_regexp_matched_num")
  182. extern static function _hx_regexp_matched_num(handle:Dynamic):Int;
  183. }