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

Optimize Filename.checkPathForIllegalChars #1662

Merged
Merged
Changes from all 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
32 changes: 22 additions & 10 deletions src/utils/filename.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,29 @@ let illegalPathChars =
let chars = Path.GetInvalidPathChars ()
chars

let checkPathForIllegalChars (path:string) =
let len = path.Length
for i = 0 to len - 1 do
let c = path.[i]

// Determine if this character is disallowed within a path by
// attempting to find it in the array of illegal path characters.
for badChar in illegalPathChars do
if c = badChar then
raise(IllegalFileNameChar(path, c))
type PathState =
| Legal
| Illegal of path: string * illegalChar: char

let checkPathForIllegalChars =
let cache = System.Collections.Concurrent.ConcurrentDictionary<string, PathState>()
fun (path: string) ->
match cache.TryGetValue path with
| true, Legal -> ()
| true, Illegal (path, c) -> raise(IllegalFileNameChar(path, c))
| _ ->
let len = path.Length
for i = 0 to len - 1 do
let c = path.[i]

// Determine if this character is disallowed within a path by
// attempting to find it in the array of illegal path characters.
for badChar in illegalPathChars do
if c = badChar then
cache.[path] <- Illegal(path, c)
raise(IllegalFileNameChar(path, c))
cache.[path] <- Legal

// Case sensitive (original behaviour preserved).
let checkSuffix (x:string) (y:string) = x.EndsWith(y,System.StringComparison.Ordinal)

Expand Down