Skip to content

Commit 099c8ca

Browse files
committed
Use firstRange instead of implementing firstIndex
We have `firstRange` in the stdlib now, so we don’t need to implement `firstIndex` ourselves.
1 parent 219b11d commit 099c8ca

File tree

3 files changed

+5
-49
lines changed

3 files changed

+5
-49
lines changed

Sources/LanguageServerProtocolJSONRPC/MessageSplitting.swift

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public struct JSONRPCMessageHeader: Hashable {
2525
}
2626
}
2727

28-
extension RandomAccessCollection<UInt8> {
28+
extension RandomAccessCollection<UInt8> where Index == Int {
2929
/// Tries to parse a single message from this collection of bytes.
3030
///
3131
/// If an entire message could be found, returns
@@ -85,36 +85,14 @@ extension RandomAccessCollection<UInt8> {
8585
throw MessageDecodingError.parseError("expected ':' in message header")
8686
}
8787
let valueStart = index(after: keyEnd)
88-
guard let valueEnd = self[valueStart...].firstIndex(of: JSONRPCMessageHeader.separator) else {
88+
guard let valueEnd = self[valueStart...].firstRange(of: JSONRPCMessageHeader.separator)?.startIndex else {
8989
return nil
9090
}
9191

9292
return ((key: self[..<keyEnd], value: self[valueStart..<valueEnd]), self[index(valueEnd, offsetBy: 2)...])
9393
}
9494
}
9595

96-
extension RandomAccessCollection where Element: Equatable {
97-
/// Returns the first index where the specified subsequence appears or nil.
98-
@inlinable
99-
public func firstIndex(of pattern: some RandomAccessCollection<Element>) -> Index? {
100-
if pattern.isEmpty {
101-
return startIndex
102-
}
103-
if count < pattern.count {
104-
return nil
105-
}
106-
107-
var i = startIndex
108-
for _ in 0..<(count - pattern.count + 1) {
109-
if self[i...].starts(with: pattern) {
110-
return i
111-
}
112-
i = self.index(after: i)
113-
}
114-
return nil
115-
}
116-
}
117-
11896
extension UInt8 {
11997
/// *Public for *testing*. Whether this byte is an ASCII whitespace character (isspace).
12098
@inlinable

Tests/BuildSystemIntegrationTests/SwiftPMBuildSystemTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ private func assertArgumentsDoNotContain(
850850
file: StaticString = #filePath,
851851
line: UInt = #line
852852
) {
853-
if let index = arguments.firstIndex(of: pattern) {
853+
if let index = arguments.firstRange(of: pattern)?.startIndex {
854854
XCTFail(
855855
"not-pattern \(pattern) unexpectedly found at \(index) in arguments \(arguments)",
856856
file: file,
@@ -867,12 +867,12 @@ private func assertArgumentsContain(
867867
file: StaticString = #filePath,
868868
line: UInt = #line
869869
) {
870-
guard let index = arguments.firstIndex(of: pattern) else {
870+
guard let index = arguments.firstRange(of: pattern)?.startIndex else {
871871
XCTFail("pattern \(pattern) not found in arguments \(arguments)", file: file, line: line)
872872
return
873873
}
874874

875-
if !allowMultiple, let index2 = arguments[(index + 1)...].firstIndex(of: pattern) {
875+
if !allowMultiple, let index2 = arguments[(index + 1)...].firstRange(of: pattern)?.startIndex {
876876
XCTFail(
877877
"pattern \(pattern) found twice (\(index), \(index2)) in \(arguments)",
878878
file: file,

Tests/LanguageServerProtocolJSONRPCTests/MessageParsingTests.swift

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -188,28 +188,6 @@ final class MessageParsingTests: XCTestCase {
188188
checkError("C\r\n", MessageDecodingError.parseError("expected ':' in message header"))
189189
}
190190

191-
func testFindSubsequence() {
192-
XCTAssertNil([0, 1, 2].firstIndex(of: [3]))
193-
XCTAssertNil([].firstIndex(of: [3]))
194-
XCTAssertNil([0, 1, 2].firstIndex(of: [1, 3]))
195-
XCTAssertNil([0, 1, 2].firstIndex(of: [0, 2]))
196-
XCTAssertNil([0, 1, 2].firstIndex(of: [2, 3]))
197-
XCTAssertNil([0, 1].firstIndex(of: [1, 0]))
198-
XCTAssertNil([0].firstIndex(of: [0, 1]))
199-
200-
XCTAssertEqual([Int]().firstIndex(of: []), 0)
201-
XCTAssertEqual([0].firstIndex(of: []), 0)
202-
XCTAssertEqual([0].firstIndex(of: [0]), 0)
203-
XCTAssertEqual([0, 1].firstIndex(of: [0]), 0)
204-
XCTAssertEqual([0, 1].firstIndex(of: [1]), 1)
205-
206-
XCTAssertEqual([0, 1].firstIndex(of: [0, 1]), 0)
207-
XCTAssertEqual([0, 1, 2, 3].firstIndex(of: [0, 1]), 0)
208-
XCTAssertEqual([0, 1, 2, 3].firstIndex(of: [0, 1, 2]), 0)
209-
XCTAssertEqual([0, 1, 2, 3].firstIndex(of: [1, 2]), 1)
210-
XCTAssertEqual([0, 1, 2, 3].firstIndex(of: [3]), 3)
211-
}
212-
213191
func testIntFromAscii() {
214192
XCTAssertNil(Int(ascii: ""))
215193
XCTAssertNil(Int(ascii: "a"))

0 commit comments

Comments
 (0)