1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
import lua.Table;
import lua.Lib;
import lua.lib.lrexlib.Rex;
// Note - lrexlib gives ascii-based offsets. Use native string tools.
import lua.NativeStringTools.*;
@:coreApi
class EReg {
var r:Rex; // the Rex extern instance.
var global:Bool; // whether the regex is in global mode.
var s:String; // the last matched string
var m:Table<Int, Int>; // the [start:Int, end:Int, and submatches:String (matched groups)] as a single table.
static var FLAGS:Table<String, Int> = Rex.flags();
public function new(r:String, opt:String):Void {
var ropt = 0;
for (i in 0...opt.length) {
switch (opt.charAt(i)) {
case "i":
ropt |= FLAGS.CASELESS;
case "m":
ropt |= FLAGS.MULTILINE;
case "s":
ropt |= FLAGS.DOTALL;
case "g":
global = true;
default:
null;
}
}
ropt |= FLAGS.UTF8; // always check validity of utf8 string
ropt |= FLAGS.UCP; // always enable utf8 character properties
if (global == null)
global = false;
this.r = Rex.create(r, ropt);
}
public function match(s:String):Bool {
return matchFromByte(s, 1);
}
inline function matchFromByte(s:String, offset:Int):Bool {
if (s == null)
return false;
this.m = lua.TableTools.pack(r.exec(s, offset));
this.s = s;
return m[1] != null;
}
public function matched(n:Int):String {
if (m[1] == null || n < 0)
throw "EReg::matched";
else if (n == 0) {
var k = sub(s, m[1], m[2]).match;
return k;
} else if (Std.isOfType(m[3], lua.Table)) {
var mn = 2 * (n - 1);
if (Std.isOfType(untyped m[3][mn + 1], Bool))
return null;
return sub(s, untyped m[3][mn + 1], untyped m[3][mn + 2]).match;
} else {
throw "EReg:matched";
}
}
public function matchedLeft():String {
if (m[1] == null)
throw "No string matched";
return sub(s, 1, m[1] - 1).match;
}
public function matchedRight():String {
if (m[1] == null)
throw "No string matched";
return sub(s, m[2] + 1).match;
}
public function matchedPos():{pos:Int, len:Int} {
var left = matchedLeft();
var matched = matched(0);
if (m[1] == null)
throw "No string matched";
return {
pos: left.length,
len: matched.length
}
}
public function matchSub(s:String, pos:Int, len:Int = -1):Bool {
var ss = s.substr(0, len < 0 ? s.length : pos + len);
if (global) {
m = lua.TableTools.pack(r.exec(ss, pos + 1));
var b = m[1] != null;
if (b) {
this.s = s;
}
return b;
} else {
m = lua.TableTools.pack(r.exec(ss, pos + 1));
var b = m[1] != null;
if (b) {
this.s = s;
}
return b;
}
}
public function split(s:String):Array<String> {
if (global) {
return Lib.fillArray(Rex.split(s, r));
} else {
// we can't use directly Rex.split because it's ignoring the 'g' flag
var d = "#__delim__#";
return Lib.fillArray(Rex.split(replace(s, d), d));
}
}
public function replace(s:String, by:String):String {
var chunks = by.split("$$");
chunks = [for (chunk in chunks) Rex.gsub(chunk, "\\$(\\d)", "%%%1", 1)];
by = chunks.join("$");
return Rex.gsub(s, r, by, global ? null : 1);
}
public function map(s:String, f:EReg->String):String {
var bytesOffset = 1;
var buf = new StringBuf();
do {
if (bytesOffset > len(s)) {
break;
} else if (!matchFromByte(s, bytesOffset)) {
buf.add(sub(s, bytesOffset).match);
break;
}
var pos = m[1];
var length = m[2] - m[1];
buf.add(sub(s, bytesOffset, pos - 1).match);
buf.add(f(this));
if (length < 0) {
var charBytes = len(sub(s, pos).match.charAt(0));
buf.add(sub(s, pos, pos + charBytes - 1).match);
bytesOffset = pos + charBytes;
} else {
bytesOffset = m[2] + 1;
}
} while (global);
if (!global && bytesOffset > 1 && bytesOffset - 1 < len(s))
buf.add(sub(s, bytesOffset).match);
return buf.toString();
}
function map_old(s:String, f:EReg->String):String {
var offset = 0;
var buf = new StringBuf();
do {
if (offset >= s.length) {
break;
} else if (!matchSub(s, offset)) {
buf.add(s.substr(offset));
break;
}
var p = matchedPos();
buf.add(s.substr(offset, p.pos - offset));
buf.add(f(this));
if (p.len == 0) {
buf.add(s.substr(p.pos, 1));
offset = p.pos + 1;
} else
offset = p.pos + p.len;
} while (global);
if (!global && offset > 0 && offset < s.length)
buf.add(s.substr(offset));
return buf.toString();
}
public static function escape(s:String):String {
return escapeRegExpRe.map(s, function(r) return "\\" + r.matched(0));
}
static var escapeRegExpRe = ~/[\[\]{}()*+?.\\\^$|]/g;
static function __init__():Void {
if (Rex == null) {
throw "Rex is missing. Please install lrexlib-pcre.";
}
}
}
|