cross-platform match function in re-flex
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
test.js
|
@@ -1 +1,7 @@
|
||||
(print "Hello world!")
|
||||
(let [match (R.match "a" "12abc")]
|
||||
(case match
|
||||
((object
|
||||
left "12"
|
||||
match "a"
|
||||
right "bc"))
|
||||
(otherwise (throw "bad match result"))))
|
||||
|
@@ -2,6 +2,21 @@ package re_flex;
|
||||
|
||||
import kiss.Prelude;
|
||||
import kiss.List;
|
||||
#if hxnodejs
|
||||
import js.lib.RegExp;
|
||||
import haxe.DynamicAccess;
|
||||
#end
|
||||
|
||||
typedef RMatch = {
|
||||
left:String,
|
||||
match:String,
|
||||
right:String,
|
||||
#if hxnodejs
|
||||
groups:Array<String>,
|
||||
namedGroups:DynamicAccess<String,String>,
|
||||
#end
|
||||
group:Int->String
|
||||
};
|
||||
|
||||
@:build(kiss.Kiss.build())
|
||||
class R {}
|
||||
|
@@ -27,3 +27,32 @@
|
||||
[])
|
||||
(true
|
||||
(for _ (range (- max min)) (oneOf expr "")))))))
|
||||
|
||||
// TODO could memoize compiled regexes
|
||||
(function :Null<RMatch> match [:String regex :String textToSearch &opt :String flags]
|
||||
(#if hxnodejs
|
||||
(ifLet [:RegExpMatch match (.exec (new RegExp regex /* TODO use or for this once it's fixed */ (if flags flags "")) textToSearch)]
|
||||
(object
|
||||
left
|
||||
(textToSearch.substr 0 match.index)
|
||||
match
|
||||
(first match)
|
||||
right
|
||||
(textToSearch.substr (+ match.index .length (first match)))
|
||||
group
|
||||
->idx (nth match idx)
|
||||
groups
|
||||
match
|
||||
namedGroups
|
||||
match.groups))
|
||||
(let [r (new EReg regex /*TODO use or for this once fixed */(if flags flags ""))]
|
||||
(if (r.match textToSearch)
|
||||
(object
|
||||
left
|
||||
(r.matchedLeft)
|
||||
match
|
||||
(r.matched 0)
|
||||
right
|
||||
(r.matchedRight)
|
||||
group
|
||||
->idx (r.matched idx))))))
|
5
test-js.hxml
Normal file
5
test-js.hxml
Normal file
@@ -0,0 +1,5 @@
|
||||
-lib kiss
|
||||
-cp src
|
||||
--main re_flex.Main
|
||||
--js test.js
|
||||
--cmd node test.js
|
Reference in New Issue
Block a user