EReg.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. import java.util.regex.*;
  23. using StringTools;
  24. @:coreApi class EReg {
  25. private var pattern:String;
  26. private var matcher:Matcher;
  27. private var cur:String;
  28. private var isGlobal:Bool;
  29. public function new(r:String, opt:String) {
  30. var flags = 0;
  31. for (i in 0...opt.length) {
  32. switch (StringTools.fastCodeAt(opt, i)) {
  33. case 'i'.code:
  34. flags |= Pattern.CASE_INSENSITIVE;
  35. case 'm'.code:
  36. flags |= Pattern.MULTILINE;
  37. case 's'.code:
  38. flags |= Pattern.DOTALL;
  39. case 'g'.code:
  40. isGlobal = true;
  41. }
  42. }
  43. flags |= Pattern.UNICODE_CASE;
  44. #if !android // see https://github.com/HaxeFoundation/haxe/issues/7632
  45. flags |= Pattern.UNICODE_CHARACTER_CLASS;
  46. #end
  47. matcher = Pattern.compile(convert(r), flags).matcher("");
  48. pattern = r;
  49. }
  50. private static function convert(r:String):String {
  51. // some references of the implementation:
  52. // http://stackoverflow.com/questions/809647/java-vs-javascript-regex-problem
  53. // http://stackoverflow.com/questions/4788413/how-to-convert-javascript-regex-to-safe-java-regex
  54. // Some necessary changes:
  55. //
  56. // \0 -> \x00
  57. // \v -> \x0b
  58. // [^] -> [\s\S]
  59. // unescaped ', " -> \', \"
  60. /* FIXME
  61. var pat = new StringBuf();
  62. var len = r.length;
  63. var i = 0;
  64. while (i < len)
  65. {
  66. var c = StringTools.fastCodeAt(r, i++);
  67. switch(c)
  68. {
  69. case '\\'.code: //escape-sequence
  70. }
  71. }
  72. */
  73. return r;
  74. }
  75. public function match(s:String):Bool {
  76. cur = s;
  77. matcher = matcher.reset(s);
  78. return matcher.find();
  79. }
  80. public function matched(n:Int):String {
  81. if (n == 0)
  82. return matcher.group();
  83. else
  84. return matcher.group(n);
  85. }
  86. public function matchedLeft():String {
  87. return untyped cur.substring(0, matcher.start());
  88. }
  89. public function matchedRight():String {
  90. return untyped cur.substring(matcher.end(), cur.length);
  91. }
  92. public function matchedPos():{pos:Int, len:Int} {
  93. var start = matcher.start();
  94. return {pos: start, len: matcher.end() - start};
  95. }
  96. public function matchSub(s:String, pos:Int, len:Int = -1):Bool {
  97. matcher = matcher.reset(len < 0 ? s : s.substr(0, pos + len));
  98. cur = s;
  99. return matcher.find(pos);
  100. }
  101. public function split(s:String):Array<String> {
  102. if (isGlobal) {
  103. var ret = [];
  104. matcher.reset(s);
  105. matcher = matcher.useAnchoringBounds(false).useTransparentBounds(true);
  106. var copyOffset = 0;
  107. while (true) {
  108. if (!matcher.find()) {
  109. ret.push(s.substring(copyOffset, s.length));
  110. break;
  111. }
  112. ret.push(s.substring(copyOffset, matcher.start()));
  113. var nextStart = matcher.end();
  114. copyOffset = nextStart;
  115. if (nextStart == matcher.regionStart()) {
  116. nextStart++; // zero-length match - shift region one forward
  117. }
  118. if (nextStart >= s.length) {
  119. ret.push("");
  120. break;
  121. }
  122. matcher.region(nextStart, s.length);
  123. }
  124. return ret;
  125. } else {
  126. var m = matcher;
  127. m.reset(s);
  128. if (m.find()) {
  129. return untyped [s.substring(0, m.start()), s.substring(m.end(), s.length)];
  130. } else {
  131. return [s];
  132. }
  133. }
  134. }
  135. inline function start(group:Int):Int {
  136. return matcher.start(group);
  137. }
  138. inline function len(group:Int):Int {
  139. return matcher.end(group) - matcher.start(group);
  140. }
  141. public function replace(s:String, by:String):String {
  142. matcher.reset(s);
  143. by = by.replace("\\", "\\\\").replace("$$", "\\$");
  144. return isGlobal ? matcher.replaceAll(by) : matcher.replaceFirst(by);
  145. }
  146. public function map(s:String, f:EReg->String):String {
  147. var offset = 0;
  148. var buf = new StringBuf();
  149. do {
  150. if (offset >= s.length)
  151. break;
  152. else if (!matchSub(s, offset)) {
  153. buf.add(s.substr(offset));
  154. break;
  155. }
  156. var p = matchedPos();
  157. buf.add(s.substr(offset, p.pos - offset));
  158. buf.add(f(this));
  159. if (p.len == 0) {
  160. buf.add(s.substr(p.pos, 1));
  161. offset = p.pos + 1;
  162. } else
  163. offset = p.pos + p.len;
  164. } while (isGlobal);
  165. if (!isGlobal && offset > 0 && offset < s.length)
  166. buf.add(s.substr(offset));
  167. return buf.toString();
  168. }
  169. public static inline function escape(s:String):String {
  170. return Pattern.quote(s);
  171. }
  172. }