EReg.hx 4.5 KB

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