EReg.hx 4.3 KB

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