-
Notifications
You must be signed in to change notification settings - Fork 340
Only use FoundationEssentials
if available
#749
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
base: main
Are you sure you want to change the base?
Changes from all commits
430389f
b5bb88f
6832850
ecad063
da0d3b6
787eed8
f1fbc9f
ed38120
a38abc1
2e8148c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ struct _WrappedParsableCommand<P: ParsableArguments>: ParsableCommand { | |
|
||
// If the type is named something like "TransformOptions", we only want | ||
// to use "transform" as the command name. | ||
if let optionsRange = name.range(of: "_options"), | ||
if let optionsRange = name._range(of: "_options"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @natecook1000 what is your preferred course of action? Keep the copied implementation here? |
||
optionsRange.upperBound == name.endIndex | ||
{ | ||
return String(name[..<optionsRange.lowerBound]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
|
||
extension BidirectionalCollection { | ||
// Equal to calling `index(&idx, offsetBy: -other.count)` with just one loop | ||
func _index<S: BidirectionalCollection>(_ index: Index, backwardsOffsetByCountOf other: S) -> Index? { | ||
var idx = index | ||
var otherIdx = other.endIndex | ||
while otherIdx > other.startIndex { | ||
guard idx > startIndex else { | ||
// other.count > self.count: bail | ||
return nil | ||
} | ||
other.formIndex(before: &otherIdx) | ||
formIndex(before: &idx) | ||
} | ||
return idx | ||
} | ||
|
||
func _range<S: BidirectionalCollection>(of other: S, anchored: Bool = false, backwards: Bool = false) -> Range<Index>? where S.Element == Element, Element : Equatable { | ||
var result: Range<Index>? = nil | ||
var fromLoc: Index | ||
var toLoc: Index | ||
if backwards { | ||
guard let idx = _index(endIndex, backwardsOffsetByCountOf: other) else { | ||
// other.count > string.count: bail | ||
return nil | ||
} | ||
fromLoc = idx | ||
|
||
toLoc = anchored ? fromLoc : startIndex | ||
} else { | ||
fromLoc = startIndex | ||
if anchored { | ||
toLoc = fromLoc | ||
} else { | ||
guard let idx = _index(endIndex, backwardsOffsetByCountOf: other) else { | ||
return nil | ||
} | ||
toLoc = idx | ||
} | ||
} | ||
|
||
let delta = fromLoc <= toLoc ? 1 : -1 | ||
|
||
while true { | ||
var str1Index = fromLoc | ||
var str2Index = other.startIndex | ||
|
||
while str2Index < other.endIndex && str1Index < endIndex { | ||
if self[str1Index] != other[str2Index] { | ||
break | ||
} | ||
formIndex(after: &str1Index) | ||
other.formIndex(after: &str2Index) | ||
} | ||
|
||
if str2Index == other.endIndex { | ||
result = fromLoc..<str1Index | ||
break | ||
} | ||
|
||
if fromLoc == toLoc { | ||
break | ||
} | ||
|
||
formIndex(&fromLoc, offsetBy: delta) | ||
} | ||
|
||
return result | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this
canImport
check necessary?replacing(_:with:)
comes from the Swift standard library (via_StringProcessing
), notFoundationEssentials
. We may be able to do away with the other branch on these altogether.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for all the branches where the result is the same, we should remove the conditional code and use the stdlib version theres no need to use the variants from Foundation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is the stdlib version. On macOS this is only available on newer macOS versions. But since on macOS you anyhow have foundation I chose this route.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd personally prefer manually backdeploying these stdlib functions then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rauhul Should I try to copy an implementation from
_StringProcessing
? Alternatively, is there something like#if canImport(SwiftStdlib, 6.0)
that we could use here?