EReg.hx 4.2 KB

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