EReg.hx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. private typedef ERegValue = hl.Abstract<"ereg">;
  23. @:access(String)
  24. @:coreApi @:final class EReg {
  25. var r : ERegValue;
  26. var last : String;
  27. var global : Bool;
  28. public function new( r : String, opt : String ) : Void {
  29. var a = opt.split("g");
  30. global = a.length > 1;
  31. if( global )
  32. opt = a.join("");
  33. this.r = regexp_new_options(r.bytes, opt.bytes);
  34. }
  35. public function match( s : String ) : Bool {
  36. var p = regexp_match(r,s.bytes,0,s.length);
  37. if( p )
  38. last = s;
  39. else
  40. last = null;
  41. return p;
  42. }
  43. public function matched( n : Int ) : String {
  44. var len = 0;
  45. var m = regexp_matched_pos(r,n,len);
  46. return m < 0 ? null : last.substr(m, len);
  47. }
  48. public function matchedLeft() : String {
  49. var p = regexp_matched_pos(r,0,null);
  50. return last.substr(0,p);
  51. }
  52. public function matchedRight() : String {
  53. var len = 0;
  54. var p = regexp_matched_pos(r,0,len);
  55. return last.substr(p + len);
  56. }
  57. public function matchedPos() : { pos : Int, len : Int } {
  58. var len = 0;
  59. var p = regexp_matched_pos(r, 0, len);
  60. if( p < 0 ) return null;
  61. return { pos : p, len : len };
  62. }
  63. public function matchSub( s : String, pos : Int, len : Int = -1):Bool {
  64. var p = regexp_match(r, s.bytes, pos, len < 0 ? s.length - pos : len);
  65. if( p )
  66. last = s;
  67. else
  68. last = null;
  69. return p;
  70. }
  71. public function split( s : String ) : Array<String> {
  72. var pos = 0;
  73. var len = s.length;
  74. var a = new Array();
  75. var first = true;
  76. do {
  77. if( !regexp_match(r,s.bytes,pos,len) )
  78. break;
  79. var plen = 0;
  80. var p = regexp_matched_pos(r,0,plen);
  81. if( plen == 0 && !first ) {
  82. if( p == s.length )
  83. break;
  84. p++;
  85. }
  86. a.push(s.substr(pos,p - pos));
  87. var tot = p + plen - pos;
  88. pos += tot;
  89. len -= tot;
  90. first = false;
  91. } while( global );
  92. a.push(s.substr(pos,len));
  93. return a;
  94. }
  95. public function replace( s : String, by : String ) : String {
  96. var b = new StringBuf();
  97. var pos = 0;
  98. var len = s.length;
  99. var a = by.split("$");
  100. var first = true;
  101. do {
  102. if( !regexp_match(r,s.bytes,pos,len) )
  103. break;
  104. var plen = 0;
  105. var p = regexp_matched_pos(r,0, plen);
  106. if( plen == 0 && !first ) {
  107. if( p == s.length )
  108. break;
  109. p++;
  110. }
  111. b.addSub(s,pos,p-pos);
  112. if( a.length > 0 )
  113. b.add(a[0]);
  114. var i = 1;
  115. while( i < a.length ) {
  116. var k = a[i];
  117. var c = StringTools.fastCodeAt(k, 0);
  118. // 1...9
  119. if( c >= 49 && c <= 57 ) {
  120. var plen = 0;
  121. var p = try regexp_matched_pos(r,Std.int(c)-48,plen) catch( e : String ) -1;
  122. if( p < 0 ){
  123. b.add("$");
  124. b.add(k);
  125. } else {
  126. if( p >= 0 ) b.addSub(s,p,plen);
  127. b.addSub(k,1,k.length - 1);
  128. }
  129. } else if( c == 0 ) {
  130. b.add("$");
  131. i++;
  132. var k2 = a[i];
  133. if( k2 != null && k2.length > 0 )
  134. b.add(k2);
  135. } else
  136. b.add("$"+k);
  137. i++;
  138. }
  139. var tot = p + plen - pos;
  140. pos += tot;
  141. len -= tot;
  142. first = false;
  143. } while( global );
  144. b.addSub(s,pos,len);
  145. return b.toString();
  146. }
  147. public function map( s : String, f : EReg -> String ) : String {
  148. var offset = 0;
  149. var buf = new StringBuf();
  150. do {
  151. if (offset >= s.length)
  152. break;
  153. else if (!matchSub(s, offset)) {
  154. buf.add(s.substr(offset));
  155. break;
  156. }
  157. var plen = 0;
  158. var p = regexp_matched_pos(r,0,plen);
  159. buf.add(s.substr(offset, p - offset));
  160. buf.add(f(this));
  161. if (plen == 0) {
  162. buf.add(s.substr(p, 1));
  163. offset = p + 1;
  164. }
  165. else
  166. offset = p + plen;
  167. } while (global);
  168. if (!global && offset > 0 && offset < s.length)
  169. buf.add(s.substr(offset));
  170. return buf.toString();
  171. }
  172. public static function escape( s : String ) : String {
  173. return escapeRegExpRe.map(s, function(r) return "\\" + r.matched(0));
  174. }
  175. static var escapeRegExpRe = ~/[\[\]{}()*+?.\\\^$|]/g;
  176. @:hlNative("std", "regexp_new_options") static function regexp_new_options( bytes : hl.Bytes, options : hl.Bytes ) : ERegValue {
  177. return null;
  178. }
  179. @:hlNative("std", "regexp_match") static function regexp_match( r : ERegValue, str : hl.Bytes, pos : Int, size : Int ) : Bool {
  180. return false;
  181. }
  182. @:hlNative("std", "regexp_matched_pos") static function regexp_matched_pos( r : ERegValue, n : Int, size : hl.Ref<Int> ) : Int {
  183. return 0;
  184. }
  185. }