Skip to content

[string] Comparison bug fix: Kelvin #16119

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

Merged
merged 1 commit into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions stdlib/public/core/StringComparison.swift
Original file line number Diff line number Diff line change
Expand Up @@ -937,26 +937,29 @@ extension _UnmanagedString where CodeUnit == UInt8 {
if _fastPath(
other._parseRawScalar(startingFrom: idx).0._isNormalizedSuperASCII
) {
return .less
}
} else {
let selfASCIIChar = UInt16(self[idx])
_sanityCheck(selfASCIIChar != otherCU, "should be different")
if idx+1 == other.count {
return _lexicographicalCompare(selfASCIIChar, otherCU)
}
if _fastPath(other.hasNormalizationBoundary(after: idx)) {
return _lexicographicalCompare(selfASCIIChar, otherCU)
return .less
}

// Rare pathological case, e.g. Kelvin symbol
var selfIterator = _NormalizedCodeUnitIterator(self)
return selfIterator.compare(with: _NormalizedCodeUnitIterator(other))
}

let selfASCIIChar = UInt16(self[idx])
_sanityCheck(selfASCIIChar != otherCU, "should be different")
if idx+1 == other.count {
return _lexicographicalCompare(selfASCIIChar, otherCU)
}
if _fastPath(other.hasNormalizationBoundary(after: idx)) {
return _lexicographicalCompare(selfASCIIChar, otherCU)
}

//
// Otherwise, need to normalize the segment and then compare
//
let selfASCIIChar = UInt16(self[idx])
return _compareStringsPostSuffix(
selfASCIIChar: selfASCIIChar, otherUTF16: other[idx...]
)
selfASCIIChar: selfASCIIChar, otherUTF16WithLeadingASCII: other[idx...]
)
}
}

Expand Down Expand Up @@ -1008,15 +1011,17 @@ extension BidirectionalCollection where Element == UInt16, SubSequence == Self {
}
}

@inline(never) // @outlined
private func _compareStringsPostSuffix(
selfASCIIChar: UInt16,
otherUTF16: _UnmanagedString<UInt16>
otherUTF16WithLeadingASCII: _UnmanagedString<UInt16>
) -> _Ordering {
let otherCU = otherUTF16[0]
let otherCU = otherUTF16WithLeadingASCII[0]
_sanityCheck(otherCU <= 0x7F, "should be ASCII, otherwise no need to call")

let segmentEndIdx = otherUTF16._findNormalizationSegmentEnd(startingFrom: 0)
let segment = otherUTF16[..<segmentEndIdx]
let segmentEndIdx = otherUTF16WithLeadingASCII._findNormalizationSegmentEnd(
startingFrom: 0)
let segment = otherUTF16WithLeadingASCII[..<segmentEndIdx]

// Fast path: If prenormal, we're done.
if _Normalization._prenormalQuickCheckYes(segment) {
Expand Down
3 changes: 3 additions & 0 deletions test/stdlib/StringAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ let tests = [
ComparisonTest(.eq, "\u{0301}", "\u{0341}"),
ComparisonTest(.lt, "\u{0301}", "\u{0954}"),
ComparisonTest(.lt, "\u{0341}", "\u{0954}"),

// (U+212A KELVIN SIGN) normalizes to ASCII "K"
ComparisonTest(.eq, "K", "\u{212A}"),
]

func checkStringComparison(
Expand Down