EReg.hx 4.1 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 python.internal.UBuiltins;
  23. import python.lib.Re;
  24. import python.lib.Re.MatchObject;
  25. import python.lib.Re.Pattern;
  26. @:coreApi
  27. class EReg {
  28. var pattern:Regex;
  29. var matchObj:MatchObject;
  30. var global:Bool;
  31. public function new(r:String, opt:String) {
  32. global = false;
  33. var options = 0;
  34. for (i in 0...opt.length) {
  35. var c = StringTools.fastCodeAt(opt, i);
  36. if (c == "m".code)
  37. options |= Re.M;
  38. if (c == "i".code)
  39. options |= Re.I;
  40. if (c == "s".code)
  41. options |= Re.S;
  42. if (c == "u".code)
  43. options |= Re.U;
  44. if (c == "g".code)
  45. global = true;
  46. }
  47. pattern = Re.compile(r, options);
  48. }
  49. public inline function match(s:String):Bool {
  50. matchObj = Re.search(pattern, s);
  51. return matchObj != null;
  52. }
  53. public inline function matched(n:Int):String {
  54. return matchObj.group(n);
  55. }
  56. public inline function matchedLeft():String {
  57. return matchObj.string.substr(0, matchObj.start());
  58. }
  59. public inline function matchedRight():String {
  60. return matchObj.string.substr(matchObj.end());
  61. }
  62. public inline function matchedPos():{pos:Int, len:Int} {
  63. return {pos: matchObj.start(), len: matchObj.end() - matchObj.start()};
  64. }
  65. public function matchSub(s:String, pos:Int, len:Int = -1):Bool {
  66. if (len != -1) {
  67. matchObj = pattern.search(s, pos, pos + len);
  68. } else {
  69. matchObj = pattern.search(s, pos);
  70. }
  71. return this.matchObj != null;
  72. }
  73. public function split(s:String):Array<String> {
  74. return if (global) {
  75. var ret = [];
  76. var lastEnd = 0;
  77. for (x in Re.finditer(pattern, s)) {
  78. ret.push(s.substring(lastEnd, x.start()));
  79. lastEnd = x.end();
  80. }
  81. ret.push(s.substr(lastEnd));
  82. ret;
  83. } else {
  84. this.match(s);
  85. if (matchObj == null) {
  86. [s];
  87. } else {
  88. [s.substring(0, matchObj.start()), s.substr(matchObj.end())];
  89. }
  90. }
  91. }
  92. public function replace(s:String, by:String):String {
  93. var by = by.split("$$").join("_hx_#repl#__");
  94. function replace(x:MatchObject) {
  95. var res = by;
  96. var g = x.groups();
  97. for (i in 0...g.length) {
  98. var gs = g[i];
  99. if (gs == null)
  100. continue;
  101. res = res.split("$" + UBuiltins.str(i + 1)).join(gs);
  102. }
  103. res = res.split("_hx_#repl#__").join("$");
  104. return res;
  105. }
  106. return Re.sub(pattern, replace, s, global ? 0 : 1);
  107. }
  108. public function map(s:String, f:EReg->String):String {
  109. var buf = new StringBuf();
  110. var pos = 0;
  111. var right = s;
  112. var cur = this;
  113. while (pos < s.length) {
  114. if (matchObj == null) {
  115. matchObj = Re.search(pattern, s);
  116. } else {
  117. matchObj = matchObj.re.search(s, pos);
  118. }
  119. if (matchObj == null)
  120. break;
  121. var pos1 = matchObj.end();
  122. var curPos = cur.matchedPos();
  123. buf.add(cur.matchedLeft().substr(pos));
  124. buf.add(f(cur));
  125. right = cur.matchedRight();
  126. if (!global) {
  127. buf.add(right);
  128. return buf.toString();
  129. }
  130. if (curPos.len == 0) {
  131. buf.add(s.charAt(pos1));
  132. right = right.substr(1);
  133. pos = pos1 + 1;
  134. } else {
  135. pos = pos1;
  136. }
  137. }
  138. buf.add(right);
  139. return buf.toString();
  140. }
  141. public static inline function escape(s:String):String {
  142. return Re.escape(s);
  143. }
  144. }