Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Less RegEx construction on recursive method #16351

Merged
merged 5 commits into from
Dec 5, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/Compiler/Utilities/sformat.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,10 @@ module Display =
else
let messageRegexPattern = @"^(?<pre>.*?)(?<!\\){(?<prop>.*?)(?<!\\)}(?<post>.*)$"
let illFormedBracketPattern = @"(?<!\\){|(?<!\\)}"
let messageRegexLookup = System.Text.RegularExpressions.Regex(messageRegexPattern)

let illFormedBracketPatternLookup =
Thorium marked this conversation as resolved.
Show resolved Hide resolved
System.Text.RegularExpressions.Regex(illFormedBracketPattern)

let rec buildObjMessageL (txt: string) (layouts: Layout list) =

Expand All @@ -1066,12 +1070,11 @@ module Display =
// 1) Everything up to the first opening bracket not preceded by a "\", lazily
// 2) Everything between that opening bracket and a closing bracket not preceded by a "\", lazily
// 3) Everything after that closing bracket
let m = System.Text.RegularExpressions.Regex.Match(txt, messageRegexPattern)
let m = messageRegexLookup.Match txt

if not m.Success then
// there isn't a match on the regex looking for a property, so now let's make sure we don't have an ill-formed format string (i.e. mismatched/stray brackets)
let illFormedMatch =
System.Text.RegularExpressions.Regex.IsMatch(txt, illFormedBracketPattern)
let illFormedMatch = illFormedBracketPatternLookup.IsMatch txt

if illFormedMatch then
None // there are mismatched brackets, bail out
Expand Down Expand Up @@ -1129,10 +1132,7 @@ module Display =

// look for stray brackets in the text before the next opening bracket
let strayClosingMatch =
System.Text.RegularExpressions.Regex.IsMatch(
postTextMatch.Groups["pre"].Value,
illFormedBracketPattern
)
illFormedBracketPatternLookup.IsMatch postTextMatch.Groups["pre"].Value

if strayClosingMatch then
None
Expand All @@ -1143,8 +1143,7 @@ module Display =

| remaingPropertyText ->
// make sure we don't have any stray brackets
let strayClosingMatch =
System.Text.RegularExpressions.Regex.IsMatch(remaingPropertyText, illFormedBracketPattern)
let strayClosingMatch = illFormedBracketPatternLookup.IsMatch remaingPropertyText

if strayClosingMatch then
None
Expand Down