Skip to content

Commit 7bc18c0

Browse files
committed
move String extensions to shared location now that we're using it in multiple spots
1 parent 782a235 commit 7bc18c0

2 files changed

Lines changed: 45 additions & 45 deletions

File tree

Sources/ArgumentParser/Completions/CompletionsGenerator.swift

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -175,51 +175,6 @@ extension String {
175175

176176
return result
177177
}
178-
179-
func firstMatch(
180-
of match: Self,
181-
at startIndex: Self.Index
182-
) -> (start: Self.Index, end: Self.Index)? {
183-
guard !match.isEmpty else { return nil }
184-
guard match.count <= self.count else { return nil }
185-
186-
var startIndex = startIndex
187-
while startIndex < self.endIndex {
188-
// Check if theres a match.
189-
if let endIndex = self.matches(match, at: startIndex) {
190-
// Return the match.
191-
return (startIndex, endIndex)
192-
}
193-
194-
// Move to the next of index.
195-
self.formIndex(after: &startIndex)
196-
}
197-
198-
return nil
199-
}
200-
201-
func matches(
202-
_ match: Self,
203-
at startIndex: Self.Index
204-
) -> Self.Index? {
205-
var selfIndex = startIndex
206-
var matchIndex = match.startIndex
207-
208-
while true {
209-
// Only continue checking if there is more match to check
210-
guard matchIndex < match.endIndex else { return selfIndex }
211-
212-
// Exit early if there is no more "self" to check.
213-
guard selfIndex < self.endIndex else { return nil }
214-
215-
// Check match and self are the the same.
216-
guard self[selfIndex] == match[matchIndex] else { return nil }
217-
218-
// Move to the next pair of indices.
219-
self.formIndex(after: &selfIndex)
220-
match.formIndex(after: &matchIndex)
221-
}
222-
}
223178
}
224179

225180
extension CommandInfoV0 {

Sources/ArgumentParser/Utilities/StringExtensions.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,49 @@ extension StringProtocol where SubSequence == Substring {
255255
var nonEmpty: Self? {
256256
isEmpty ? nil : self
257257
}
258+
259+
func firstMatch(
260+
of match: Self,
261+
at startIndex: Self.Index
262+
) -> (start: Self.Index, end: Self.Index)? {
263+
guard !match.isEmpty else { return nil }
264+
guard match.count <= self.count else { return nil }
265+
266+
var startIndex = startIndex
267+
while startIndex < self.endIndex {
268+
// Check if there's a match.
269+
if let endIndex = self.matches(match, at: startIndex) {
270+
// Return the match.
271+
return (startIndex, endIndex)
272+
}
273+
274+
// Move to the next index.
275+
self.formIndex(after: &startIndex)
276+
}
277+
278+
return nil
279+
}
280+
281+
func matches(
282+
_ match: Self,
283+
at startIndex: Self.Index
284+
) -> Self.Index? {
285+
var selfIndex = startIndex
286+
var matchIndex = match.startIndex
287+
288+
while true {
289+
// Only continue checking if there is more match to check
290+
guard matchIndex < match.endIndex else { return selfIndex }
291+
292+
// Exit early if there is no more "self" to check.
293+
guard selfIndex < self.endIndex else { return nil }
294+
295+
// Check match and self are the same.
296+
guard self[selfIndex] == match[matchIndex] else { return nil }
297+
298+
// Move to the next pair of indices.
299+
self.formIndex(after: &selfIndex)
300+
match.formIndex(after: &matchIndex)
301+
}
302+
}
258303
}

0 commit comments

Comments
 (0)