Created
May 17, 2019 06:16
-
-
Save pragmatrix/fa0666323d7f5da6c40dd52cf48a81ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let [<Literal>] FilenameMaxLen: int = 64 | |
// Note: this is platform dependent, therefore Windows | |
// version of System.IO.Path.GetInvalidFileNameChars() is hardcoded here. | |
let InvalidFilenameCharacters = [| | |
'"'; '<'; '>'; '|'; '\000'; '\001'; '\002'; '\003'; '\004'; '\005'; '\006'; | |
'\007'; '\b'; '\009'; '\010'; '\011'; '\012'; '\013'; '\014'; '\015'; | |
'\016'; '\017'; '\018'; '\019'; '\020'; '\021'; '\022'; '\023'; '\024'; | |
'\025'; '\026'; '\027'; '\028'; '\029'; '\030'; '\031'; ':'; '*'; '?'; | |
'\\'; '/' | |
|] | |
let [<Literal>] FilenameReplacementCharacter = '_' | |
let private InvalidRegexCharacters = Regex.Escape(String(InvalidFilenameCharacters)); | |
// note: this combines multiple occurrences to one match, and so might shorten the | |
// filename. | |
let private InvalidRegex = String.Format(@"([{0}]*\.+$)|([{0}]+)", InvalidRegexCharacters); | |
// TODO: toolbox candidate. | |
let private sanitizeFilename (fn: string) : string = | |
// note: we want to truncate the string _before_ | |
// eliminating invalid characters, because the replacement | |
// algorithm might change. | |
let fn = fn.[0..(min FilenameMaxLen fn.Length)-1] | |
Regex.Replace(fn, InvalidRegex, "_"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment