EReg.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 lua.Table;
  23. import lua.Lib;
  24. import lua.lib.lrexlib.Rex;
  25. // Note - lrexlib gives ascii-based offsets. Use native string tools.
  26. import lua.NativeStringTools;
  27. @:coreApi
  28. class EReg {
  29. var r : Rex; // the Rex extern instance.
  30. var global : Bool; // whether the regex is in global mode.
  31. var s : String; // the last matched string
  32. var m : Table<Int,Int>; // the [start:Int, end:Int, and submatches:String (matched groups)] as a single table.
  33. static var FLAGS : Table<String,Int> = Rex.flags();
  34. public function new( r : String, opt : String ) : Void {
  35. var ropt = 0;
  36. for (i in 0...opt.length){
  37. switch(opt.charAt(i)){
  38. case "i" : ropt |= FLAGS.CASELESS;
  39. case "m" : ropt |= FLAGS.MULTILINE;
  40. case "s" : ropt |= FLAGS.DOTALL;
  41. case "g" : global = true;
  42. default : null;
  43. }
  44. }
  45. ropt |= FLAGS.UTF8; // always check validity of utf8 string
  46. ropt |= FLAGS.UCP; // always enable utf8 character properties
  47. if (global == null) global = false;
  48. this.r = Rex.create(r, ropt);
  49. }
  50. public function match( s : String ) : Bool {
  51. if (s == null) return false;
  52. this.m = lua.TableTools.pack(r.exec(s));
  53. this.s = s;
  54. return m[1] != null;
  55. }
  56. public function matched( n : Int ) : String {
  57. if (m[1] == null || n < 0) throw "EReg::matched";
  58. else if (n == 0) {
  59. var k = NativeStringTools.sub(s, m[1], m[2]).match;
  60. return k;
  61. } else if (Std.is(m[3], lua.Table)){
  62. var mn = 2 * (n - 1);
  63. if (Std.is(untyped m[3][mn+1], Bool)) return null;
  64. return NativeStringTools.sub(s, untyped m[3][mn + 1], untyped m[3][mn + 2]).match;
  65. } else {
  66. throw "EReg:matched";
  67. }
  68. }
  69. public function matchedLeft() : String {
  70. if( m[1] == null ) throw "No string matched";
  71. return NativeStringTools.sub(s, 1, m[1]-1).match;
  72. }
  73. public function matchedRight() : String {
  74. if( m[1] == null ) throw "No string matched";
  75. return NativeStringTools.sub(s, m[2]+1).match;
  76. }
  77. public function matchedPos() : { pos : Int, len : Int } {
  78. var left = matchedLeft();
  79. var matched = matched(0);
  80. if( m[1] == null ) throw "No string matched";
  81. return {
  82. pos : left.length,
  83. len : matched.length
  84. }
  85. }
  86. public function matchSub( s : String, pos : Int, len : Int = -1):Bool {
  87. var ss = s.substr(0, len < 0 ? s.length : pos + len);
  88. if (global){
  89. m = lua.TableTools.pack(r.exec(ss, pos + 1));
  90. var b = m[1] != null;
  91. if (b){
  92. this.s = s;
  93. }
  94. return b;
  95. } else {
  96. m = lua.TableTools.pack(r.exec(ss, pos + 1));
  97. var b = m[1] != null;
  98. if (b){
  99. this.s = s;
  100. }
  101. return b;
  102. }
  103. }
  104. public function split( s : String ) : Array<String> {
  105. if (global){
  106. return Lib.fillArray(Rex.split(s, r));
  107. } else {
  108. // we can't use directly Rex.split because it's ignoring the 'g' flag
  109. var d = "#__delim__#";
  110. return Lib.fillArray(Rex.split(replace(s,d), d));
  111. }
  112. }
  113. public function replace( s : String, by : String ) : String {
  114. var chunks = by.split("$$");
  115. chunks = [for (chunk in chunks) Rex.gsub(chunk, "\\$(\\d)", "%%%1", 1)];
  116. by = chunks.join("$");
  117. return Rex.gsub(s,r,by, global ? null : 1);
  118. }
  119. public function map( s : String, f : EReg -> String ) : String {
  120. var offset = 0;
  121. var buf = new StringBuf();
  122. do {
  123. if (offset >= s.length){
  124. break;
  125. }
  126. else if (!matchSub(s, offset)) {
  127. buf.add(s.substr(offset));
  128. break;
  129. }
  130. var p = matchedPos();
  131. buf.add(s.substr(offset, p.pos - offset));
  132. buf.add(f(this));
  133. if (p.len == 0) {
  134. buf.add(s.substr(p.pos, 1));
  135. offset = p.pos + 1;
  136. }
  137. else
  138. offset = p.pos + p.len;
  139. } while (global);
  140. if (!global && offset > 0 && offset < s.length)
  141. buf.add(s.substr(offset));
  142. return buf.toString();
  143. }
  144. public static function escape( s : String ) : String {
  145. return escapeRegExpRe.map(s, function(r) return "\\" + r.matched(0));
  146. }
  147. static var escapeRegExpRe = ~/[\[\]{}()*+?.\\\^$|]/g;
  148. static function __init__() : Void {
  149. if (Rex == null){
  150. throw "Rex is missing. Please install lrexlib-pcre.";
  151. }
  152. }
  153. }