1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
//===--- Utils.swift ------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 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
//
//===----------------------------------------------------------------------===//
extension Dictionary {
@inline(__always)
mutating func withValue<R>(
for key: Key, default defaultValue: Value, body: (inout Value) throws -> R
) rethrows -> R {
try body(&self[key, default: defaultValue])
}
mutating func insertValue(
_ newValue: @autoclosure () -> Value, for key: Key
) -> Bool {
if self[key] == nil {
self[key] = newValue()
return true
}
return false
}
}
extension Sequence {
func sorted<T: Comparable>(by keyPath: KeyPath<Element, T>) -> [Element] {
sorted(by: { $0[keyPath: keyPath] < $1[keyPath: keyPath] })
}
}
extension String {
init(utf8 buffer: UnsafeRawBufferPointer) {
guard !buffer.isEmpty else {
self = ""
return
}
self = String(unsafeUninitializedCapacity: buffer.count,
initializingUTF8With: { dest in
_ = dest.initialize(from: buffer)
return buffer.count
})
}
init(utf8 buffer: UnsafeBufferPointer<UInt8>) {
self.init(utf8: UnsafeRawBufferPointer(buffer))
}
init(utf8 slice: Slice<UnsafeRawBufferPointer>) {
self = String(utf8: .init(rebasing: slice))
}
init(utf8 buffer: ByteScanner.Bytes) {
self = buffer.withUnsafeBytes(String.init(utf8:))
}
func scanningUTF8<R>(_ scan: (inout ByteScanner) throws -> R) rethrows -> R {
var tmp = self
return try tmp.withUTF8 { utf8 in
var scanner = ByteScanner(utf8)
return try scan(&scanner)
}
}
func tryDropPrefix(_ prefix: String) -> String? {
guard hasPrefix(prefix) else { return nil }
return String(dropFirst(prefix.count))
}
func escaped(addQuotesIfNeeded: Bool) -> String {
scanningUTF8 { scanner in
var needsQuotes = false
let result = scanner.consumeWhole { consumer in
switch consumer.peek {
case "\\", "\"":
consumer.append(Byte(ascii: "\\"))
case " ", "$": // $ is potentially a variable reference
needsQuotes = true
default:
break
}
}
let escaped = result.isUnchanged ? self : String(utf8: result)
return addQuotesIfNeeded && needsQuotes ? "\"\(escaped)\"" : escaped
}
}
var escaped: String {
escaped(addQuotesIfNeeded: true)
}
init(_ str: StaticString) {
self = str.withUTF8Buffer { utf8 in
String(utf8: utf8)
}
}
var isASCII: Bool {
// Thanks, @testable interface!
_classify()._isASCII
}
/// A more efficient version of replacingOccurrences(of:with:)/replacing(_:with:),
/// since the former involves bridging, and the latter currently has no fast
/// paths for strings.
func replacing(_ other: String, with replacement: String) -> String {
guard !other.isEmpty else {
return self
}
guard isASCII else {
// Not ASCII, fall back to slower method.
return replacingOccurrences(of: other, with: replacement)
}
let otherUTF8 = other.utf8
return scanningUTF8 { scanner in
let bytes = scanner.consumeWhole { consumer in
guard otherUTF8.count <= consumer.remaining.count else {
// If there's no way we can eat the string, eat the remaining.
consumer.eatRemaining()
return
}
while consumer.trySkip(otherUTF8) {
consumer.append(utf8: replacement)
}
}
return bytes.isUnchanged ? self : String(utf8: bytes)
}
}
}
|