Skip to content

Add trimmingSuffix, trimmingPrefix and mutating variants #104

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

Merged
merged 8 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 89 additions & 3 deletions Sources/Algorithms/Trim.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,51 @@
//
// 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(toStartAt:)
//===----------------------------------------------------------------------===//

extension Collection where Self: RangeReplaceableCollection {
@inlinable
public mutating func trimPrefix(
while predicate: (Element) throws -> Bool
) rethrows {
replaceSubrange(startIndex..<endIndex, with: try trimmingPrefix(while: predicate))
}
}

//===----------------------------------------------------------------------===//
// trimming(toStartAt:) / trimmingSuffix(while:)
//===----------------------------------------------------------------------===//

extension BidirectionalCollection {
Expand All @@ -29,8 +74,49 @@ 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 self[startIndex...].startOfSuffix(while: predicate)
return self[startIndex..<end]
}
}

//===----------------------------------------------------------------------===//
// trim(toStartAt:) / trimSuffix(while:)
//===----------------------------------------------------------------------===//

extension BidirectionalCollection where Self: RangeReplaceableCollection {
@inlinable
public mutating func trim(
Copy link
Contributor

Choose a reason for hiding this comment

The 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 trim, right @timvermeulen ?

while predicate: (Element) throws -> Bool
) rethrows {
replaceSubrange(startIndex..<endIndex, with: try trimming(while: predicate))
}

@inlinable
public mutating func trimSuffix(
while predicate: (Element) throws -> Bool
) rethrows {
replaceSubrange(startIndex..<endIndex, with: try trimmingSuffix(while: predicate))
}
}
10 changes: 10 additions & 0 deletions Tests/SwiftAlgorithmsTests/TrimTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,14 @@ final class TrimTests: XCTestCase {
let results_one = [2, 10, 12, 15, 20, 100].trimming { $0.isMultiple(of: 2) }
XCTAssertEqual(results_one, [15])
}

func testTrimmingPrefix() {
let results = [2, 10, 12, 15, 20, 100].trimmingPrefix { $0.isMultiple(of: 2) }
XCTAssertEqual(results, [15, 20, 100])
}

func testTrimmingSuffix() {
let results = [2, 10, 12, 15, 20, 100].trimmingSuffix { $0.isMultiple(of: 2) }
XCTAssertEqual(results, [2, 10, 12, 15])
}
}