-
Notifications
You must be signed in to change notification settings - Fork 114
Code cleanup; FilePath separator normalization #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5d1b2fb
Folders for FilePath and Platform sources
milseman 3367b48
Follow test convention of expected as first parameter
milseman 79eed1f
Enforce at construction/mutation that FilePath's separators are norma…
milseman bbb7425
Update Sources/System/FilePath/FilePathParsing.swift
milseman 0d3b479
Update Sources/System/FilePath/FilePathParsing.swift
milseman 6f9fc01
Update Sources/System/FilePath/FilePathParsing.swift
milseman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,108 @@ | ||||||
/* | ||||||
This source file is part of the Swift System open source project | ||||||
|
||||||
Copyright (c) 2020 Apple Inc. and the Swift System project authors | ||||||
Licensed under Apache License v2.0 with Runtime Library Exception | ||||||
|
||||||
See https://swift.org/LICENSE.txt for license information | ||||||
*/ | ||||||
|
||||||
private typealias SystemChar = SystemString.Element | ||||||
|
||||||
// The separator we use internally | ||||||
private var genericSeparator: SystemChar { | ||||||
numericCast(UInt8(ascii: "/")) | ||||||
} | ||||||
|
||||||
// The platform preferred separator | ||||||
// | ||||||
// TODO: Make private | ||||||
private var platformSeparator: SystemChar { | ||||||
#if os(Windows) | ||||||
return numericCast(UInt8(ascii: "\\")) | ||||||
#else | ||||||
return genericSeparator | ||||||
#endif | ||||||
} | ||||||
|
||||||
private func isSeparator(_ c: SystemChar) -> Bool { | ||||||
c == genericSeparator || c == platformSeparator | ||||||
} | ||||||
|
||||||
extension FilePath { | ||||||
// For invariant enforcing/checking. Should always return `nil` on | ||||||
// a fully-formed path | ||||||
private func _trailingSepIdx() -> Storage.Index? { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The abbreviation here seems ad hoc. |
||||||
guard bytes.count > 2 else { return nil } | ||||||
let idx = bytes.index(bytes.endIndex, offsetBy: -2) | ||||||
return isSeparator(bytes[idx]) ? idx : nil | ||||||
} | ||||||
|
||||||
// Enforce invariants by removing a trailing separator. | ||||||
// | ||||||
// Precondition: There is exactly zero or one trailing slashes | ||||||
// | ||||||
// Postcondition: Path is root, or has no trailing separator | ||||||
private mutating func _removeTrailingSeparator() { | ||||||
assert(bytes.last == 0, "even empty paths have null termiantor") | ||||||
defer { assert(_trailingSepIdx() == nil) } | ||||||
|
||||||
guard let sepIdx = _trailingSepIdx() else { return } | ||||||
bytes.remove(at: sepIdx) | ||||||
} | ||||||
|
||||||
// Enforce invariants by normalizing the internal separator representation. | ||||||
// | ||||||
// 1) Normalize all separators to platform-preferred separator | ||||||
// 2) Drop redundant separators | ||||||
// 3) Drop trailing separators | ||||||
// | ||||||
// On Windows, UNC and device paths are allowed to begin with two separators | ||||||
// | ||||||
// The POSIX standard does allow two leading separators to | ||||||
// denote implementation-specific handling, but Darwin and Linux | ||||||
// do not treat these differently. | ||||||
// | ||||||
internal mutating func _normalizeSeparators() { | ||||||
var (writeIdx, readIdx) = (bytes.startIndex, bytes.startIndex) | ||||||
#if os(Windows) | ||||||
// TODO: skip over two leading backslashes | ||||||
#endif | ||||||
|
||||||
while readIdx < bytes.endIndex { | ||||||
assert(writeIdx <= readIdx) | ||||||
|
||||||
let wasSeparator = isSeparator(bytes[readIdx]) | ||||||
if wasSeparator { | ||||||
bytes[readIdx] = platformSeparator | ||||||
} | ||||||
bytes.swapAt(writeIdx, readIdx) | ||||||
bytes.formIndex(after: &writeIdx) | ||||||
bytes.formIndex(after: &readIdx) | ||||||
|
||||||
if readIdx == bytes.endIndex { break } | ||||||
|
||||||
while wasSeparator && isSeparator(bytes[readIdx]) { | ||||||
bytes.formIndex(after: &readIdx) | ||||||
if readIdx == bytes.endIndex { break } | ||||||
} | ||||||
} | ||||||
bytes.removeLast(bytes.distance(from: writeIdx, to: readIdx)) | ||||||
self._removeTrailingSeparator() | ||||||
} | ||||||
|
||||||
internal func _invariantCheck() { | ||||||
#if DEBUG | ||||||
precondition(bytes.last! == 0) | ||||||
|
||||||
// TODO: Should this be a hard trap unconditionally?? | ||||||
precondition(bytes.firstIndex(of: 0) == bytes.count - 1) | ||||||
|
||||||
var normal = self | ||||||
normal._normalizeSeparators() | ||||||
precondition(self == normal) | ||||||
precondition(self._trailingSepIdx() == nil) | ||||||
|
||||||
#endif // DEBUG | ||||||
} | ||||||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is private I think you can remove the TODO.