EReg.hx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 split(s:String):Array<String> {
  67. var pos = 0;
  68. var len = s.length;
  69. var a = new Array();
  70. var first = true;
  71. do {
  72. if (!_hx_regexp_match(r, s, pos, len))
  73. break;
  74. var p = _hx_regexp_matched_pos(r, 0);
  75. if (p.len == 0 && !first) {
  76. if (p.pos == s.length)
  77. break;
  78. p.pos += 1;
  79. }
  80. a.push(s.substr(pos, p.pos - pos));
  81. var tot = p.pos + p.len - pos;
  82. pos += tot;
  83. len -= tot;
  84. first = false;
  85. } while (global);
  86. a.push(s.substr(pos, len));
  87. return a;
  88. }
  89. public function replace(s:String, by:String):String {
  90. var b = new StringBuf();
  91. var pos = 0;
  92. var len = s.length;
  93. var a = by.split("$");
  94. var first = true;
  95. do {
  96. if (!_hx_regexp_match(r, s, pos, len))
  97. break;
  98. var p = _hx_regexp_matched_pos(r, 0);
  99. if (p.len == 0 && !first) {
  100. if (p.pos == s.length)
  101. break;
  102. p.pos += 1;
  103. }
  104. b.addSub(s, pos, p.pos - pos);
  105. if (a.length > 0)
  106. b.add(a[0]);
  107. var i = 1;
  108. while (i < a.length) {
  109. var k = a[i];
  110. var c = k.charCodeAt(0);
  111. // 1...9
  112. if (c >= 49 && c <= 57) {
  113. var p = try _hx_regexp_matched_pos(r, Std.int(c) - 48) catch (e:String) null;
  114. if (p == null) {
  115. b.add("$");
  116. b.add(k);
  117. } else {
  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 offset = 0;
  141. var buf = new StringBuf();
  142. do {
  143. if (offset >= s.length)
  144. break;
  145. else if (!matchSub(s, offset)) {
  146. buf.add(s.substr(offset));
  147. break;
  148. }
  149. var p = _hx_regexp_matched_pos(r, 0);
  150. buf.add(s.substr(offset, p.pos - offset));
  151. buf.add(f(this));
  152. if (p.len == 0) {
  153. buf.add(s.substr(p.pos, 1));
  154. offset = p.pos + 1;
  155. } else
  156. offset = p.pos + p.len;
  157. } while (global);
  158. if (!global && offset > 0 && offset < s.length)
  159. buf.add(s.substr(offset));
  160. return buf.toString();
  161. }
  162. public static function escape(s:String):String {
  163. return escapeRegExpRe.map(s, function(r) return "\\" + r.matched(0));
  164. }
  165. static var escapeRegExpRe = ~/[\[\]{}()*+?.\\\^$|]/g;
  166. function toString():String
  167. return 'EReg($r)';
  168. @:native("_hx_regexp_new_options")
  169. extern static function _hx_regexp_new_options(s:String, options:String):Dynamic;
  170. @:native("_hx_regexp_match")
  171. extern static function _hx_regexp_match(handler:Dynamic, string:String, pos:Int, len:Int):Bool;
  172. @:native("_hx_regexp_matched")
  173. extern static function _hx_regexp_matched(handle:Dynamic, pos:Int):String;
  174. @:native("_hx_regexp_matched_pos")
  175. extern static function _hx_regexp_matched_pos(handle:Dynamic, match:Int):{pos:Int, len:Int};
  176. }