From 5b88f94451ca655cd19f8326279bbc080c02259e Mon Sep 17 00:00:00 2001 From: Vasily Kirichenko Date: Wed, 26 Oct 2016 12:42:34 +0300 Subject: [PATCH] optimize Filename.checkPathForIllegalChars --- src/utils/filename.fs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/utils/filename.fs b/src/utils/filename.fs index ad494a0fd75..1617563e56b 100644 --- a/src/utils/filename.fs +++ b/src/utils/filename.fs @@ -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() + 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)