Files
swift-mirror/stdlib/public/core/StringHashable.swift
Michael Ilseman 4ab45dfe20 [String] Drop in initial UTF-8 String prototype
This is a giant squashing of a lot of individual changes prototyping a
switch of String in Swift 5 to be natively encoded as UTF-8. It
includes what's necessary for a functional prototype, dropping some
history, but still leaves plenty of history available for future
commits.

My apologies to anyone trying to do code archeology between this
commit and the one prior. This was the lesser of evils.
2018-11-04 10:42:40 -08:00

55 lines
1.7 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 String : Hashable {
/// Hashes the essential components of this value by feeding them into the
/// given hasher.
///
/// - Parameter hasher: The hasher to use when combining the components
/// of this instance.
@inlinable // For pre-normal fast paths
public func hash(into hasher: inout Hasher) {
// TODO(UTF8 perf): pre-normal checks, fast-paths, etc.
_guts._normalizedHash(into: &hasher)
}
}
extension StringProtocol {
/// Hashes the essential components of this value by feeding them into the
/// given hasher.
///
/// - Parameter hasher: The hasher to use when combining the components
/// of this instance.
@inlinable
public func hash(into hasher: inout Hasher) {
unimplemented_utf8()
}
}
extension _StringGuts {
@usableFromInline // @opaque
@inline(never) // slow-path
internal func _normalizedHash(into hasher: inout Hasher) {
// TODO(UTF8 perf): fast-paths, incremental (non-allocating) normalization,
// etc. This approach is very slow.
String(self)._normalize().withUnsafeBytes {
hasher.combine(bytes: $0)
}
hasher.combine(0xFF as UInt8) // terminator
}
}