EReg.hx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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)
  71. throw "EReg::matched";
  72. // we can't differenciate between optional groups at the end of a match
  73. // that have not been matched and invalid groups
  74. if (n >= Global.count(matches))
  75. return null;
  76. if ((matches[n][1] : Int) < 0)
  77. return null;
  78. return matches[n][0];
  79. }
  80. public function matchedLeft():String {
  81. if (Global.count(matches) == 0)
  82. throw "No string matched";
  83. return Global.substr(last, 0, matches[0][1]);
  84. }
  85. public function matchedRight():String {
  86. if (Global.count(matches) == 0)
  87. throw "No string matched";
  88. var x:Int = (matches[0][1] : Int) + Global.strlen(matches[0][0]);
  89. return Global.substr(last, x);
  90. }
  91. public function matchedPos():{pos:Int, len:Int} {
  92. return {
  93. pos: Global.mb_strlen(Global.substr(last, 0, matches[0][1])),
  94. len: Global.mb_strlen(matches[0][0])
  95. };
  96. }
  97. public function matchSub(s:String, pos:Int, len:Int = -1):Bool {
  98. var subject = len < 0 ? s : s.substr(0, pos + len);
  99. var p = Global.preg_match(reUnicode, subject, matches, Const.PREG_OFFSET_CAPTURE, pos);
  100. if (p == false) {
  101. handlePregError();
  102. p = Global.preg_match(re, subject, matches, Const.PREG_OFFSET_CAPTURE, pos);
  103. }
  104. if ((p : Int) > 0) {
  105. last = s;
  106. } else {
  107. last = null;
  108. }
  109. return (p : Int) > 0;
  110. }
  111. public function split(s:String):Array<String> {
  112. var parts:NativeArray = Global.preg_split(reUnicode, s, (global ? -1 : 2));
  113. if (parts == null) {
  114. handlePregError();
  115. parts = Global.preg_split(re, s, (global ? -1 : 2));
  116. }
  117. return @:privateAccess Array.wrap(parts);
  118. }
  119. public function replace(s:String, by:String):String {
  120. by = Global.str_replace("\\$", "\\\\$", by);
  121. by = Global.str_replace("$$", "\\$", by);
  122. if (!Global.preg_match('/\\\\([^?].*?\\\\)/', re)) {
  123. by = Global.preg_replace('/\\$(\\d+)/', '\\$\\1', by);
  124. }
  125. var result = Global.preg_replace(reUnicode, by, s, global ? -1 : 1);
  126. if (result == null) {
  127. handlePregError();
  128. result = Global.preg_replace(re, by, s, global ? -1 : 1);
  129. }
  130. return result;
  131. }
  132. public function map(s:String, f:EReg->String):String {
  133. var offset = 0;
  134. var buf = new StringBuf();
  135. var length = s.length;
  136. do {
  137. if (offset >= length) {
  138. break;
  139. } else if (!matchSub(s, offset)) {
  140. buf.add(s.substr(offset));
  141. break;
  142. }
  143. var p = matchedPos();
  144. buf.add(s.substr(offset, p.pos - offset));
  145. buf.add(f(this));
  146. if (p.len == 0) {
  147. buf.add(s.substr(p.pos, 1));
  148. offset = p.pos + 1;
  149. } else {
  150. offset = p.pos + p.len;
  151. }
  152. } while (global);
  153. if (!global && offset > 0 && offset < length) {
  154. buf.add(s.substr(offset));
  155. }
  156. return buf.toString();
  157. }
  158. public static inline function escape(s:String):String {
  159. return Global.preg_quote(s);
  160. }
  161. inline function get_reUnicode():String {
  162. return Syntax.concat(re, 'u');
  163. }
  164. }