Files
swift-mirror/stdlib/public/core/StringComparable.swift
Michael Ilseman 8851bac1be [String] Inlining, NFC fast paths, and more.
Add inlinability annotations to restore performance parity with 4.2 String.

Take advantage of known NFC as a fast-path for comparison, and
overhaul comparison dispatch.

RRC improvements and optmizations.
2018-11-04 10:42:41 -08:00

96 lines
3.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
//===----------------------------------------------------------------------===//
import SwiftShims
extension StringProtocol {
@_specialize(where Self == String, R == String)
@_specialize(where Self == String, R == Substring)
@_specialize(where Self == Substring, R == String)
@_specialize(where Self == Substring, R == Substring)
@_effects(readonly)
public static func == <R: StringProtocol>(lhs: Self, rhs: R) -> Bool {
return lhs._slicedGuts.compare(with: rhs._slicedGuts) == .equal
}
@inlinable @inline(__always) // forward to other operator
@_effects(readonly)
public static func != <R: StringProtocol>(lhs: Self, rhs: R) -> Bool {
return !(lhs == rhs)
}
@_specialize(where Self == String, R == String)
@_specialize(where Self == String, R == Substring)
@_specialize(where Self == Substring, R == String)
@_specialize(where Self == Substring, R == Substring)
@_effects(readonly)
public static func < <R: StringProtocol>(lhs: Self, rhs: R) -> Bool {
return lhs._slicedGuts.compare(with: rhs._slicedGuts) == .less
}
@inlinable @inline(__always) // forward to other operator
@_effects(readonly)
public static func > <R: StringProtocol>(lhs: Self, rhs: R) -> Bool {
return rhs < lhs
}
@inlinable @inline(__always) // forward to other operator
@_effects(readonly)
public static func <= <R: StringProtocol>(lhs: Self, rhs: R) -> Bool {
return !(rhs < lhs)
}
@inlinable @inline(__always) // forward to other operator
@_effects(readonly)
public static func >= <R: StringProtocol>(lhs: Self, rhs: R) -> Bool {
return !(lhs < rhs)
}
}
extension String : Equatable {
@inlinable @inline(__always) // For the bitwise comparision
@_effects(readonly)
public static func == (lhs: String, rhs: String) -> Bool {
if lhs._guts.rawBits == rhs._guts.rawBits { return true }
if _fastPath(lhs._guts.isNFCFastUTF8 && rhs._guts.isNFCFastUTF8) {
Builtin.onFastPath() // aggressively inline / optimize
return lhs._guts.withFastUTF8 { nfcSelf in
return rhs._guts.withFastUTF8 { nfcOther in
return _binaryCompare(nfcSelf, nfcOther) == 0
}
}
}
return lhs._slicedGuts.compare(with: rhs._slicedGuts) == .equal
}
}
extension String : Comparable {
@inlinable @inline(__always) // For the bitwise comparision
@_effects(readonly)
public static func < (lhs: String, rhs: String) -> Bool {
if lhs._guts.rawBits == rhs._guts.rawBits { return false }
if _fastPath(lhs._guts.isNFCFastUTF8 && rhs._guts.isNFCFastUTF8) {
Builtin.onFastPath() // aggressively inline / optimize
return lhs._guts.withFastUTF8 { nfcSelf in
return rhs._guts.withFastUTF8 { nfcOther in
return _binaryCompare(nfcSelf, nfcOther) < 0
}
}
}
return lhs._slicedGuts.compare(with: rhs._slicedGuts) == .less
}
}
extension Substring : Equatable {}