EReg.hx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C)2005-2019 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 reUnicode(get,never):String;
  32. var matches : NativeIndexedArray<NativeIndexedArray<EitherType<Int,String>>>;
  33. public function new( r : String, opt : String ) : Void {
  34. this.pattern = r;
  35. options = Global.str_replace('g', '', opt);
  36. global = options != opt;
  37. this.re = '"' + Global.str_replace('"', '\\"', r) + '"' + options;
  38. }
  39. public function match( s : String ) : Bool {
  40. var p = Global.preg_match(reUnicode, s, matches, Const.PREG_OFFSET_CAPTURE);
  41. if(p == false) {
  42. handlePregError();
  43. p = Global.preg_match(re, s, matches, Const.PREG_OFFSET_CAPTURE);
  44. }
  45. if ((p:Int) > 0) {
  46. last = s;
  47. } else {
  48. last = null;
  49. }
  50. return (p:Int) > 0;
  51. }
  52. function handlePregError():Void {
  53. var e = Global.preg_last_error();
  54. if(e == Const.PREG_INTERNAL_ERROR) {
  55. throw 'EReg: internal PCRE error';
  56. } else if(e == Const.PREG_BACKTRACK_LIMIT_ERROR) {
  57. throw 'EReg: backtrack limit';
  58. } else if(e == Const.PREG_RECURSION_LIMIT_ERROR) {
  59. throw 'EReg: recursion limit';
  60. } else if(e == Const.PREG_JIT_STACKLIMIT_ERROR) {
  61. throw 'failed due to limited JIT stack space';
  62. }
  63. // else if(e == PREG_BAD_UTF8_ERROR) {
  64. // throw 'EReg: malformed UTF8';
  65. // } else if(e == PREG_BAD_UTF8_OFFSET_ERROR) {
  66. // throw 'EReg: the offset didn\'t correspond to the begin of a valid UTF-8 code point';
  67. // }
  68. }
  69. public function matched( n : Int ) : String {
  70. if (matches == null || n < 0) throw "EReg::matched";
  71. // we can't differenciate between optional groups at the end of a match
  72. // that have not been matched and invalid groups
  73. if (n >= Global.count(matches)) return null;
  74. if ((matches[n][1]:Int) < 0) return null;
  75. return matches[n][0];
  76. }
  77. public function matchedLeft() : String {
  78. if (Global.count(matches) == 0) throw "No string matched";
  79. return Global.substr(last, 0, matches[0][1]);
  80. }
  81. public function matchedRight() : String {
  82. if (Global.count(matches) == 0) throw "No string matched";
  83. var x : Int = (matches[0][1]:Int) + Global.strlen(matches[0][0]);
  84. return Global.substr(last, x);
  85. }
  86. public function matchedPos() : { pos : Int, len : Int } {
  87. return {
  88. pos : Global.mb_strlen(Global.substr(last, 0, matches[0][1])),
  89. len : Global.mb_strlen(matches[0][0])
  90. };
  91. }
  92. public function matchSub( s : String, pos : Int, len : Int = -1):Bool {
  93. var subject = len < 0 ? s : s.substr(0, pos + len);
  94. var p = Global.preg_match(reUnicode, subject, matches, Const.PREG_OFFSET_CAPTURE, pos);
  95. if(p == false) {
  96. handlePregError();
  97. p = Global.preg_match(re, subject, matches, Const.PREG_OFFSET_CAPTURE, pos);
  98. }
  99. if((p:Int) > 0) {
  100. last = s;
  101. }
  102. else {
  103. last = null;
  104. }
  105. return (p:Int) > 0;
  106. }
  107. public function split( s : String ) : Array<String> {
  108. var parts:NativeArray = Global.preg_split(reUnicode, s, (global ? -1 : 2));
  109. if(parts == null) {
  110. handlePregError();
  111. parts = Global.preg_split(re, s, (global ? -1 : 2));
  112. }
  113. return @:privateAccess Array.wrap(parts);
  114. }
  115. public function replace( s : String, by : String ) : String {
  116. by = Global.str_replace("\\$", "\\\\$", by);
  117. by = Global.str_replace("$$", "\\$", by);
  118. if (!Global.preg_match('/\\\\([^?].*?\\\\)/', re)) {
  119. by = Global.preg_replace('/\\$(\\d+)/', '\\$\\1', by);
  120. }
  121. var result = Global.preg_replace(reUnicode, by, s, global ? -1 : 1);
  122. if(result == null) {
  123. handlePregError();
  124. result = Global.preg_replace(re, by, s, global ? -1 : 1);
  125. }
  126. return result;
  127. }
  128. public function map( s : String, f : EReg -> String ) : String {
  129. var offset = 0;
  130. var buf = new StringBuf();
  131. var length = s.length;
  132. do {
  133. if (offset >= length) {
  134. break;
  135. } else if (!matchSub(s, offset)) {
  136. buf.add(s.substr(offset));
  137. break;
  138. }
  139. var p = matchedPos();
  140. buf.add(s.substr(offset, p.pos - offset));
  141. buf.add(f(this));
  142. if (p.len == 0) {
  143. buf.add(s.substr(p.pos, 1));
  144. offset = p.pos + 1;
  145. }
  146. else {
  147. offset = p.pos + p.len;
  148. }
  149. } while (global);
  150. if (!global && offset > 0 && offset < length) {
  151. buf.add(s.substr(offset));
  152. }
  153. return buf.toString();
  154. }
  155. public static inline function escape( s : String ) : String {
  156. return Global.preg_quote(s);
  157. }
  158. inline function get_reUnicode():String {
  159. return Syntax.concat(re, 'u');
  160. }
  161. }