Skip to content

Commit ff0849e

Browse files
authored
Merge pull request #9 from fink-lang/regex
regex
2 parents 3606292 + bac2d51 commit ff0849e

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/regex.fnk

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{raw} = import './str'
22

33

4-
regex = fn pattern, flags:
5-
new RegExp pattern, flags
4+
regex = fn pattern, flags='':
5+
new RegExp pattern, 'u${flags}'
66

77

88
---
@@ -18,11 +18,15 @@ reg = rx'
1818
```
1919
---
2020
rx = fn strings, ...exprs:
21-
ingorables = regex raw'(\s*#.*\n)|(\n\s*)'
21+
# implement in loxia or larix for optimization
22+
ingorables = regex raw'(\s+#.*\n)|(\n\s*)'
23+
esc = regex raw`(^|[^\\])((\\{2})*)\\(?=[#'"${'`'}'])`
2224

2325
[...parts] = pipe strings.raw:
2426
map part:
25-
replace_all part, ingorables, ''
27+
pipe part:
28+
replace_all ?, ingorables, ''
29+
replace_all ?, esc, '$1$3'
2630

2731
regex raw {raw: parts}, ...exprs
2832

src/regex.test.fnk

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,25 @@ describe 'string templates', fn:
6262
it 'handles multiline commented regex', fn:
6363
expect
6464
rx'
65-
(?<year>\d{4})- # year part of a date
65+
(?<year>\d{4})- # year part of a date
6666
(?<month>\d{2})- # month part of a date
67-
(?<day>\d{2}) # day part of a date
67+
(?<day>\d{2}) # day part of a date
6868
'
6969
to_equal rx'(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})'
7070

7171

72-
it 'handles escaping comment', fn:
72+
it 'handles escaping', fn:
7373
expect
74-
rx'foo\#bar'
75-
to_equal regex 'foo\\#bar'
74+
rx'
75+
\'\"\`
76+
foo \# bar
77+
\${2}
78+
\\*
79+
'
80+
to_equal regex `'"\`foo # bar\\\${2}\\\\*`
7681

7782

7883
it 'handles spaces', fn:
7984
expect
8085
rx'[ ]{3}'
8186
to_equal regex '[ ]{3}'
82-
83-

0 commit comments

Comments
 (0)