Skip to content

Instantly share code, notes, and snippets.

@rojepp
Created December 30, 2016 23:49
Show Gist options
  • Save rojepp/6d5cfbbc9b1235645df895ab9c51ee7b to your computer and use it in GitHub Desktop.
Save rojepp/6d5cfbbc9b1235645df895ab9c51ee7b to your computer and use it in GitHub Desktop.
open System
open System.IO
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
failwith "apa"
let time f message =
let t = System.Diagnostics.Stopwatch.StartNew()
try f () finally
printf "%s took %dms\n" message t.ElapsedMilliseconds
let times n f message =
time (fun () ->
for i in 1 .. n do
f ()) message
let illegalPathChars2 =
Path.GetInvalidPathChars () |> set
let checkPathForIllegalChars2 (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.
if illegalPathChars2.Contains c then
failwith "apa"
let checkPathForIllegalChars3 =
let chars = new System.Collections.Generic.HashSet<_>(Path.GetInvalidPathChars())
(fun (path:string) ->
let len = path.Length
for i = 0 to len - 1 do
let c = path.[i]
if chars.Contains c then
failwith "apa")
let checkPathForIllegalChars4 =
let invalidChars = new System.Collections.Generic.HashSet<_>(Path.GetInvalidPathChars())
(fun (path:string) ->
let chars = new System.Collections.Generic.HashSet<_>(path)
let intersection = chars.IntersectWith(invalidChars)
if chars.Count > 0 then
failwith "apa")
let checkPathForIllegalChars5 =
let chars = new System.Collections.Generic.HashSet<_>(Path.GetInvalidPathChars())
(fun (path:string) ->
let len = path.Length
for i = 0 to path.Length - 1 do
let c = path.[i]
if chars.Contains c then
failwith "apa")
let checkPathForIllegalChars6 =
let chars = new System.Collections.Generic.HashSet<_>(Path.GetInvalidPathChars())
(fun (path:string) ->
for c in path do
if chars.Contains c then
failwith "apa")
let checkPathForIllegalChars7 =
let chars = new System.Collections.Generic.HashSet<_>(Path.GetInvalidPathChars())
(fun (path:string) ->
if chars.Overlaps path then
failwith "apa")
let checkPathForIllegalChars8 (path:string) =
for c in path do
for badChar in illegalPathChars do
if c = badChar then
failwith "apa"
[<EntryPoint>]
let main argv =
times 100000 (fun () -> checkPathForIllegalChars "c:\\dev\\myproject\\utilities\\longstring with spaces\\filename.fs") "fsharp"
times 100000 (fun () -> checkPathForIllegalChars2 "c:\\dev\\myproject\\utilities\\longstring with spaces\\filename.fs") "FsharpSet"
times 100000 (fun () -> checkPathForIllegalChars3 "c:\\dev\\myproject\\utilities\\longstring with spaces\\filename.fs") "HashSet"
times 100000 (fun () -> checkPathForIllegalChars4 "c:\\dev\\myproject\\utilities\\longstring with spaces\\filename.fs") "Intersect"
times 100000 (fun () -> checkPathForIllegalChars5 "c:\\dev\\myproject\\utilities\\longstring with spaces\\filename.fs") "HashSetBounds"
times 100000 (fun () -> checkPathForIllegalChars6 "c:\\dev\\myproject\\utilities\\longstring with spaces\\filename.fs") "HashSetForIn"
times 100000 (fun () -> checkPathForIllegalChars7 "c:\\dev\\myproject\\utilities\\longstring with spaces\\filename.fs") "HashSetOverlaps"
times 100000 (fun () -> checkPathForIllegalChars8 "c:\\dev\\myproject\\utilities\\longstring with spaces\\filename.fs") "fsharpForIn"
times 100000 (fun () -> checkPathForIllegalChars "c:\\dev\\myproject\\utilities\\longstring with spaces\\filename.fs") "fsharp"
times 100000 (fun () -> checkPathForIllegalChars2 "c:\\dev\\myproject\\utilities\\longstring with spaces\\filename.fs") "FsharpSet"
times 100000 (fun () -> checkPathForIllegalChars3 "c:\\dev\\myproject\\utilities\\longstring with spaces\\filename.fs") "HashSet"
times 100000 (fun () -> checkPathForIllegalChars4 "c:\\dev\\myproject\\utilities\\longstring with spaces\\filename.fs") "Intersect"
times 100000 (fun () -> checkPathForIllegalChars5 "c:\\dev\\myproject\\utilities\\longstring with spaces\\filename.fs") "HashSetBounds"
times 100000 (fun () -> checkPathForIllegalChars6 "c:\\dev\\myproject\\utilities\\longstring with spaces\\filename.fs") "HashSetForIn"
times 100000 (fun () -> checkPathForIllegalChars7 "c:\\dev\\myproject\\utilities\\longstring with spaces\\filename.fs") "HashSetOverlaps"
times 100000 (fun () -> checkPathForIllegalChars8 "c:\\dev\\myproject\\utilities\\longstring with spaces\\filename.fs") "fsharpForIn"
printfn "%A" argv
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment