-
Notifications
You must be signed in to change notification settings - Fork 446
Add trimmingSuffix
, trimmingPrefix
and mutating variants
#104
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
natecook1000
merged 8 commits into
apple:main
from
fedeci:feature/trimming-prefix-suffix
May 25, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e4218b1
feat: add `trimmingSuffix(while)` and `trimmingPrefix(while)`
fedeci 7b3416e
feat: add mutating variants
fedeci c5bf937
Update mutating variants
fedeci 3369266
Address code review
fedeci 3308846
Add overloads
fedeci 9c2fb04
Add tests
fedeci 18406a2
Add docs
fedeci 8a67ef3
Update Guides
fedeci 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 |
---|---|---|
|
@@ -7,6 +7,94 @@ | |
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// | ||
|
||
//===----------------------------------------------------------------------===// | ||
// trimmingPrefix(while:) | ||
//===----------------------------------------------------------------------===// | ||
|
||
extension Collection { | ||
/// Returns a `SubSequence` formed by discarding all elements at the start | ||
/// of the collection which satisfy the given predicate. | ||
/// | ||
/// This example uses `trimmingPrefix(while:)` to get a substring without the white | ||
/// space at the beginning of the string: | ||
/// | ||
/// let myString = " hello, world " | ||
/// print(myString.trimmingPrefix(while: \.isWhitespace)) // "hello, world " | ||
/// | ||
/// - Parameters: | ||
/// - predicate: A closure which determines if the element should be | ||
/// omitted from the resulting slice. | ||
/// | ||
/// - Complexity: O(*n*), where *n* is the length of this collection. | ||
/// | ||
@inlinable | ||
public func trimmingPrefix( | ||
while predicate: (Element) throws -> Bool | ||
) rethrows -> SubSequence { | ||
let start = try endOfPrefix(while: predicate) | ||
return self[start..<endIndex] | ||
} | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// trimPrefix(while:) | ||
//===----------------------------------------------------------------------===// | ||
|
||
extension Collection where Self: RangeReplaceableCollection { | ||
/// Mutates a `Collection` by discarding all elements at the start | ||
/// of it which satisfy the given predicate. | ||
/// | ||
/// This example uses `trimPrefix(while:)` to remove the white | ||
/// space at the beginning of the string: | ||
/// | ||
/// let myString = " hello, world " | ||
/// myString.trimPrefix(while: \.isWhitespace) | ||
/// print(myString) // "hello, world " | ||
/// | ||
/// - Parameters: | ||
/// - predicate: A closure which determines if the element should be | ||
/// removed from the string. | ||
/// | ||
/// - Complexity: O(*n*), where *n* is the length of this collection. | ||
/// | ||
@inlinable | ||
@_disfavoredOverload | ||
public mutating func trimPrefix( | ||
fedeci marked this conversation as resolved.
Show resolved
Hide resolved
|
||
while predicate: (Element) throws -> Bool | ||
) rethrows { | ||
let end = try endOfPrefix(while: predicate) | ||
removeSubrange(startIndex..<end) | ||
} | ||
} | ||
|
||
extension Collection where Self == Self.SubSequence { | ||
/// Mutates a `Collection` by discarding all elements at the start | ||
/// of it which satisfy the given predicate. | ||
/// | ||
/// This example uses `trimPrefix(while:)` to remove the white | ||
/// space at the beginning of the string: | ||
/// | ||
/// let myString = " hello, world " | ||
/// myString.trimPrefix(while: \.isWhitespace) | ||
/// print(myString) // "hello, world " | ||
/// | ||
/// - Parameters: | ||
/// - predicate: A closure which determines if the element should be | ||
/// removed from the string. | ||
/// | ||
/// - Complexity: O(*n*), where *n* is the length of this collection. | ||
/// | ||
@inlinable | ||
public mutating func trimPrefix( | ||
while predicate: (Element) throws -> Bool | ||
) rethrows { | ||
self = try trimmingPrefix(while: predicate) | ||
} | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// trimming(while:) / trimmingSuffix(while:) | ||
//===----------------------------------------------------------------------===// | ||
|
||
extension BidirectionalCollection { | ||
|
@@ -29,8 +117,132 @@ extension BidirectionalCollection { | |
public func trimming( | ||
while predicate: (Element) throws -> Bool | ||
) rethrows -> SubSequence { | ||
let start = try endOfPrefix(while: predicate) | ||
let end = try self[start...].startOfSuffix(while: predicate) | ||
return self[start..<end] | ||
return try trimmingPrefix(while: predicate).trimmingSuffix(while: predicate) | ||
} | ||
|
||
/// Returns a `SubSequence` formed by discarding all elements at the end | ||
/// of the collection which satisfy the given predicate. | ||
/// | ||
/// This example uses `trimmingSuffix(while:)` to get a substring without the white | ||
/// space at the end of the string: | ||
/// | ||
/// let myString = " hello, world " | ||
/// print(myString.trimmingSuffix(while: \.isWhitespace)) // " hello, world" | ||
/// | ||
/// - Parameters: | ||
/// - predicate: A closure which determines if the element should be | ||
/// omitted from the resulting slice. | ||
/// | ||
/// - Complexity: O(*n*), where *n* is the length of this collection. | ||
/// | ||
@inlinable | ||
public func trimmingSuffix( | ||
while predicate: (Element) throws -> Bool | ||
) rethrows -> SubSequence { | ||
let end = try startOfSuffix(while: predicate) | ||
return self[startIndex..<end] | ||
} | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// trim(while:) / trimSuffix(while:) | ||
//===----------------------------------------------------------------------===// | ||
|
||
extension BidirectionalCollection where Self: RangeReplaceableCollection { | ||
/// Mutates a `BidirectionalCollection` by discarding all elements at the start | ||
/// and at the end of it which satisfy the given predicate. | ||
/// | ||
/// This example uses `trim(while:)` to remove the white | ||
/// space at the beginning of the string: | ||
/// | ||
/// let myString = " hello, world " | ||
/// myString.trim(while: \.isWhitespace) | ||
/// print(myString) // "hello, world" | ||
/// | ||
/// - Parameters: | ||
/// - predicate: A closure which determines if the element should be | ||
/// removed from the string. | ||
/// | ||
/// - Complexity: O(*n*), where *n* is the length of this collection. | ||
/// | ||
@inlinable | ||
@_disfavoredOverload | ||
public mutating func trim( | ||
while predicate: (Element) throws -> Bool | ||
) rethrows { | ||
replaceSubrange(startIndex..<endIndex, with: try trimming(while: predicate)) | ||
} | ||
|
||
/// Mutates a `BidirectionalCollection` by discarding all elements at the end | ||
/// of it which satisfy the given predicate. | ||
/// | ||
/// This example uses `trimSuffix(while:)` to remove the white | ||
/// space at the beginning of the string: | ||
/// | ||
/// let myString = " hello, world " | ||
/// myString.trimSuffix(while: \.isWhitespace) | ||
/// print(myString) // " hello, world" | ||
/// | ||
/// - Parameters: | ||
/// - predicate: A closure which determines if the element should be | ||
/// removed from the string. | ||
/// | ||
/// - Complexity: O(*n*), where *n* is the length of this collection. | ||
/// | ||
@inlinable | ||
@_disfavoredOverload | ||
public mutating func trimSuffix( | ||
while predicate: (Element) throws -> Bool | ||
) rethrows { | ||
let start = try startOfSuffix(while: predicate) | ||
removeSubrange(start..<endIndex) | ||
} | ||
} | ||
|
||
extension BidirectionalCollection where Self == Self.SubSequence { | ||
/// Mutates a `BidirectionalCollection` by discarding all elements at the start | ||
/// and at the end of it which satisfy the given predicate. | ||
/// | ||
/// This example uses `trim(while:)` to remove the white | ||
/// space at the beginning of the string: | ||
/// | ||
/// let myString = " hello, world " | ||
/// myString.trim(while: \.isWhitespace) | ||
/// print(myString) // "hello, world" | ||
/// | ||
/// - Parameters: | ||
/// - predicate: A closure which determines if the element should be | ||
/// removed from the string. | ||
/// | ||
/// - Complexity: O(*n*), where *n* is the length of this collection. | ||
/// | ||
@inlinable | ||
public mutating func trim( | ||
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. For what I understood from the issue we should also add an unconstrained version of |
||
while predicate: (Element) throws -> Bool | ||
) rethrows { | ||
self = try trimming(while: predicate) | ||
} | ||
|
||
/// Mutates a `BidirectionalCollection` by discarding all elements at the end | ||
/// of it which satisfy the given predicate. | ||
/// | ||
/// This example uses `trimSuffix(while:)` to remove the white | ||
/// space at the beginning of the string: | ||
/// | ||
/// let myString = " hello, world " | ||
/// myString.trimSuffix(while: \.isWhitespace) | ||
/// print(myString) // " hello, world" | ||
/// | ||
/// - Parameters: | ||
/// - predicate: A closure which determines if the element should be | ||
/// removed from the string. | ||
/// | ||
/// - Complexity: O(*n*), where *n* is the length of this collection. | ||
/// | ||
@inlinable | ||
public mutating func trimSuffix( | ||
fedeci marked this conversation as resolved.
Show resolved
Hide resolved
|
||
while predicate: (Element) throws -> Bool | ||
) rethrows { | ||
self = try trimmingSuffix(while: predicate) | ||
} | ||
} |
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.
I added
0.3.0
supposing that this will be released in a next minor version, but I am not sure.