EReg.hx 3.8 KB

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