EReg.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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:flash.utils.RegExp;
  24. var result:Dynamic;
  25. public function new(r:String, opt:String):Void {
  26. this.r = new flash.utils.RegExp(r, opt);
  27. }
  28. public function match(s:String):Bool {
  29. if (r.global)
  30. r.lastIndex = 0;
  31. result = r.exec(s);
  32. return (result != null);
  33. }
  34. public function matched(n:Int):String {
  35. return if (result != null && n >= 0 && n < (result:Array<Dynamic>).length) result[n] else throw "EReg::matched";
  36. }
  37. public function matchedLeft():String {
  38. if (result == null)
  39. throw "No string matched";
  40. var s:String = result.input;
  41. return s.substr(0, result.index);
  42. }
  43. public function matchedRight():String {
  44. if (result == null)
  45. throw "No string matched";
  46. var rl = (result.index : Int) + (result[0] : String).length;
  47. var s:String = result.input;
  48. return s.substr(rl, s.length - rl);
  49. }
  50. public function matchedPos():{pos:Int, len:Int} {
  51. if (result == null)
  52. throw "No string matched";
  53. return {pos: result.index, len: (result[0] : String).length};
  54. }
  55. public function matchedNum():Int {
  56. if (result == null) return 0;
  57. return result.length;
  58. }
  59. public function matchSub(s:String, pos:Int, len:Int = -1):Bool {
  60. return if (r.global) {
  61. r.lastIndex = pos;
  62. result = r.exec(len < 0 ? s : s.substr(0, pos + len));
  63. var b = result != null;
  64. if (b) {
  65. result.input = s;
  66. }
  67. b;
  68. } else {
  69. var b = match(len < 0 ? s.substr(pos) : s.substr(pos, len));
  70. if (b) {
  71. result.input = s;
  72. result.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. var s:String = (s : Dynamic).replace(r, d);
  81. return s.split(d);
  82. }
  83. public function replace(s:String, by:String):String {
  84. return (s : Dynamic).replace(r, by);
  85. }
  86. public function map(s:String, f:EReg->String):String {
  87. var offset = 0;
  88. var buf = new StringBuf();
  89. var first = true;
  90. do {
  91. if (offset >= s.length)
  92. break;
  93. else if (!matchSub(s, offset)) {
  94. buf.add(s.substr(offset));
  95. break;
  96. }
  97. var p = matchedPos();
  98. buf.add(s.substr(offset, p.pos - offset));
  99. buf.add(f(this));
  100. if (p.len == 0) {
  101. buf.add(s.substr(p.pos, 1));
  102. offset = p.pos + 1;
  103. } else
  104. offset = p.pos + p.len;
  105. first = false;
  106. } while (r.global);
  107. if (!r.global && offset > 0 && offset < s.length)
  108. buf.add(s.substr(offset));
  109. return buf.toString();
  110. }
  111. public static inline function escape(s:String):String {
  112. return (cast s).replace(escapeRe, "\\$&");
  113. }
  114. static var escapeRe = new flash.utils.RegExp("[.*+?^${}()|[\\]\\\\]", "g");
  115. }