EReg.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. @:core_api @:final class EReg {
  26. var r : Dynamic;
  27. var last : String;
  28. var global : Bool;
  29. var pattern : String;
  30. var options : String;
  31. var re : String;
  32. var matches : ArrayAccess<Dynamic>;
  33. static var QUOTE : String = untyped __call__("preg_quote", "/", "/");
  34. public function new( r : String, opt : String ) : Void {
  35. this.pattern = r;
  36. var a = opt.split("g");
  37. global = a.length > 1;
  38. if( global )
  39. opt = a.join("");
  40. this.options = opt;
  41. this.re = untyped __php__("'\"' . str_replace('\"','\\\\\"',$r) . '\"' . $opt");
  42. }
  43. public function match( s : String ) : Bool {
  44. var p : Int = untyped __call__("preg_match", re, s, matches, __php__("PREG_OFFSET_CAPTURE"));
  45. if(p > 0)
  46. last = s;
  47. else
  48. last = null;
  49. return p > 0;
  50. }
  51. public function matched( n : Int ) : String {
  52. if ( n < 0 ) throw "EReg::matched";
  53. // we can't differenciate between optional groups at the end of a match
  54. // that have not been matched and invalid groups
  55. if( n >= untyped __call__("count", matches)) return null;
  56. if(untyped __php__("$this->matches[$n][1] < 0")) return null;
  57. return untyped __php__("$this->matches[$n][0]");
  58. }
  59. public function matchedLeft() : String {
  60. if( untyped __call__("count", matches) == 0 ) throw "No string matched";
  61. return last.substr(0, untyped __php__("$this->matches[0][1]"));
  62. }
  63. public function matchedRight() : String {
  64. if( untyped __call__("count", matches) == 0 ) throw "No string matched";
  65. var x : Int = untyped __php__("$this->matches[0][1]") + __call__("strlen",__php__("$this->matches[0][0]"));
  66. return last.substr(x);
  67. }
  68. public function matchedPos() : { pos : Int, len : Int } {
  69. return untyped { pos : __php__("$this->matches[0][1]"), len : __call__("strlen",__php__("$this->matches[0][0]")) };
  70. }
  71. public function split( s : String ) : Array<String> {
  72. return untyped __php__("new _hx_array(preg_split($this->re, $s, $this->hglobal ? -1 : 2))");
  73. }
  74. public function replace( s : String, by : String ) : String {
  75. by = untyped __call__("str_replace", "\\$", "\\\\$", by);
  76. by = untyped __call__("str_replace", "$$", "\\$", by);
  77. untyped __php__("if(!preg_match('/\\\\([^?].+?\\\\)/', $this->re)) $by = preg_replace('/\\$(\\d+)/', '\\\\\\$\\1', $by)");
  78. return untyped __call__("preg_replace", re, by, s, global ? -1 : 1);
  79. }
  80. public function customReplace( s : String, f : EReg -> String ) : String {
  81. var buf = "";
  82. while( true ) {
  83. if( !match(s) )
  84. break;
  85. buf += matchedLeft();
  86. buf += f(this);
  87. s = matchedRight();
  88. }
  89. buf += s;
  90. return buf;
  91. }
  92. }