EReg.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 matchSub(s:String, pos:Int, len:Int = -1):Bool {
  63. matcher = matcher.reset(len < 0 ? s : s.substr(0, pos + len));
  64. cur = s;
  65. return matcher.find(pos);
  66. }
  67. public function split(s:String):Array<String> {
  68. if (isGlobal) {
  69. var ret = [];
  70. matcher.reset(s);
  71. matcher = matcher.useAnchoringBounds(false).useTransparentBounds(true);
  72. var copyOffset = 0;
  73. while (true) {
  74. if (!matcher.find()) {
  75. ret.push(s.substring(copyOffset, s.length));
  76. break;
  77. }
  78. ret.push(s.substring(copyOffset, matcher.start()));
  79. var nextStart = matcher.end();
  80. copyOffset = nextStart;
  81. if (nextStart == matcher.regionStart()) {
  82. nextStart++; // zero-length match - shift region one forward
  83. }
  84. if (nextStart >= s.length) {
  85. ret.push("");
  86. break;
  87. }
  88. matcher.region(nextStart, s.length);
  89. }
  90. return ret;
  91. } else {
  92. var m = matcher;
  93. m.reset(s);
  94. if (m.find()) {
  95. return untyped [s.substring(0, m.start()), s.substring(m.end(), s.length)];
  96. } else {
  97. return [s];
  98. }
  99. }
  100. }
  101. inline function start(group:Int):Int {
  102. return matcher.start(group);
  103. }
  104. inline function len(group:Int):Int {
  105. return matcher.end(group) - matcher.start(group);
  106. }
  107. public function replace(s:String, by:String):String {
  108. matcher.reset(s);
  109. by = by.replace("\\", "\\\\").replace("$$", "\\$");
  110. return isGlobal ? matcher.replaceAll(by) : matcher.replaceFirst(by);
  111. }
  112. public function map(s:String, f:EReg->String):String {
  113. var offset = 0;
  114. var buf = new StringBuf();
  115. do {
  116. if (offset >= s.length)
  117. break;
  118. else if (!matchSub(s, offset)) {
  119. buf.add(s.substr(offset));
  120. break;
  121. }
  122. var p = matchedPos();
  123. buf.add(s.substr(offset, p.pos - offset));
  124. buf.add(f(this));
  125. if (p.len == 0) {
  126. buf.add(s.substr(p.pos, 1));
  127. offset = p.pos + 1;
  128. } else
  129. offset = p.pos + p.len;
  130. } while (isGlobal);
  131. if (!isGlobal && offset > 0 && offset < s.length)
  132. buf.add(s.substr(offset));
  133. return buf.toString();
  134. }
  135. public static inline function escape(s:String):String {
  136. return Pattern.quote(s);
  137. }
  138. static function compilePattern(r:String, opt:String):CompiledPattern {
  139. var flags = 0;
  140. var isGlobal = false;
  141. for (i in 0...opt.length) {
  142. switch (StringTools.fastCodeAt(opt, i)) {
  143. case 'i'.code:
  144. flags |= Pattern.CASE_INSENSITIVE;
  145. case 'm'.code:
  146. flags |= Pattern.MULTILINE;
  147. case 's'.code:
  148. flags |= Pattern.DOTALL;
  149. case 'g'.code:
  150. isGlobal = true;
  151. }
  152. }
  153. flags |= Pattern.UNICODE_CASE;
  154. #if !android // see https://github.com/HaxeFoundation/haxe/issues/7632
  155. flags |= Pattern.UNICODE_CHARACTER_CLASS;
  156. #end
  157. return {
  158. pattern: Pattern.compile(r, flags),
  159. isGlobal: isGlobal
  160. }
  161. }
  162. }