EReg.hx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 cs.system.text.regularExpressions.Regex;
  23. class EReg {
  24. private var regex : Regex;
  25. private var m : Match;
  26. private var isGlobal : Bool;
  27. private var cur : String;
  28. private var sub : Int;
  29. public function new( r : String, opt : String ) : Void {
  30. var opts:Int = cast CultureInvariant;
  31. for (i in 0...opt.length) untyped {
  32. switch(cast(opt[i], Int))
  33. {
  34. case 'i'.code:
  35. opts |= cast(IgnoreCase, Int);
  36. case 'g'.code:
  37. isGlobal = true;
  38. case 'm'.code:
  39. opts |= cast(Multiline, Int);
  40. case 'c'.code:
  41. opts |= cast(Compiled, Int);
  42. }
  43. }
  44. this.regex = new Regex(r, cast(opts, RegexOptions));
  45. }
  46. public function match( s : String ) : Bool {
  47. sub = 0;
  48. m = regex.Match(s);
  49. cur = s;
  50. return m.Success;
  51. }
  52. public function matched( n : Int ) : String {
  53. if (m == null || cast(n, UInt) > m.Groups.Count)
  54. throw "EReg::matched";
  55. return m.Groups[n].Value;
  56. }
  57. public function matchedLeft() : String {
  58. return untyped cur.Substring(0, sub + m.Index);
  59. }
  60. public function matchedRight() : String {
  61. return untyped cur.Substring(sub + m.Index + m.Length);
  62. }
  63. public function matchedPos() : { pos : Int, len : Int } {
  64. return { pos : sub + m.Index, len : m.Length };
  65. }
  66. public function matchSub( s : String, pos : Int, len : Int = -1):Bool {
  67. var s2 = (len < 0 ? s.substr(pos) : s.substr(pos, len));
  68. sub = pos;
  69. m = regex.Match(s2);
  70. cur = s;
  71. return m.Success;
  72. }
  73. public function split( s : String ) : Array<String> {
  74. if (isGlobal)
  75. return cs.Lib.array(regex.Split(s));
  76. var m = regex.Match(s);
  77. return untyped [s.Substring(0, m.Index), s.Substring(m.Index + m.Length)];
  78. }
  79. inline function start(group:Int)
  80. {
  81. return m.Groups[group].Index + sub;
  82. }
  83. inline function len(group:Int)
  84. {
  85. return m.Groups[group].Length;
  86. }
  87. public function replace( s : String, by : String ) : String
  88. {
  89. var b = new StringBuf();
  90. var pos = 0;
  91. var len = s.length;
  92. var a = by.split("$");
  93. var first = true;
  94. do {
  95. if( !matchSub(s,pos,len) )
  96. break;
  97. var p = matchedPos();
  98. if( p.len == 0 && !first ) {
  99. if( p.pos == s.length )
  100. break;
  101. p.pos += 1;
  102. }
  103. b.addSub(s,pos,p.pos-pos);
  104. if( a.length > 0 )
  105. b.add(a[0]);
  106. var i = 1;
  107. while( i < a.length ) {
  108. var k = a[i];
  109. var c = k.charCodeAt(0);
  110. // 1...9
  111. if( c >= 49 && c <= 57 ) {
  112. try {
  113. var ppos = start( c-48 ), plen = this.len( c-48 );
  114. b.addSub(s, ppos, plen);
  115. }
  116. catch(e:Dynamic)
  117. {
  118. b.add("$");
  119. b.add(k);
  120. }
  121. } else if( c == null ) {
  122. b.add("$");
  123. i++;
  124. var k2 = a[i];
  125. if( k2 != null && k2.length > 0 )
  126. b.add(k2);
  127. } else
  128. b.add("$"+k);
  129. i++;
  130. }
  131. var tot = p.pos + p.len - pos;
  132. pos += tot;
  133. len -= tot;
  134. first = false;
  135. } while( isGlobal );
  136. b.addSub(s,pos,len);
  137. return b.toString();
  138. }
  139. public function map( s : String, f : EReg -> String ) : String {
  140. var offset = 0;
  141. var buf = new StringBuf();
  142. do {
  143. if (offset >= s.length)
  144. break;
  145. else if (!matchSub(s, offset)) {
  146. buf.add(s.substr(offset));
  147. break;
  148. }
  149. var p = matchedPos();
  150. buf.add(s.substr(offset, p.pos - offset));
  151. buf.add(f(this));
  152. if (p.len == 0) {
  153. buf.add(s.substr(p.pos, 1));
  154. offset = p.pos + 1;
  155. }
  156. else
  157. offset = p.pos + p.len;
  158. } while (isGlobal);
  159. if (!isGlobal && offset > 0 && offset < s.length)
  160. buf.add(s.substr(offset));
  161. return buf.toString();
  162. }
  163. }