From 1ef2ae7216791b6cdfc610859a9b05151d33ade6 Mon Sep 17 00:00:00 2001 From: Steffan Andrews Date: Tue, 26 Jul 2022 12:10:05 -0700 Subject: [PATCH] `Collection`: Added `longest()` and `longestIndex()` --- .../OTCore/Extensions/Swift/Collections.swift | 41 +++++++++++++++++++ .../Extensions/Swift/Collections Tests.swift | 26 ++++++++++++ 2 files changed, 67 insertions(+) diff --git a/Sources/OTCore/Extensions/Swift/Collections.swift b/Sources/OTCore/Extensions/Swift/Collections.swift index 60fcf55..55ea5d7 100644 --- a/Sources/OTCore/Extensions/Swift/Collections.swift +++ b/Sources/OTCore/Extensions/Swift/Collections.swift @@ -859,6 +859,47 @@ extension Collection where Element: StringProtocol { } } +// MARK: - longest() / longestIndex() + +extension Collection where Element: StringProtocol { + + /// **OTCore:** + /// Returns the longest string in the collection. + /// If more than one element are the same length as the longest element, the first will be returned. + /// - complexity: O(*n*) + @inlinable @_disfavoredOverload + public func longest() -> Element? { + + guard let idx = longestIndex() else { return nil } + return self[idx] + + } + + /// **OTCore:** + /// Returns the index of the longest string in the collection. + /// If more than one element are the same length as the longest element, the first will be returned. + /// - complexity: O(*n*) + @inlinable @_disfavoredOverload + public func longestIndex() -> Index? { + + guard !isEmpty else { return nil } + + var longestIndex: Index = startIndex + var longestLength: Int = self[startIndex].count + + for index in indices { + let elementLength = self[index].count + if elementLength > longestLength { + longestIndex = index + longestLength = elementLength + } + } + + return longestIndex + + } +} + // MARK: - stringValueArrayLiteral extension Collection where Element: BinaryInteger { diff --git a/Tests/OTCoreTests/Extensions/Swift/Collections Tests.swift b/Tests/OTCoreTests/Extensions/Swift/Collections Tests.swift index 20fbe82..1c3b3f3 100644 --- a/Tests/OTCoreTests/Extensions/Swift/Collections Tests.swift +++ b/Tests/OTCoreTests/Extensions/Swift/Collections Tests.swift @@ -1165,6 +1165,8 @@ class Extensions_Swift_Collections_Tests: XCTestCase { } + // MARK: - shortest() / shortestIndex() + func testStringProtocol_Shortest() { XCTAssertEqual([String]().shortest(), nil) @@ -1187,6 +1189,30 @@ class Extensions_Swift_Collections_Tests: XCTestCase { } + // MARK: - longest() / longestIndex() + + func testStringProtocol_Longest() { + + XCTAssertEqual([String]().longest(), nil) + XCTAssertEqual([""].longest(), "") + XCTAssertEqual(["A"].longest(), "A") + XCTAssertEqual(["A", "1", "xy", "", "abc"].longest(), "abc") + XCTAssertEqual(["abc", "123", "A", "1", "xyz"].longest(), "abc") + XCTAssertEqual(["123", "abc", "1", "A", "xyz"].longest(), "123") + + } + + func testStringProtocol_LongestIndex() { + + XCTAssertEqual([String]().longestIndex(), nil) + XCTAssertEqual([""].longestIndex(), 0) + XCTAssertEqual(["A"].longestIndex(), 0) + XCTAssertEqual(["A", "1", "xy", "", "abc"].longestIndex(), 4) + XCTAssertEqual(["abc", "123", "A", "1", "xyz"].longestIndex(), 0) + XCTAssertEqual(["123", "abc", "1", "A", "xyz"].longestIndex(), 0) + + } + // MARK: - .stringValueArrayLiteral func teststringValueArrayLiteral() {