EnsureCorrectPathDelimiter
Function EnsureCorrectPathDelimiter( const S : string) : string
Example
procedure ScriptEvent(var Value: variant);
begin
Value := EnsureCorrectPathDelimiter('C:/Fictional/Inbound');
// C:\Fictional\Inbound\
end;
Usage
EnsureCorrectPathDelimiter normalises slash direction from a leading web-path heuristic and ensures a trailing delimiter.
Parameters
| Name | Type | Description |
|---|---|---|
S | string, const | Whole path-like string to transform. |
Returns
'' for empty input. Otherwise, a slash-normalised string ending in / when the original begins with / or case-insensitive 'http'; all other input is normalised to backslashes and ends in \.
Behaviour
'C:/Folder/File'becomes'C:\Folder\File\'.'/api\orders'becomes'/api/orders/'.'https:\\example.test\api'starts withhttp, so it becomes'https://example.test/api/'.'relative/path'does not begin/orhttp, so it becomes'relative\path\'.- Existing repeated separators are preserved; only slash direction is changed.
Errors
There is no normal content-dependent exception. Argument conversion and allocation failures can propagate.
Usage notes
Use only when this exact Velox heuristic matches the consumer's contract. For a known Windows path, prefer explicit Windows handling; for a URL, parse and modify the URL path rather than the complete string.
Additional Technical Info
EnsureCorrectPathDelimiter chooses web-style or Windows-style separators from the beginning of S, replaces every separator of the other kind, and ensures one selected delimiter at the end. It is a Velox string heuristic, not a path or URL parser.
The example is source-reviewed and was not executed by the documentation workflow.
Implementation
The function tests S.StartsWith('/') case-sensitively or S.StartsWith('http', True) case-insensitively. The web branch calls Delphi StringReplace(S, '\', '/', [rfReplaceAll]); the other branch replaces every / with \. It then calls IncludeCorrectPathDelimeter, which applies the same leading heuristic and appends the selected delimiter when absent.
Edge cases and quirks
- The web test accepts any case-insensitive prefix
'http', including'httpstuff'; it does not requirehttp://orhttps://. - A URL is treated as one undifferentiated string. A missing trailing delimiter is appended after a query or fragment, so
https://host/path?x=1becomeshttps://host/path?x=1/. Parse structured URLs before modifying their path component. - The function does not validate a scheme, host, drive, UNC share, rootedness, traversal segment or illegal character.
.and..are not resolved, duplicate separators are not collapsed, and environment/configuration tokens are not expanded.- A Windows path deliberately beginning
/is classified as web-style.
Side effects
None.
Performance and concurrency
The whole input can be scanned and copied by StringReplace, making work linear in length. There is no mutable shared state.
Related entries
EnsureWebPathDelimiteralways chooses forward slashes and can add a leading slash.IncludeCorrectPathDelimeterappends a delimiter without converting existing separators.IncludeTrailingPathDelimiterfollows the platform delimiter only and does not use the web heuristic.
External references
- Embarcadero
TStringHelper.StartsWith - Embarcadero
System.SysUtils.StringReplace - Free Pascal
StringReplace- compatible replacement context; the leading heuristic is Velox-owned.