Skip to content

Commit 994fcd4

Browse files
committed
tests, use just NonBacktracking instead
1 parent 0aeea1b commit 994fcd4

2 files changed

Lines changed: 30 additions & 11 deletions

File tree

src/fable-library-ts/RegExp.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,16 @@ export function create(pattern: string, options = 0) {
99
// * ECMAScript: 0x0100 (ignored)
1010
// * NonBacktracking: 0x0400
1111
if ((options & ~(1 ^ 2 ^ 8 ^ 16 ^ 256 ^ 1024)) !== 0) {
12-
throw new Error("RegexOptions only supports: IgnoreCase, Multiline, Compiled, Singleline, ECMAScript and (NonBacktracking ||| ECMAScript)");
12+
throw new Error("RegexOptions only supports: IgnoreCase, Multiline, Compiled, Singleline, ECMAScript and NonBacktracking");
1313
}
1414

15-
// Set always global and unicode flags for compatibility with dotnet, see #2925
15+
// Set global and unicode flags for compatibility with dotnet, see #2925
1616
let flags = "g";
17-
flags += options & 256 ? "" : "u"; // only add 'u' if not EcmaScript, ('l' and 'u' together are not allowed)
17+
// add unicode only if not NonBacktracking, 'l' and 'u' together are not allowed
18+
flags += options & 1024 ? "l" : "u";
1819
flags += options & 1 ? "i" : ""; // 0x0001 RegexOptions.IgnoreCase
1920
flags += options & 2 ? "m" : "";
2021
flags += options & 16 ? "s" : "";
21-
if (options & 1024) {
22-
if (options & 256) {
23-
flags += "l";
24-
}
25-
else {
26-
throw new Error("RegexOptions.NonBacktracking is only allowed together with ECMAScript");
27-
}
28-
}
2922
return new RegExp(pattern, flags);
3023
}
3124
// From http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex

tests/Js/Main/RegexTests.fs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,32 @@ let tests =
3737
let r = Regex("[a-z]", options)
3838
int r.Options |> equal 257
3939

40+
testCase "RegexOptions.NonBacktracking = 1024" <| fun _ ->
41+
let options = RegexOptions.NonBacktracking
42+
int options |> equal 1024
43+
44+
testCase "Nonbacktracking regex tests" <| fun _ ->
45+
// Note: this is not consistent with .NET because JS linear engine
46+
// does not support unicode. JS \d = 10 digits, .NET \d = 350+ digits
47+
// but for these tests the behavior should be identical.
48+
let none = RegexOptions.None
49+
let nonb = RegexOptions.NonBacktracking
50+
let multiline = RegexOptions.Multiline
51+
let ignorecase = RegexOptions.IgnoreCase
52+
let multilineignorecase = multiline ||| ignorecase
53+
let str = "For more information, see Chapter 3.4.5.1"
54+
let sameResult extraopts str pat =
55+
let result1 = Regex.Match(str, pat, none ||| extraopts).Success
56+
let result2 = Regex.Match(str, pat, nonb ||| extraopts).Success
57+
equal result1 result2
58+
sameResult none str "Chapter \d+(\.\d)*"
59+
sameResult none str "chapter \d+(\.\d)*"
60+
sameResult multilineignorecase "^ab" "ab\ncd"
61+
sameResult multilineignorecase "^cd" "ab\ncd"
62+
sameResult multilineignorecase "^AB" "ab\ncd"
63+
sameResult multilineignorecase "^bc" "ab\ncd"
64+
65+
4066
testCase "Regex.IsMatch with IgnoreCase and Multiline works" <| fun _ ->
4167
let str = "ab\ncd"
4268
let option1 = RegexOptions.IgnoreCase

0 commit comments

Comments
 (0)