EReg.hx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C)2005-2018 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. options = Global.str_replace('g', '', opt);
  35. global = options != opt;
  36. this.re = '"' + Global.str_replace('"', '\\"', r) + '"u' + options;
  37. }
  38. public function match( s : String ) : Bool {
  39. var p : Int = Global.preg_match(re, s, matches, Const.PREG_OFFSET_CAPTURE);
  40. if (p > 0) {
  41. last = s;
  42. } else {
  43. last = null;
  44. }
  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 >= Global.count(matches)) return null;
  52. if ((matches[n][1]:Int) < 0) return null;
  53. return matches[n][0];
  54. }
  55. public function matchedLeft() : String {
  56. if (Global.count(matches) == 0) throw "No string matched";
  57. return Global.substr(last, 0, matches[0][1]);
  58. }
  59. public function matchedRight() : String {
  60. if (Global.count(matches) == 0) throw "No string matched";
  61. var x : Int = (matches[0][1]:Int) + Global.strlen(matches[0][0]);
  62. return Global.substr(last, x);
  63. }
  64. public function matchedPos() : { pos : Int, len : Int } {
  65. return {
  66. pos : Global.mb_strlen(Global.substr(last, 0, matches[0][1])),
  67. len : Global.mb_strlen(matches[0][0])
  68. };
  69. }
  70. public function matchSub( s : String, pos : Int, len : Int = -1):Bool {
  71. var subject = len < 0 ? s : s.substr(0, pos + len);
  72. var p : Int = Global.preg_match(re, subject, matches, Const.PREG_OFFSET_CAPTURE, pos);
  73. if(p > 0) {
  74. last = s;
  75. }
  76. else {
  77. last = null;
  78. }
  79. return p > 0;
  80. }
  81. public function split( s : String ) : Array<String> {
  82. var parts:NativeArray = Global.preg_split(re, s, (global ? -1 : 2));
  83. return @:privateAccess Array.wrap(parts);
  84. }
  85. public function replace( s : String, by : String ) : String {
  86. by = Global.str_replace("\\$", "\\\\$", by);
  87. by = Global.str_replace("$$", "\\$", by);
  88. if (!Global.preg_match('/\\\\([^?].*?\\\\)/', re)) {
  89. by = Global.preg_replace('/\\$(\\d+)/', '\\$\\1', by);
  90. }
  91. return Global.preg_replace(re, by, s, global ? -1 : 1);
  92. }
  93. public function map( s : String, f : EReg -> String ) : String {
  94. var offset = 0;
  95. var buf = new StringBuf();
  96. var length = s.length;
  97. do {
  98. if (offset >= length) {
  99. break;
  100. } else if (!matchSub(s, offset)) {
  101. buf.add(s.substr(offset));
  102. break;
  103. }
  104. var p = matchedPos();
  105. buf.add(s.substr(offset, p.pos - offset));
  106. buf.add(f(this));
  107. if (p.len == 0) {
  108. buf.add(s.substr(p.pos, 1));
  109. offset = p.pos + 1;
  110. }
  111. else {
  112. offset = p.pos + p.len;
  113. }
  114. } while (global);
  115. if (!global && offset > 0 && offset < length) {
  116. buf.add(s.substr(offset));
  117. }
  118. return buf.toString();
  119. }
  120. public static inline function escape( s : String ) : String {
  121. return Global.preg_quote(s);
  122. }
  123. }