Skip to content

Commit 7f494c4

Browse files
Improve some regex functions
1 parent cd8913c commit 7f494c4

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

src/Fable.Transforms/Replacements.fs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2916,8 +2916,7 @@ let regex com (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Exp
29162916
| Some (ExprType (EntFullName "System.Text.RegularExpressions.Group")) -> true
29172917
| _ -> false
29182918

2919-
match i.CompiledName with
2920-
| ".ctor" ->
2919+
let createRegex r t args =
29212920
let makeRegexConst r (pattern: string) flags =
29222921
let flags = RegexFlag.RegexGlobal::flags // .NET regex are always global
29232922
RegexConstant(pattern, flags) |> makeValue r
@@ -2935,9 +2934,12 @@ let regex com (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Exp
29352934
| _ -> None
29362935
getFlags e
29372936
match args with
2938-
| [StringConst pattern] -> makeRegexConst r pattern [] |> Some
2939-
| StringConst pattern::(RegexFlags flags)::_ -> makeRegexConst r pattern flags |> Some
2940-
| _ -> Helper.LibCall(com, "RegExp", "create", t, args, i.SignatureArgTypes, ?loc=r) |> Some
2937+
| [StringConst pattern] -> makeRegexConst r pattern []
2938+
| StringConst pattern::(RegexFlags flags)::_ -> makeRegexConst r pattern flags
2939+
| _ -> Helper.LibCall(com, "RegExp", "create", t, args, ?loc=r)
2940+
2941+
match i.CompiledName with
2942+
| ".ctor" -> createRegex r t args |> Some
29412943
| "get_Options" -> Helper.LibCall(com, "RegExp", "options", t, [thisArg.Value], [thisArg.Value.Type], ?loc=r) |> Some
29422944
// Capture
29432945
| "get_Index" ->
@@ -2986,6 +2988,19 @@ let regex com (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Exp
29862988
| "get_Item" -> getExpr r t thisArg.Value args.Head |> Some
29872989
| "get_Count" -> propStr "length" thisArg.Value |> Some
29882990
| "GetEnumerator" -> getEnumerator com r t thisArg.Value |> Some
2991+
| "IsMatch" | "Match" | "Matches" as meth ->
2992+
match thisArg, args with
2993+
| Some thisArg, args ->
2994+
if args.Length > 2 then
2995+
$"Regex.{meth} doesn't support more than 2 arguments"
2996+
|> addError com ctx.InlinePath r
2997+
thisArg::args |> Some
2998+
| None, input::pattern::args ->
2999+
let reg = createRegex None Any (pattern::args)
3000+
[reg; input] |> Some
3001+
| _ -> None
3002+
|> Option.map (fun args ->
3003+
Helper.LibCall(com, "RegExp", Naming.lowerFirst meth, t, args, i.SignatureArgTypes, ?loc=r))
29893004
| meth ->
29903005
let meth = Naming.removeGetSetPrefix meth |> Naming.lowerFirst
29913006
Helper.LibCall(com, "RegExp", meth, t, args, i.SignatureArgTypes, ?thisArg=thisArg, ?loc=r) |> Some

src/fable-library/RegExp.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export type MatchEvaluator = (match: any) => string;
22

3-
export function create(pattern: string, options: number = 0) {
3+
export function create(pattern: string, options = 0) {
44
// Supported RegexOptions
55
// * IgnoreCase: 0x0001
66
// * Multiline: 0x0002
@@ -25,35 +25,26 @@ export function unescape(str: string) {
2525
return str.replace(/\\([\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|])/g, "$1");
2626
}
2727

28-
export function isMatch(str: string | RegExp, pattern: string, options: number = 0) {
29-
let reg: RegExp;
30-
reg = str instanceof RegExp
31-
? (reg = str as RegExp, str = pattern, reg.lastIndex = options, reg)
32-
: reg = create(pattern, options);
33-
return reg.test(str as string);
28+
export function isMatch(reg: RegExp, input: string, startAt = 0) {
29+
reg.lastIndex = startAt;
30+
return reg.test(input);
3431
}
3532

36-
export function match(str: string | RegExp, pattern: string, options: number = 0) {
37-
let reg: RegExp;
38-
reg = str instanceof RegExp
39-
? (reg = str as RegExp, str = pattern, reg.lastIndex = options, reg)
40-
: reg = create(pattern, options);
41-
return reg.exec(str as string);
33+
export function match(reg: RegExp, input: string, startAt = 0) {
34+
reg.lastIndex = startAt;
35+
return reg.exec(input);
4236
}
4337

44-
export function matches(str: string | RegExp, pattern: string, options: number = 0) {
45-
let reg: RegExp;
46-
reg = str instanceof RegExp
47-
? (reg = str as RegExp, str = pattern, reg.lastIndex = options, reg)
48-
: reg = create(pattern, options);
38+
export function matches(reg: RegExp, input: string, startAt = 0) {
39+
reg.lastIndex = startAt;
4940
if (!reg.global) {
5041
throw new Error("Non-global RegExp"); // Prevent infinite loop
5142
}
52-
let m = reg.exec(str as string);
43+
let m = reg.exec(input);
5344
const matches: RegExpExecArray[] = [];
5445
while (m !== null) {
5546
matches.push(m);
56-
m = reg.exec(str as string);
47+
m = reg.exec(input);
5748
}
5849
return matches;
5950
}

0 commit comments

Comments
 (0)