We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Low level enough to be good to implemented here, especially useful when comparing byte arrays that represent lexicographical keys.
Naive implementation:
extension Array: Comparable where Element: Comparable { public static func < (lhs: [Element], rhs: [Element]) -> Bool { for (leftElement, rightElement) in zip(lhs, rhs) { if leftElement < rightElement { return true } else if leftElement > rightElement { return false } } return lhs.count < rhs.count } }
Sneakier solution: https://github.com/apple/swift/blob/aa3e5904f8ba8bf9ae06d96946774d171074f6e5/stdlib/public/core/StringComparison.swift#L280-L295
// Perform a binary comparison of bytes in memory. Return value is negative if // less, 0 if equal, positive if greater. @_effects(readonly) internal func _binaryCompare<UInt8>( _ lhs: UnsafeBufferPointer<UInt8>, _ rhs: UnsafeBufferPointer<UInt8> ) -> Int { var cmp = Int(truncatingIfNeeded: _swift_stdlib_memcmp( lhs.baseAddress._unsafelyUnwrappedUnchecked, rhs.baseAddress._unsafelyUnwrappedUnchecked, Swift.min(lhs.count, rhs.count))) if cmp == 0 { cmp = lhs.count &- rhs.count } return cmp }
https://github.com/apple/swift/blob/aa3e5904f8ba8bf9ae06d96946774d171074f6e5/stdlib/public/SwiftShims/LibcShims.h#L70-L75
SWIFT_READONLY static inline int _swift_stdlib_memcmp(const void *s1, const void *s2, __swift_size_t n) { extern int memcmp(const void *, const void *, __swift_size_t); return memcmp(s1, s2, n); }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Low level enough to be good to implemented here, especially useful when comparing byte arrays that represent lexicographical keys.
Naive implementation:
Sneakier solution:
https://github.com/apple/swift/blob/aa3e5904f8ba8bf9ae06d96946774d171074f6e5/stdlib/public/core/StringComparison.swift#L280-L295
https://github.com/apple/swift/blob/aa3e5904f8ba8bf9ae06d96946774d171074f6e5/stdlib/public/SwiftShims/LibcShims.h#L70-L75
The text was updated successfully, but these errors were encountered: