EReg.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. @:coreApi class EReg {
  23. var r:HaxeRegExp;
  24. public inline function new(r:String, opt:String):Void {
  25. this.r = new HaxeRegExp(r, opt.split("u").join("")); // 'u' (utf8) depends on page encoding
  26. }
  27. public function match(s:String):Bool {
  28. if (r.global)
  29. r.lastIndex = 0;
  30. r.m = r.exec(s);
  31. r.s = s;
  32. return (r.m != null);
  33. }
  34. public function matched(n:Int):String {
  35. return if (r.m != null && n >= 0 && n < r.m.length) r.m[n] else throw "EReg::matched";
  36. }
  37. public function matchedLeft():String {
  38. if (r.m == null)
  39. throw "No string matched";
  40. return r.s.substr(0, r.m.index);
  41. }
  42. public function matchedRight():String {
  43. if (r.m == null)
  44. throw "No string matched";
  45. var sz = r.m.index + r.m[0].length;
  46. return r.s.substr(sz, r.s.length - sz);
  47. }
  48. public function matchedPos():{pos:Int, len:Int} {
  49. if (r.m == null)
  50. throw "No string matched";
  51. return {pos: r.m.index, len: r.m[0].length};
  52. }
  53. public function matchedNum():Int {
  54. if (r.m == null)
  55. return 0;
  56. return r.m.length;
  57. }
  58. public function matchSub(s:String, pos:Int, len:Int = -1):Bool {
  59. return if (r.global) {
  60. r.lastIndex = pos;
  61. r.m = r.exec(len < 0 ? s : s.substr(0, pos + len));
  62. var b = r.m != null;
  63. if (b) {
  64. r.s = s;
  65. }
  66. b;
  67. } else {
  68. // TODO: check some ^/$ related corner cases
  69. var b = match(len < 0 ? s.substr(pos) : s.substr(pos, len));
  70. if (b) {
  71. r.s = s;
  72. r.m.index += pos;
  73. }
  74. b;
  75. }
  76. }
  77. public function split(s:String):Array<String> {
  78. // we can't use directly s.split because it's ignoring the 'g' flag
  79. var d = "#__delim__#";
  80. return replace(s, d).split(d);
  81. }
  82. public inline function replace(s:String, by:String):String {
  83. return js.lib.NativeStringTools.replace(s, r, by);
  84. }
  85. public function map(s:String, f:EReg->String):String {
  86. var offset = 0;
  87. var buf = new StringBuf();
  88. do {
  89. if (offset >= s.length)
  90. break;
  91. else if (!matchSub(s, offset)) {
  92. buf.add(s.substr(offset));
  93. break;
  94. }
  95. var p = matchedPos();
  96. buf.add(s.substr(offset, p.pos - offset));
  97. buf.add(f(this));
  98. if (p.len == 0) {
  99. buf.add(s.substr(p.pos, 1));
  100. offset = p.pos + 1;
  101. } else
  102. offset = p.pos + p.len;
  103. } while (r.global);
  104. if (!r.global && offset > 0 && offset < s.length)
  105. buf.add(s.substr(offset));
  106. return buf.toString();
  107. }
  108. public static inline function escape(s:String):String {
  109. return js.lib.NativeStringTools.replace(s, escapeRe, "\\$&");
  110. }
  111. static var escapeRe = new js.lib.RegExp("[.*+?^${}()|[\\]\\\\]", "g");
  112. }
  113. @:native("RegExp")
  114. private extern class HaxeRegExp extends js.lib.RegExp {
  115. var m:js.lib.RegExp.RegExpMatch;
  116. var s:String;
  117. }