From bc6b0bee8fe4120642029daaa8ce6c069ef667b8 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Wed, 26 Aug 2015 17:29:32 -0400 Subject: Rework replacement API to rely on transformation Redesign library API around highly general regex-based transformations. Instead of specifying a string to substitute for each match, you now execute an entire function over the match (and over nonmatching regions as well). The resulting C++ code is much simpler, with more functionality pushed into Ur, and the engine now supports certain types of regex transformations needed to mimic Perl. --- src/regex__FFI.js | 57 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 26 deletions(-) (limited to 'src/regex__FFI.js') diff --git a/src/regex__FFI.js b/src/regex__FFI.js index d69369b..1062520 100644 --- a/src/regex__FFI.js +++ b/src/regex__FFI.js @@ -14,37 +14,42 @@ var UrWeb = { Regex: { -_compile: function(needle_string) { - var needle; +Substring: { + start: function(substring) { + return substring.start; + }, + + length: function(substring) { + return substring.length; + }, + + List: { + length: function(list) { + return list.length; + }, + + get: function(list, n) { + return list[n]; + }, + }, +}, + +doMatch: function(needle_string, haystack) { try { - needle = new RegExp(needle_string, "g"); + var needle = new RegExp(needle_string); } catch (e) { er("regex: compilation failed"); } - return needle; -}, - -succeeded: function(match) { - return !!match; -}, - -nSubexpressionMatches: function(match) { - return match.length - 1; -}, - -subexpressionMatch: function(match, n) { - if (match.length - 1 <= n) { - er("regex: match does not exist"); + var result = needle.exec(haystack); + if (result) { + for (var i = 0; i < result.length; i++) { + result[i] = {start: haystack.indexOf(result[i]), + length: result[i].length}; + } + } else { + result = [] } - return match[n + 1]; -}, - -doMatch: function(needle, haystack) { - return haystack.match(UrWeb.Regex._compile(needle)); -}, - -replace: function(needle, replacement, haystack) { - return haystack.replace(UrWeb.Regex._compile(needle), replacement); + return result; }, }}; // UrWeb.Regex -- cgit v1.2.3