2
0

EReg.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. import haxe.extern.EitherType;
  23. import php.*;
  24. @:coreApi @:final class EReg {
  25. var r : Dynamic;
  26. var last : String;
  27. var global : Bool;
  28. var pattern : String;
  29. var options : String;
  30. var re : String;
  31. var matches : NativeIndexedArray<NativeIndexedArray<EitherType<Int,String>>>;
  32. public function new( r : String, opt : String ) : Void {
  33. this.pattern = r;
  34. var a = opt.split("g");
  35. global = a.length > 1;
  36. if (global) {
  37. opt = a.join("");
  38. }
  39. this.options = opt;
  40. this.re = '"' + Global.str_replace('"', '\\"', r) + '"' + opt;
  41. }
  42. public function match( s : String ) : Bool {
  43. var p : Int = Global.preg_match(re, s, matches, Const.PREG_OFFSET_CAPTURE);
  44. if (p > 0) {
  45. last = s;
  46. } else {
  47. last = null;
  48. }
  49. return p > 0;
  50. }
  51. public function matched( n : Int ) : String {
  52. if (matches == null || 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 >= Global.count(matches)) return null;
  56. if ((matches[n][1]:Int) < 0) return null;
  57. return matches[n][0];
  58. }
  59. public function matchedLeft() : String {
  60. if (Global.count(matches) == 0) throw "No string matched";
  61. return last.substr(0, matches[0][1]);
  62. }
  63. public function matchedRight() : String {
  64. if (Global.count(matches) == 0) throw "No string matched";
  65. var x : Int = (matches[0][1]:Int) + Global.strlen(matches[0][0]);
  66. return last.substr(x);
  67. }
  68. public function matchedPos() : { pos : Int, len : Int } {
  69. return {
  70. pos : matches[0][1],
  71. len : Global.strlen(matches[0][0])
  72. };
  73. }
  74. public function matchSub( s : String, pos : Int, len : Int = -1):Bool {
  75. var subject = len < 0 ? s : s.substr(0,pos + len);
  76. var p : Int = Global.preg_match(re, subject, matches, Const.PREG_OFFSET_CAPTURE, pos);
  77. if(p > 0) {
  78. last = s;
  79. }
  80. else {
  81. last = null;
  82. }
  83. return p > 0;
  84. }
  85. public function split( s : String ) : Array<String> {
  86. var parts:NativeArray = Global.preg_split(re, s, (global ? -1 : 2));
  87. return @:privateAccess Array.wrap(parts);
  88. }
  89. public function replace( s : String, by : String ) : String {
  90. by = Global.str_replace("\\$", "\\\\$", by);
  91. by = Global.str_replace("$$", "\\$", by);
  92. if (!Global.preg_match('/\\\\([^?].*?\\\\)/', re)) {
  93. by = Global.preg_replace('/\\$(\\d+)/', '\\$\\1', by);
  94. }
  95. return Global.preg_replace(re, by, s, global ? -1 : 1);
  96. }
  97. public function map( s : String, f : EReg -> String ) : String {
  98. var offset = 0;
  99. var buf = new StringBuf();
  100. do {
  101. if (offset >= s.length) {
  102. break;
  103. } else if (!matchSub(s, offset)) {
  104. buf.add(s.substr(offset));
  105. break;
  106. }
  107. var p = matchedPos();
  108. buf.add(s.substr(offset, p.pos - offset));
  109. buf.add(f(this));
  110. if (p.len == 0) {
  111. buf.add(s.substr(p.pos, 1));
  112. offset = p.pos + 1;
  113. }
  114. else {
  115. offset = p.pos + p.len;
  116. }
  117. } while (global);
  118. if (!global && offset > 0 && offset < s.length) {
  119. buf.add(s.substr(offset));
  120. }
  121. return buf.toString();
  122. }
  123. public static inline function escape( s : String ) : String {
  124. return Global.preg_quote(s);
  125. }
  126. }