Skip to content

Commit 427a0b6

Browse files
memoize Filename.checkPathForIllegalChars
1 parent 81eb5b7 commit 427a0b6

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/utils/filename.fs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,28 @@ let illegalPathChars =
1515
let chars = Path.GetInvalidPathChars ()
1616
chars
1717

18-
let checkPathForIllegalChars (path:string) =
19-
let len = path.Length
20-
for i = 0 to len - 1 do
21-
let c = path.[i]
18+
type private PathState =
19+
| Legal
20+
| Illegal of path: string * illegalChar: char
21+
22+
let private checkPathForIllegalChars =
23+
let cache = System.Collections.Concurrent.ConcurrentDictionary<string, PathState>()
24+
fun (path: string) ->
25+
match cache.TryGetValue path with
26+
| true, Legal -> ()
27+
| true, Illegal (path, c) -> raise(IllegalFileNameChar(path, c))
28+
| _ ->
29+
let len = path.Length
30+
for i = 0 to len - 1 do
31+
let c = path.[i]
2232

23-
// Determine if this character is disallowed within a path by
24-
// attempting to find it in the array of illegal path characters.
25-
for badChar in illegalPathChars do
26-
if c = badChar then
27-
raise(IllegalFileNameChar(path, c))
33+
// Determine if this character is disallowed within a path by
34+
// attempting to find it in the array of illegal path characters.
35+
for badChar in illegalPathChars do
36+
if c = badChar then
37+
cache.[path] <- Illegal(path, c)
38+
raise(IllegalFileNameChar(path, c))
39+
cache.[path] <- Legal
2840

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

0 commit comments

Comments
 (0)