EReg.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (C)2005-2015 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. @:coreApi class EReg {
  23. var r : Dynamic;
  24. var last : String;
  25. var global : Bool;
  26. public function new( r : String, opt : String ) : Void {
  27. var a = opt.split("g");
  28. global = a.length > 1;
  29. if( global )
  30. opt = a.join("");
  31. this.r = regexp_new_options(r, opt);
  32. }
  33. public function match( s : String ) : Bool {
  34. var p = regexp_match(r,s,0,s.length);
  35. if( p )
  36. last = s;
  37. else
  38. last = null;
  39. return p;
  40. }
  41. public function matched( n : Int ) : String {
  42. var m = regexp_matched(r,n);
  43. return m;
  44. }
  45. public function matchedLeft() : String {
  46. var p = regexp_matched_pos(r,0);
  47. return last.substr(0,p.pos);
  48. }
  49. public function matchedRight() : String {
  50. var p = regexp_matched_pos(r,0);
  51. var sz = p.pos+p.len;
  52. return last.substr(sz,last.length-sz);
  53. }
  54. public function matchedPos() : { pos : Int, len : Int } {
  55. return regexp_matched_pos(r,0);
  56. }
  57. public function matchSub( s : String, pos : Int, len : Int = -1):Bool {
  58. var p = regexp_match(r, s, pos, len < 0 ? s.length - pos : len);
  59. if (p)
  60. last = s;
  61. else
  62. last = null;
  63. return p;
  64. }
  65. public function split( s : String ) : Array<String> {
  66. var pos = 0;
  67. var len = s.length;
  68. var a = new Array();
  69. var first = true;
  70. do {
  71. if( !regexp_match(r,s,pos,len) )
  72. break;
  73. var p = regexp_matched_pos(r,0);
  74. if( p.len == 0 && !first ) {
  75. if( p.pos == s.length )
  76. break;
  77. p.pos += 1;
  78. }
  79. a.push(s.substr(pos,p.pos - pos));
  80. var tot = p.pos + p.len - pos;
  81. pos += tot;
  82. len -= tot;
  83. first = false;
  84. } while( global );
  85. a.push(s.substr(pos,len));
  86. return a;
  87. }
  88. public function replace( s : String, by : String ) : String {
  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( !regexp_match(r,s,pos,len) )
  96. break;
  97. var p = regexp_matched_pos(r,0);
  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. var p = try regexp_matched_pos(r,Std.int(c)-48) catch( e : String ) null;
  113. if( p == null ){
  114. b.add("$");
  115. b.add(k);
  116. }else{
  117. b.addSub(s,p.pos,p.len);
  118. b.addSub(k,1,k.length - 1);
  119. }
  120. } else if( c == null ) {
  121. b.add("$");
  122. i++;
  123. var k2 = a[i];
  124. if( k2 != null && k2.length > 0 )
  125. b.add(k2);
  126. } else
  127. b.add("$"+k);
  128. i++;
  129. }
  130. var tot = p.pos + p.len - pos;
  131. pos += tot;
  132. len -= tot;
  133. first = false;
  134. } while( global );
  135. b.addSub(s,pos,len);
  136. return b.toString();
  137. }
  138. public function map( s : String, f : EReg -> String ) : String {
  139. var offset = 0;
  140. var buf = new StringBuf();
  141. do {
  142. if (offset >= s.length)
  143. break;
  144. else if (!matchSub(s, offset)) {
  145. buf.add(s.substr(offset));
  146. break;
  147. }
  148. var p = regexp_matched_pos(r,0);
  149. buf.add(s.substr(offset, p.pos - offset));
  150. buf.add(f(this));
  151. if (p.len == 0) {
  152. buf.add(s.substr(p.pos, 1));
  153. offset = p.pos + 1;
  154. }
  155. else
  156. offset = p.pos + p.len;
  157. } while (global);
  158. if (!global && offset > 0 && offset < s.length)
  159. buf.add(s.substr(offset));
  160. return buf.toString();
  161. }
  162. static var regexp_new_options : String -> String -> Dynamic = cpp.Lib.load("regexp","regexp_new_options",2);
  163. static var regexp_match : Dynamic -> String -> Int -> Int -> Dynamic = cpp.Lib.load("regexp","regexp_match",4);
  164. static var regexp_matched : Dynamic -> Int -> Dynamic = cpp.Lib.load("regexp","regexp_matched",2);
  165. static var regexp_matched_pos : Dynamic -> Int -> { pos : Int, len : Int } = cpp.Lib.load("regexp","regexp_matched_pos",2);
  166. }