EReg.hx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 java.util.regex.Regex;
  23. @:coreType class EReg {
  24. private var pattern:String;
  25. private var matcher:Matcher;
  26. private var cur:String;
  27. private var sub:Int;
  28. private var isGlobal:Bool;
  29. public function new( r : String, opt : String ) {
  30. var flags = 0;
  31. for (i in 0...opt.length)
  32. {
  33. switch(StringTools.fastCodeAt(opt, i))
  34. {
  35. case 'i'.code:
  36. flags |= Pattern.CASE_INSENSITIVE;
  37. case 'm'.code:
  38. flags |= Pattern.MULTILINE;
  39. case 's'.code:
  40. flags |= Pattern.DOTALL;
  41. case 'g'.code:
  42. isGlobal = true;
  43. }
  44. }
  45. matcher = Pattern.compile(convert(r), flags).matcher("");
  46. pattern = r;
  47. }
  48. private static function convert(r:String):String
  49. {
  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. sub = 0;
  76. cur = s;
  77. matcher = matcher.reset(s);
  78. return matcher.find();
  79. }
  80. public function matched( n : Int ) : String
  81. {
  82. if (n == 0)
  83. return matcher.group();
  84. else
  85. return matcher.group(n);
  86. }
  87. public function matchedLeft() : String
  88. {
  89. return untyped cur.substring(0, sub + matcher.start());
  90. }
  91. public function matchedRight() : String
  92. {
  93. return untyped cur.substring(sub + matcher.end(), cur.length);
  94. }
  95. public function matchedPos() : { pos : Int, len : Int } {
  96. var start = matcher.start();
  97. return { pos : sub + start, len : matcher.end() - start };
  98. }
  99. public function matchSub( s : String, pos : Int, len : Int = -1):Bool {
  100. var s2 = (len < 0 ? s.substr(pos) : s.substr(pos, len));
  101. sub = pos;
  102. matcher = matcher.reset(s2);
  103. cur = s;
  104. return matcher.find();
  105. }
  106. public function split( s : String ) : Array<String>
  107. {
  108. if (isGlobal)
  109. {
  110. var ret = [];
  111. while(this.match(s))
  112. {
  113. ret.push(matchedLeft());
  114. s = matchedRight();
  115. }
  116. ret.push(s);
  117. return ret;
  118. } else {
  119. var m = matcher;
  120. m.reset(s);
  121. if (m.find())
  122. {
  123. return untyped [s.substring(0, m.start()), s.substring(m.end(), s.length)];
  124. } else {
  125. return [s];
  126. }
  127. }
  128. }
  129. inline function start(group:Int)
  130. {
  131. return matcher.start(group) + sub;
  132. }
  133. inline function len(group:Int)
  134. {
  135. return matcher.end(group) - matcher.start(group);
  136. }
  137. public function replace( s : String, by : String ) : String
  138. {
  139. var b = new StringBuf();
  140. var pos = 0;
  141. var len = s.length;
  142. var a = by.split("$");
  143. var first = true;
  144. do {
  145. if( !matchSub(s,pos,len) )
  146. break;
  147. var p = matchedPos();
  148. if( p.len == 0 && !first ) {
  149. if( p.pos == s.length )
  150. break;
  151. p.pos += 1;
  152. }
  153. b.addSub(s,pos,p.pos-pos);
  154. if( a.length > 0 )
  155. b.add(a[0]);
  156. var i = 1;
  157. while( i < a.length ) {
  158. var k = a[i];
  159. var c = k.charCodeAt(0);
  160. // 1...9
  161. if( c >= 49 && c <= 57 ) {
  162. try {
  163. var ppos = start( c-48 ), plen = this.len( c-48 );
  164. b.addSub(s, ppos, plen);
  165. }
  166. catch(e:Dynamic)
  167. {
  168. b.add("$");
  169. b.add(k);
  170. }
  171. } else if( c == null ) {
  172. b.add("$");
  173. i++;
  174. var k2 = a[i];
  175. if( k2 != null && k2.length > 0 )
  176. b.add(k2);
  177. } else
  178. b.add("$"+k);
  179. i++;
  180. }
  181. var tot = p.pos + p.len - pos;
  182. pos += tot;
  183. len -= tot;
  184. first = false;
  185. } while( isGlobal );
  186. b.addSub(s,pos,len);
  187. return b.toString();
  188. }
  189. public function map( s : String, f : EReg -> String ) : String {
  190. var offset = 0;
  191. var buf = new StringBuf();
  192. do {
  193. if (offset >= s.length)
  194. break;
  195. else if (!matchSub(s, offset)) {
  196. buf.add(s.substr(offset));
  197. break;
  198. }
  199. var p = matchedPos();
  200. buf.add(s.substr(offset, p.pos - offset));
  201. buf.add(f(this));
  202. if (p.len == 0) {
  203. buf.add(s.substr(p.pos, 1));
  204. offset = p.pos + 1;
  205. }
  206. else
  207. offset = p.pos + p.len;
  208. } while (isGlobal);
  209. if (!isGlobal && offset > 0 && offset < s.length)
  210. buf.add(s.substr(offset));
  211. return buf.toString();
  212. }
  213. }