Files

42 lines
1.5 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2023 - 2026 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
//
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
//
//===----------------------------------------------------------------------===//
#if compiler(>=6.2) && !$Embedded
@available(SwiftStdlib 6.2, *)
extension BigString: Comparable {
public static func < (left: Self, right: Self) -> Bool {
// FIXME: Implement properly normalized comparisons & hashing.
// This is somewhat tricky as we shouldn't just normalize individual pieces of the string
// split up on random Character boundaries -- Unicode does not promise that
// norm(a + c) == norm(a) + norm(b) in this case.
// To do this properly, we'll probably need to expose new stdlib entry points. :-/
if left.isIdentical(to: right) { return false }
// FIXME: Even if we keep doing characterwise comparisons, we should skip over shared subtrees.
var it1 = left.makeIterator()
var it2 = right.makeIterator()
while true {
switch (it1.next(), it2.next()) {
case (nil, nil): return false
case (nil, .some): return true
case (.some, nil): return false
case let (a?, b?):
if a == b { continue }
return a < b
}
}
}
}
#endif // compiler(>=6.2) && !$Embedded