EReg.hx 4.6 KB

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