EReg.hx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C)2005-2017 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 (matches == null || 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 : s.substr(0,pos + len), matches, __php__("PREG_OFFSET_CAPTURE"), pos);
  69. if(p > 0) {
  70. last = s;
  71. }
  72. else
  73. last = null;
  74. return p > 0;
  75. }
  76. public function split( s : String ) : Array<String> {
  77. return untyped __php__("new _hx_array(preg_split($this->re, $s, $this->{\"global\"} ? -1 : 2))");
  78. }
  79. public function replace( s : String, by : String ) : String {
  80. by = untyped __call__("str_replace", "\\$", "\\\\$", by);
  81. by = untyped __call__("str_replace", "$$", "\\$", by);
  82. untyped __php__("if(!preg_match('/\\\\([^?].*?\\\\)/', $this->re)) $by = preg_replace('/\\$(\\d+)/', '\\\\\\$\\1', $by)");
  83. return untyped __call__("preg_replace", re, by, s, global ? -1 : 1);
  84. }
  85. public function map( s : String, f : EReg -> String ) : String {
  86. var offset = 0;
  87. var buf = new StringBuf();
  88. do {
  89. if (offset >= s.length)
  90. break;
  91. else if (!matchSub(s, offset)) {
  92. buf.add(s.substr(offset));
  93. break;
  94. }
  95. var p = matchedPos();
  96. buf.add(s.substr(offset, p.pos - offset));
  97. buf.add(f(this));
  98. if (p.len == 0) {
  99. buf.add(s.substr(p.pos, 1));
  100. offset = p.pos + 1;
  101. }
  102. else
  103. offset = p.pos + p.len;
  104. } while (global);
  105. if (!global && offset > 0 && offset < s.length)
  106. buf.add(s.substr(offset));
  107. return buf.toString();
  108. }
  109. public static function escape( s : String ) : String {
  110. return untyped __call__("preg_quote", s);
  111. }
  112. }