EReg.hx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. @: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 split( s : String ) : Array<String> {
  58. var pos = 0;
  59. var len = s.length;
  60. var a = new Array();
  61. var first = true;
  62. do {
  63. if( !regexp_match(r,s,pos,len) )
  64. break;
  65. var p = regexp_matched_pos(r,0);
  66. if( p.len == 0 && !first ) {
  67. if( p.pos == s.length )
  68. break;
  69. p.pos += 1;
  70. }
  71. a.push(s.substr(pos,p.pos - pos));
  72. var tot = p.pos + p.len - pos;
  73. pos += tot;
  74. len -= tot;
  75. first = false;
  76. } while( global );
  77. a.push(s.substr(pos,len));
  78. return a;
  79. }
  80. public function replace( s : String, by : String ) : String {
  81. var b = new StringBuf();
  82. var pos = 0;
  83. var len = s.length;
  84. var a = by.split("$");
  85. var first = true;
  86. do {
  87. if( !regexp_match(r,s,pos,len) )
  88. break;
  89. var p = regexp_matched_pos(r,0);
  90. if( p.len == 0 && !first ) {
  91. if( p.pos == s.length )
  92. break;
  93. p.pos += 1;
  94. }
  95. b.addSub(s,pos,p.pos-pos);
  96. if( a.length > 0 )
  97. b.add(a[0]);
  98. var i = 1;
  99. while( i < a.length ) {
  100. var k = a[i];
  101. var c = k.charCodeAt(0);
  102. // 1...9
  103. if( c >= 49 && c <= 57 ) {
  104. var p = try regexp_matched_pos(r,Std.int(c)-48) catch( e : String ) null;
  105. if( p == null ){
  106. b.add("$");
  107. b.add(k);
  108. }else{
  109. b.addSub(s,p.pos,p.len);
  110. b.addSub(k,1,k.length - 1);
  111. }
  112. } else if( c == null ) {
  113. b.add("$");
  114. i++;
  115. var k2 = a[i];
  116. if( k2 != null && k2.length > 0 )
  117. b.add(k2);
  118. } else
  119. b.add("$"+k);
  120. i++;
  121. }
  122. var tot = p.pos + p.len - pos;
  123. pos += tot;
  124. len -= tot;
  125. first = false;
  126. } while( global );
  127. b.addSub(s,pos,len);
  128. return b.toString();
  129. }
  130. public function customReplace( s : String, f : EReg -> String ) : String {
  131. var buf = new StringBuf();
  132. while( true ) {
  133. if( !match(s) )
  134. break;
  135. buf.add(matchedLeft());
  136. buf.add(f(this));
  137. s = matchedRight();
  138. }
  139. buf.add(s);
  140. return buf.toString();
  141. }
  142. static var regexp_new_options : String -> String -> Dynamic = cpp.Lib.load("regexp","regexp_new_options",2);
  143. static var regexp_match : Dynamic -> String -> Int -> Int -> Dynamic = cpp.Lib.load("regexp","regexp_match",4);
  144. static var regexp_matched : Dynamic -> Int -> Dynamic = cpp.Lib.load("regexp","regexp_matched",2);
  145. static var regexp_matched_pos : Dynamic -> Int -> { pos : Int, len : Int } = cpp.Lib.load("regexp","regexp_matched_pos",2);
  146. }