EReg.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 @:final class EReg {
  23. var r : Dynamic;
  24. var last : String;
  25. var global : Bool;
  26. var pattern : String;
  27. var options : String;
  28. var re : String;
  29. var matches : ArrayAccess<Dynamic>;
  30. public function new( r : String, opt : String ) : Void {
  31. this.pattern = r;
  32. var a = opt.split("g");
  33. global = a.length > 1;
  34. if( global )
  35. opt = a.join("");
  36. this.options = opt;
  37. this.re = untyped __php__("'\"' . str_replace('\"','\\\\\"',$r) . '\"' . $opt");
  38. }
  39. public function match( s : String ) : Bool {
  40. var p : Int = untyped __call__("preg_match", re, s, matches, __php__("PREG_OFFSET_CAPTURE"));
  41. if(p > 0)
  42. last = s;
  43. else
  44. last = null;
  45. return p > 0;
  46. }
  47. public function matched( n : Int ) : String {
  48. if ( n < 0 ) throw "EReg::matched";
  49. // we can't differenciate between optional groups at the end of a match
  50. // that have not been matched and invalid groups
  51. if( n >= untyped __call__("count", matches)) return null;
  52. if(untyped __php__("$this->matches[$n][1] < 0")) return null;
  53. return untyped __php__("$this->matches[$n][0]");
  54. }
  55. public function matchedLeft() : String {
  56. if( untyped __call__("count", matches) == 0 ) throw "No string matched";
  57. return last.substr(0, untyped __php__("$this->matches[0][1]"));
  58. }
  59. public function matchedRight() : String {
  60. if( untyped __call__("count", matches) == 0 ) throw "No string matched";
  61. var x : Int = untyped __php__("$this->matches[0][1]") + __call__("strlen",__php__("$this->matches[0][0]"));
  62. return last.substr(x);
  63. }
  64. public function matchedPos() : { pos : Int, len : Int } {
  65. return untyped { pos : __php__("$this->matches[0][1]"), len : __call__("strlen",__php__("$this->matches[0][0]")) };
  66. }
  67. public function matchSub( s : String, pos : Int, len : Int = -1):Bool {
  68. var p : Int = untyped __call__("preg_match", re, len < 0 ? s.substr(pos) : s.substr(pos,len), matches, __php__("PREG_OFFSET_CAPTURE"));
  69. if(p > 0) {
  70. untyped __php__("$this->matches[0][1] += $pos");
  71. last = s;
  72. }
  73. else
  74. last = null;
  75. return p > 0;
  76. }
  77. public function split( s : String ) : Array<String> {
  78. return untyped __php__("new _hx_array(preg_split($this->re, $s, $this->hglobal ? -1 : 2))");
  79. }
  80. public function replace( s : String, by : String ) : String {
  81. by = untyped __call__("str_replace", "\\$", "\\\\$", by);
  82. by = untyped __call__("str_replace", "$$", "\\$", by);
  83. untyped __php__("if(!preg_match('/\\\\([^?].+?\\\\)/', $this->re)) $by = preg_replace('/\\$(\\d+)/', '\\\\\\$\\1', $by)");
  84. return untyped __call__("preg_replace", re, by, s, global ? -1 : 1);
  85. }
  86. public function map( s : String, f : EReg -> String ) : String {
  87. var offset = 0;
  88. var buf = new StringBuf();
  89. do {
  90. if (!matchSub(s, offset))
  91. break;
  92. var p = matchedPos();
  93. buf.add(s.substr(offset, p.pos - offset));
  94. buf.add(f(this));
  95. offset = p.pos + p.len;
  96. } while (global);
  97. buf.add(s.substr(offset));
  98. return buf.toString();
  99. }
  100. #if !haxe3
  101. public inline function customReplace( s : String, f : EReg -> String ) : String return map(s, f)
  102. #end
  103. }