EReg.hx 5.0 KB

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