2
0

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