Update regular expression for invalid patterns

* Update regular expression for invalid patterns for efficiency
* Update table title
This commit is contained in:
abraunegg 2024-03-25 09:33:01 +11:00
parent 3c44f7d2d9
commit 35ebdc87d4
2 changed files with 11 additions and 4 deletions

View file

@ -103,7 +103,7 @@ Should a file or folder infringe upon these naming conventions or restrictions,
> [!TIP]
> UTF-16 provides a capability to use UTF-16 characters to work around the restrictions and limitations imposed by Microsoft OneDrive. An example of some replacement characters are below:
> | Standard Character | UTF-16 Replacement Character |
> | Standard Invalid Character | Potential UTF-16 Replacement Character |
> |--------------------|------------------------------|
> | . | (One Dot Leader, `\u2024`) |
> | : | ː (Modifier Letter Triangular Colon, `\u02D0`) |

View file

@ -350,11 +350,18 @@ bool isValidName(string path) {
// Regular expression for invalid patterns
// https://support.microsoft.com/en-us/office/restrictions-and-limitations-in-onedrive-and-sharepoint-64883a5d-228e-48f5-b3d2-eb39e07630fa?ui=en-us&rs=en-us&ad=us#invalidcharacters
// Leading whitespace and trailing whitespace/dot
// Leading whitespace and trailing whitespace
// Invalid characters
auto invalidNameReg = ctRegex!(`^\s.*|^.*[\s\.]$|.*[<>:"\|\?*/\\].*`);
// Trailing dot '.' (not documented above) , however see issue https://github.com/abraunegg/onedrive/issues/2678
auto matchResult = match(itemName, invalidNameReg);
//auto invalidNameReg = ctRegex!(`^\s.*|^.*[\s\.]$|.*[<>:"\|\?*/\\].*`); - original to remove at some point
auto invalidNameReg = ctRegex!(`^\s+|\s$|\.$|[<>:"\|\?*/\\]`); // revised 25/3/2024
// - ^\s+ matches one or more whitespace characters at the start of the string. The + ensures we match one or more whitespaces, making it more efficient than .* for detecting leading whitespaces.
// - \s$ matches a whitespace character at the end of the string. This is more precise than [\s\.]$ because we'll handle the dot separately.
// - \.$ specifically matches a dot character at the end of the string, addressing the requirement to catch trailing dots as invalid.
// - [<>:"\|\?*/\\] matches any single instance of the specified invalid characters: ", *, :, <, >, ?, /, \, |
auto matchResult = match(itemName, invalidNameReg);
if (!matchResult.empty) {
return false;
}