EReg.hx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import python.lib.Builtin;
  2. import python.lib.Re;
  3. import python.lib.Re.MatchObject;
  4. import python.lib.Re.Pattern;
  5. class EReg {
  6. /**
  7. Creates a new regular expression with pattern `r` and modifiers `opt`.
  8. This is equivalent to the shorthand syntax `~/r/opt`
  9. If `r` or `opt` are null, the result is unspecified.
  10. **/
  11. var pattern:Regex;
  12. var matchObj:MatchObject;
  13. var global:Bool;
  14. public function new( r : String, opt : String ) {
  15. global = false;
  16. var options = 0;
  17. for (i in 0...opt.length) {
  18. var c = StringTools.fastCodeAt(opt, i);
  19. if (c == "m".code) options |= Re.M;
  20. if (c == "i".code) options |= Re.I;
  21. if (c == "s".code) options |= Re.S;
  22. if (c == "u".code) options |= Re.U;
  23. if (c == "g".code) global = true;
  24. }
  25. pattern = Re.compile(r, options);
  26. }
  27. /**
  28. Tells if `this` regular expression matches String `s`.
  29. This method modifies the internal state.
  30. If `s` is `null`, the result is unspecified.
  31. **/
  32. public function match( s : String ) : Bool {
  33. matchObj = Re.search(pattern, s);
  34. return matchObj != null;
  35. }
  36. /**
  37. Returns the matched sub-group `n` of `this` EReg.
  38. This method should only be called after `this.match` or
  39. `this.matchSub`, and then operates on the String of that operation.
  40. The index `n` corresponds to the n-th set of parentheses in the pattern
  41. of `this` EReg. If no such sub-group exists, an exception is thrown.
  42. If `n` equals 0, the whole matched substring is returned.
  43. **/
  44. public function matched( n : Int ) : String {
  45. return matchObj.group(n);
  46. }
  47. /**
  48. Returns the part to the left of the last matched substring.
  49. If the most recent call to `this.match` or `this.matchSub` did not
  50. match anything, the result is unspecified.
  51. If the global g modifier was in place for the matching, only the
  52. substring to the left of the leftmost match is returned.
  53. The result does not include the matched part.
  54. **/
  55. public function matchedLeft() : String {
  56. return matchObj.string.substr(0, matchObj.start());
  57. }
  58. /**
  59. Returns the part to the right of the last matched substring.
  60. If the most recent call to `this.match` or `this.matchSub` did not
  61. match anything, the result is unspecified.
  62. If the global g modifier was in place for the matching, only the
  63. substring to the right of the leftmost match is returned.
  64. The result does not include the matched part.
  65. **/
  66. public function matchedRight() : String {
  67. return matchObj.string.substr(matchObj.end());
  68. }
  69. /**
  70. Returns the position and length of the last matched substring, within
  71. the String which was last used as argument to `this.match` or
  72. `this.matchSub`.
  73. If the most recent call to `this.match` or `this.matchSub` did not
  74. match anything, the result is unspecified.
  75. If the global g modifier was in place for the matching, the position and
  76. length of the leftmost substring is returned.
  77. **/
  78. public function matchedPos() : { pos : Int, len : Int } {
  79. return { pos : matchObj.start(), len : matchObj.end() - matchObj.start() };
  80. }
  81. /**
  82. Tells if `this` regular expression matches a substring of String `s`.
  83. This function expects `pos` and `len` to describe a valid substring of
  84. `s`, or else the result is unspecified. To get more robust behavior,
  85. `this.matchSub(s.substr(pos,len))` can be used instead.
  86. This method modifies the internal state.
  87. If `s` is null, the result is unspecified.
  88. **/
  89. public function matchSub( s : String, pos : Int, ?len : Int):Bool {
  90. if (len != null) {
  91. matchObj = pattern.search(s, pos, pos+len);
  92. } else {
  93. matchObj = pattern.search(s, pos);
  94. }
  95. return this.matchObj != null;
  96. }
  97. /**
  98. Splits String `s` at all substrings `this` EReg matches.
  99. If a match is found at the start of `s`, the result contains a leading
  100. empty String "" entry.
  101. If a match is found at the end of `s`, the result contains a trailing
  102. empty String "" entry.
  103. If two matching substrings appear next to each other, the result
  104. contains the empty String "" between them.
  105. By default, this method splits `s` into two parts at the first matched
  106. substring. If the global g modifier is in place, `s` is split at each
  107. matched substring.
  108. If `s` is null, the result is unspecified.
  109. **/
  110. public function split( s : String ) : Array<String> {
  111. return if (global) {
  112. var ret = [];
  113. var lastEnd = 0;
  114. for (x in Re.finditer(pattern, s)) {
  115. ret.push(s.substring(lastEnd, x.start() ));
  116. lastEnd = x.end();
  117. }
  118. ret.push(s.substr(lastEnd));
  119. return ret;
  120. } else {
  121. this.match(s);
  122. if (matchObj == null) {
  123. return [s];
  124. } else {
  125. return [ s.substring(0, matchObj.start()), s.substr(matchObj.end()) ];
  126. }
  127. }
  128. }
  129. /**
  130. Replaces the first substring of `s` which `this` EReg matches with `by`.
  131. If `this` EReg does not match any substring, the result is `s`.
  132. By default, this method replaces only the first matched substring. If
  133. the global g modifier is in place, all matched substrings are replaced.
  134. If `by` contains `$1` to `$9`, the digit corresponds to number of a
  135. matched sub-group and its value is used instead. If no such sub-group
  136. exists, the replacement is unspecified. The string `$$` becomes `$`.
  137. If `s` or `by` are null, the result is unspecified.
  138. **/
  139. public function replace( s : String, by : String ) : String
  140. {
  141. var by = by.split("$$").join("_hx_#repl#__");
  142. function replace (x:MatchObject) {
  143. var res = by;
  144. var g = x.groups();
  145. for (i in 0...g.length) {
  146. res = res.split("$"+Builtin.str(i+1)).join(g[i]);
  147. }
  148. res = res.split("_hx_#repl#__").join("$");
  149. return res;
  150. }
  151. return Re.sub(pattern, replace, s, global ? 0 : 1 );
  152. }
  153. /**
  154. For each occurence of the pattern in the string `s`, the function `f` is called and
  155. can return the string that needs to be replaced. All occurences are matched anyway,
  156. and setting the `g` flag might cause some incorrect behavior on some platforms.
  157. **/
  158. public function map( s : String, f : EReg -> String ) : String {
  159. var buf = new StringBuf();
  160. var pos = 0;
  161. var right = s;
  162. var cur = this;
  163. while( pos < s.length ) {
  164. if (matchObj == null) {
  165. matchObj = Re.search(pattern, s);
  166. } else {
  167. matchObj = matchObj.re.search(s, pos);
  168. }
  169. if( matchObj == null )
  170. break;
  171. var pos1 = matchObj.end();
  172. var curPos = cur.matchedPos();
  173. buf.add(cur.matchedLeft().substr(pos));
  174. buf.add(f(cur));
  175. right = cur.matchedRight();
  176. if (!global) {
  177. buf.add(right);
  178. return buf.toString();
  179. }
  180. if (curPos.len == 0) {
  181. buf.add(s.charAt(pos1));
  182. right = right.substr(1);
  183. pos = pos1+1;
  184. } else {
  185. pos = pos1;
  186. }
  187. }
  188. buf.add(right);
  189. return buf.toString();
  190. }
  191. }