EReg.hx 3.6 KB

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