Skip to content

Commit e010406

Browse files
committed
more foundation removal, use existing code in the repo
1 parent 7bc18c0 commit e010406

3 files changed

Lines changed: 44 additions & 31 deletions

File tree

Sources/ArgumentParser/Completions/CompletionsGenerator.swift

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -152,29 +152,6 @@ extension String {
152152
self.replacing("-", with: "_")
153153
}
154154

155-
func replacing(_ old: Self, with new: Self) -> Self {
156-
guard !old.isEmpty else { return self }
157-
158-
var result = ""
159-
var startIndex = self.startIndex
160-
161-
// Look for occurrences of the old string.
162-
while let matchRange = self.firstMatch(of: old, at: startIndex) {
163-
// Add the substring before the match.
164-
result.append(contentsOf: self[startIndex..<matchRange.start])
165-
166-
// Add the replacement string.
167-
result.append(contentsOf: new)
168-
169-
// Move past the matched portion.
170-
startIndex = matchRange.end
171-
}
172-
173-
// No more matches found, add the rest of the string.
174-
result.append(contentsOf: self[startIndex..<self.endIndex])
175-
176-
return result
177-
}
178155
}
179156

180157
extension CommandInfoV0 {

Sources/ArgumentParser/Usage/CommandSearcher.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ import Foundation
2727

2828
extension String {
2929
/// Find the range of a substring (case-insensitive).
30-
fileprivate func rangeOfSubstring(_ substring: String) -> Range<String.Index>? {
30+
fileprivate func rangeOfSubstring(_ substring: String) -> Range<String.Index>?
31+
{
3132
#if canImport(FoundationEssentials)
3233
// FoundationEssentials doesn't include String.range(of:)
3334
// Use the existing firstMatch implementation from CompletionsGenerator.swift
3435
let lowercased = self.lowercased()
3536
let lowercasedSubstring = substring.lowercased()
3637

37-
guard let match = lowercased.firstMatch(of: lowercasedSubstring, at: lowercased.startIndex) else {
38+
guard
39+
let match = lowercased.firstMatch(
40+
of: lowercasedSubstring, at: lowercased.startIndex)
41+
else {
3842
return nil
3943
}
4044

@@ -75,10 +79,17 @@ enum ANSICode {
7579
let searchRange = searchStartIndex..<text.endIndex
7680
let lowercasedSearchRange = String(lowercasedText[searchRange])
7781

78-
if let matchRange = lowercasedSearchRange.rangeOfSubstring(lowercasedTerm) {
82+
if let matchRange = lowercasedSearchRange.rangeOfSubstring(lowercasedTerm)
83+
{
7984
// Convert the match range from lowercased text to original text
80-
let matchStart = text.index(searchStartIndex, offsetBy: lowercasedSearchRange.distance(from: lowercasedSearchRange.startIndex, to: matchRange.lowerBound))
81-
let matchEnd = text.index(searchStartIndex, offsetBy: lowercasedSearchRange.distance(from: lowercasedSearchRange.startIndex, to: matchRange.upperBound))
85+
let matchStart = text.index(
86+
searchStartIndex,
87+
offsetBy: lowercasedSearchRange.distance(
88+
from: lowercasedSearchRange.startIndex, to: matchRange.lowerBound))
89+
let matchEnd = text.index(
90+
searchStartIndex,
91+
offsetBy: lowercasedSearchRange.distance(
92+
from: lowercasedSearchRange.startIndex, to: matchRange.upperBound))
8293

8394
// Add text before the match
8495
result += text[searchStartIndex..<matchStart]
@@ -413,9 +424,7 @@ struct CommandSearcher {
413424
}
414425

415426
// Replace newlines with spaces for display
416-
snippet = snippet.replacingOccurrences(of: "\n", with: " ")
417-
.replacingOccurrences(of: "\\s+", with: " ", options: String.CompareOptions.regularExpression)
418-
427+
snippet = snippet.replacing("\n", with: " ")
419428
return snippet
420429
}
421430

Sources/ArgumentParser/Utilities/StringExtensions.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,31 @@ extension StringProtocol where SubSequence == Substring {
300300
match.formIndex(after: &matchIndex)
301301
}
302302
}
303+
304+
}
305+
306+
extension String {
307+
func replacing(_ old: Self, with new: Self) -> Self {
308+
guard !old.isEmpty else { return self }
309+
310+
var result = ""
311+
var startIndex = self.startIndex
312+
313+
// Look for occurrences of the old string.
314+
while let matchRange = self.firstMatch(of: old, at: startIndex) {
315+
// Add the substring before the match.
316+
result.append(contentsOf: self[startIndex..<matchRange.start])
317+
318+
// Add the replacement string.
319+
result.append(contentsOf: new)
320+
321+
// Move past the matched portion.
322+
startIndex = matchRange.end
323+
}
324+
325+
// No more matches found, add the rest of the string.
326+
result.append(contentsOf: self[startIndex..<self.endIndex])
327+
328+
return result
329+
}
303330
}

0 commit comments

Comments
 (0)