mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Cherry pick of https://github.com/swiftlang/swift/pull/83184 <!-- If this pull request is targeting a release branch, please fill out the following form: https://github.com/swiftlang/.github/blob/main/PULL_REQUEST_TEMPLATE/release.md?plain=1 Otherwise, replace this comment with a description of your changes and rationale. Provide links to external references/discussions if appropriate. If this pull request resolves any GitHub issues, link them like so: Resolves <link to issue>, resolves <link to another issue>. For more information about linking a pull request to an issue, see: https://docs.github.com/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue --> <!-- Before merging this pull request, you must run the Swift continuous integration tests. For information about triggering CI builds via @swift-ci, see: https://github.com/apple/swift/blob/main/docs/ContinuousIntegration.md#swift-ci Thank you for your contribution to Swift! --> Co-authored-by: Alex Martini <amartini@apple.com>
111 lines
2.8 KiB
Swift
111 lines
2.8 KiB
Swift
// TODO: comment header
|
|
|
|
|
|
@available(SwiftStdlib 6.2, *)
|
|
extension UTF8Span {
|
|
/// Whether this span has the same bytes as `other`.
|
|
///
|
|
/// - Complexity: O(n)
|
|
@_alwaysEmitIntoClient
|
|
public func bytesEqual(to other: some Sequence<UInt8>) -> Bool {
|
|
unsafe _withUnsafeBufferPointer { unsafe $0.elementsEqual(other) }
|
|
}
|
|
|
|
/// Whether this span has the same `Unicode.Scalar`s as `other`.
|
|
///
|
|
/// - Complexity: O(n)
|
|
@_alwaysEmitIntoClient
|
|
public func unicodeScalarsEqual(
|
|
to other: some Sequence<Unicode.Scalar>
|
|
) -> Bool {
|
|
// TODO: We don't need to decode our code units, we can just match
|
|
// against their scalars' encoded bytes
|
|
|
|
var scalars = makeUnicodeScalarIterator()
|
|
var otherScalars = other.makeIterator()
|
|
while let s = scalars.next() {
|
|
guard let otherS = otherScalars.next(), s == otherS else {
|
|
return false
|
|
}
|
|
}
|
|
guard scalars.next() == nil else {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
/// Whether this span has the same `Character`s as `other`.
|
|
///
|
|
/// - Complexity: O(n)
|
|
@_unavailableInEmbedded
|
|
@_alwaysEmitIntoClient
|
|
public func charactersEqual(
|
|
to other: some Sequence<Character>
|
|
) -> Bool {
|
|
var chars = makeCharacterIterator()
|
|
var otherChars = other.makeIterator()
|
|
while let c = chars.next() {
|
|
guard let otherC = otherChars.next(), c == otherC else {
|
|
return false
|
|
}
|
|
}
|
|
guard chars.next() == nil else {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
|
|
@available(SwiftStdlib 6.2, *)
|
|
extension UTF8Span {
|
|
/// Whether `self` is equivalent to `other` under Unicode Canonical
|
|
/// Equivalence.
|
|
///
|
|
/// - Complexity: O(n)
|
|
public func isCanonicallyEquivalent(
|
|
to other: UTF8Span
|
|
) -> Bool {
|
|
unsafe self._withUnsafeBufferPointer { selfBufPtr in
|
|
unsafe other._withUnsafeBufferPointer { otherBufPtr in
|
|
unsafe _stringCompareFastUTF8(
|
|
selfBufPtr,
|
|
otherBufPtr,
|
|
expecting: .equal,
|
|
bothNFC: self.isKnownNFC && other.isKnownNFC)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Whether `self` orders less than `other` under Unicode Canonical
|
|
/// Equivalence using normalized code-unit order (in NFC).
|
|
///
|
|
/// - Complexity: O(n)
|
|
public func isCanonicallyLessThan(
|
|
_ other: UTF8Span
|
|
) -> Bool {
|
|
unsafe self._withUnsafeBufferPointer { selfBufPtr in
|
|
unsafe other._withUnsafeBufferPointer { otherBufPtr in
|
|
unsafe _stringCompareFastUTF8(
|
|
selfBufPtr,
|
|
otherBufPtr,
|
|
expecting: .less,
|
|
bothNFC: self.isKnownNFC && other.isKnownNFC)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// // FIXME: remove
|
|
// @available(SwiftStdlib 6.2, *)
|
|
// extension UTF8Span {
|
|
// public static func ~=(_ lhs: StaticString, _ rhs: UTF8Span) -> Bool {
|
|
// return lhs.withUTF8Buffer { str in
|
|
// rhs._withUnsafeBufferPointer { span in
|
|
// str.elementsEqual(span)
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
|