EReg.hx 7.6 KB

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