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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 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
//
//===----------------------------------------------------------------------===//
// COW helpers
extension _StringGuts {
internal var nativeCapacity: Int? {
@inline(never)
@_effects(releasenone)
get {
guard hasNativeStorage else { return nil }
return _object.withNativeStorage { $0.capacity }
}
}
internal var nativeUnusedCapacity: Int? {
@inline(never)
@_effects(releasenone)
get {
guard hasNativeStorage else { return nil }
return _object.withNativeStorage { $0.unusedCapacity }
}
}
// If natively stored and uniquely referenced, return the storage's total
// capacity. Otherwise, nil.
internal var uniqueNativeCapacity: Int? {
@inline(never)
@_effects(releasenone)
mutating get {
guard isUniqueNative else { return nil }
return _object.withNativeStorage { $0.capacity }
}
}
// If natively stored and uniquely referenced, return the storage's spare
// capacity. Otherwise, nil.
internal var uniqueNativeUnusedCapacity: Int? {
@inline(never)
@_effects(releasenone)
mutating get {
guard isUniqueNative else { return nil }
return _object.withNativeStorage { $0.unusedCapacity }
}
}
@usableFromInline // @testable
internal var isUniqueNative: Bool {
@inline(__always) mutating get {
// Note: mutating so that self is `inout`.
guard hasNativeStorage else { return false }
defer { _fixLifetime(self) }
var bits: UInt = _object.largeAddressBits
return _isUnique_native(&bits)
}
}
}
// Range-replaceable operation support
extension _StringGuts {
@inline(__always)
internal mutating func updateNativeStorage<R>(
_ body: (__StringStorage) -> R
) -> R {
let (r, cf) = self._object.withNativeStorage {
let r = body($0)
let cf = $0._countAndFlags
return (r, cf)
}
// We need to pick up new count/flags from the modified storage.
self._object._setCountAndFlags(to: cf)
return r
}
@inlinable
internal init(_initialCapacity capacity: Int) {
self.init()
if _slowPath(capacity > _SmallString.capacity) {
self.grow(capacity) // TODO: no factor should be applied
}
}
internal mutating func reserveCapacity(_ n: Int) {
// Check if there's nothing to do
if n <= _SmallString.capacity { return }
if let currentCap = self.uniqueNativeCapacity, currentCap >= n { return }
// Grow
self.grow(n) // TODO: no factor should be applied
}
// Grow to accommodate at least `n` code units
@usableFromInline
internal mutating func grow(_ n: Int) {
defer {
self._invariantCheck()
_internalInvariant(
self.uniqueNativeCapacity != nil && self.uniqueNativeCapacity! >= n)
}
_internalInvariant(
self.uniqueNativeCapacity == nil || self.uniqueNativeCapacity! < n)
// If unique and native, apply a 2x growth factor to avoid problematic
// performance when used in a loop. If one if those doesn't apply, we
// can just use the requested capacity (at least the current utf-8 count).
// TODO: Don't do this! Growth should only happen for append...
let growthTarget: Int
if let capacity = self.uniqueNativeCapacity {
growthTarget = Swift.max(n, capacity * 2)
} else {
growthTarget = Swift.max(n, self.utf8Count)
}
// `isFastUTF8` is not the same as `isNative`. It can include small
// strings or foreign strings that provide contiguous UTF-8 access.
if _fastPath(isFastUTF8) {
let isASCII = self.isASCII
let storage = self.withFastUTF8 {
__StringStorage.create(
initializingFrom: $0,
codeUnitCapacity: growthTarget,
isASCII: isASCII)
}
self = _StringGuts(storage)
return
}
_foreignGrow(growthTarget)
}
@inline(never) // slow-path
private mutating func _foreignGrow(_ n: Int) {
let newString = String(_uninitializedCapacity: n) { buffer in
guard let count = _foreignCopyUTF8(into: buffer) else {
fatalError("String capacity was smaller than required")
}
return count
}
self = newString._guts
}
// Ensure unique native storage with sufficient capacity for the following
// append.
private mutating func prepareForAppendInPlace(
totalCount: Int,
otherUTF8Count otherCount: Int
) {
defer {
_internalInvariant(self.uniqueNativeUnusedCapacity != nil,
"growth should produce uniqueness")
_internalInvariant(self.uniqueNativeUnusedCapacity! >= otherCount,
"growth should produce enough capacity")
}
// See if we can accommodate without growing or copying. If we have
// sufficient capacity, we do not need to grow, and we can skip the copy if
// unique. Otherwise, growth is required.
let sufficientCapacity: Bool
if let unused = self.nativeUnusedCapacity, unused >= otherCount {
sufficientCapacity = true
} else {
sufficientCapacity = false
}
if self.isUniqueNative && sufficientCapacity {
return
}
// If we have to resize anyway, and we fit in smol, we should have made one
_internalInvariant(totalCount > _SmallString.capacity)
// Non-unique storage: just make a copy of the appropriate size, otherwise
// grow like an array.
let growthTarget: Int
if sufficientCapacity {
growthTarget = totalCount
} else {
growthTarget = Swift.max(
totalCount, _growArrayCapacity(nativeCapacity ?? 0))
}
self.grow(growthTarget) // NOTE: this already has exponential growth...
}
internal mutating func append(_ other: _StringGuts) {
if self.isSmall && other.isSmall {
if let smol = _SmallString(self.asSmall, appending: other.asSmall) {
self = _StringGuts(smol)
return
}
}
append(_StringGutsSlice(other))
}
@inline(never)
@_effects(readonly)
private func _foreignConvertedToSmall() -> _SmallString {
let smol = String(_uninitializedCapacity: _SmallString.capacity) { buffer in
guard let count = _foreignCopyUTF8(into: buffer) else {
fatalError("String capacity was smaller than required")
}
return count
}
_internalInvariant(smol._guts.isSmall)
return smol._guts.asSmall
}
private func _convertedToSmall() -> _SmallString {
_internalInvariant(utf8Count <= _SmallString.capacity)
if _fastPath(isSmall) {
return asSmall
}
if isFastUTF8 {
return withFastUTF8 { _SmallString($0)! }
}
return _foreignConvertedToSmall()
}
internal mutating func append(_ slicedOther: _StringGutsSlice) {
defer { self._invariantCheck() }
let otherCount = slicedOther.utf8Count
let totalCount = utf8Count + otherCount
/*
Goal: determine if we need to allocate new native capacity
Possible scenarios in which we need to allocate:
• Not uniquely owned and native: we can't use the capacity to grow into,
have to become unique + native by allocating
• Not enough capacity: have to allocate to grow
Special case: a non-smol String that can fit in a smol String but doesn't
meet the above criteria shouldn't throw away its buffer just to be smol.
The reasoning here is that it may be bridged or have reserveCapacity'd
in preparation for appending more later, in which case we would end up
have to allocate anyway to convert back from smol.
If we would have to re-allocate anyway then that's not a problem and we
should just be smol.
e.g. consider
var str = "" // smol
str.reserveCapacity(100) // large native unique
str += "<html>" // don't convert back to smol here!
str += htmlContents // because this would have to anyway!
*/
let hasEnoughUsableSpace = isUniqueNative &&
nativeUnusedCapacity! >= otherCount
let shouldBeSmol = totalCount <= _SmallString.capacity &&
(isSmall || !hasEnoughUsableSpace)
if shouldBeSmol {
let smolSelf = _convertedToSmall()
let smolOther = String(Substring(slicedOther))._guts._convertedToSmall()
// TODO: In-register slicing
self = _StringGuts(_SmallString(smolSelf, appending: smolOther)!)
return
}
prepareForAppendInPlace(totalCount: totalCount, otherUTF8Count: otherCount)
if slicedOther.isFastUTF8 {
let otherIsASCII = slicedOther.isASCII
slicedOther.withFastUTF8 { otherUTF8 in
self.appendInPlace(otherUTF8, isASCII: otherIsASCII)
}
return
}
_foreignAppendInPlace(slicedOther)
}
internal mutating func appendInPlace(
_ other: UnsafeBufferPointer<UInt8>, isASCII: Bool
) {
updateNativeStorage { $0.appendInPlace(other, isASCII: isASCII) }
}
@inline(never) // slow-path
private mutating func _foreignAppendInPlace(_ other: _StringGutsSlice) {
_internalInvariant(!other.isFastUTF8)
_internalInvariant(self.uniqueNativeUnusedCapacity != nil)
var iter = Substring(other).utf8.makeIterator()
updateNativeStorage { $0.appendInPlace(&iter, isASCII: other.isASCII) }
}
internal mutating func clear() {
guard isUniqueNative else {
self = _StringGuts()
return
}
// Reset the count
updateNativeStorage { $0.clear() }
}
internal mutating func remove(from lower: Index, to upper: Index) {
let lowerOffset = lower._encodedOffset
let upperOffset = upper._encodedOffset
_internalInvariant(lower.transcodedOffset == 0 && upper.transcodedOffset == 0)
_internalInvariant(lowerOffset <= upperOffset && upperOffset <= self.count)
if isUniqueNative {
updateNativeStorage { $0.remove(from: lowerOffset, to: upperOffset) }
return
}
// TODO(cleanup): Add append on guts taking range, use that
var result = String()
// FIXME: It should be okay to get rid of excess capacity
// here. rdar://problem/45635432
result.reserveCapacity(
nativeCapacity ?? (count &- (upperOffset &- lowerOffset)))
result.append(contentsOf: String(self)[..<lower])
result.append(contentsOf: String(self)[upper...])
self = result._guts
}
// - Returns: The encoded offset range of the replaced contents in the result.
@discardableResult
internal mutating func replaceSubrange<C>(
_ bounds: Range<Index>,
with newElements: C
) -> Range<Int>
where C: Collection, C.Iterator.Element == Character {
if isUniqueNative {
if let repl = newElements as? String {
if repl._guts.isFastUTF8 {
return repl._guts.withFastUTF8 {
uniqueNativeReplaceSubrange(
bounds, with: $0, isASCII: repl._guts.isASCII)
}
}
} else if let repl = newElements as? Substring {
if repl._wholeGuts.isFastUTF8 {
return repl._wholeGuts.withFastUTF8(range: repl._offsetRange) {
uniqueNativeReplaceSubrange(
bounds, with: $0, isASCII: repl._wholeGuts.isASCII)
}
}
}
return uniqueNativeReplaceSubrange(
bounds, with: newElements.lazy.flatMap { $0.utf8 })
}
var result = String()
// FIXME: It should be okay to get rid of excess capacity
// here. rdar://problem/45635432
if let capacity = self.nativeCapacity {
result.reserveCapacity(capacity)
}
let selfStr = String(self)
result.append(contentsOf: selfStr[..<bounds.lowerBound])
let i = result._guts.count
result.append(contentsOf: newElements)
let j = result._guts.count
result.append(contentsOf: selfStr[bounds.upperBound...])
self = result._guts
return Range(_uncheckedBounds: (i, j))
}
// - Returns: The encoded offset range of the replaced contents in the result.
@discardableResult
internal mutating func replaceSubrange<C>(
_ bounds: Range<Index>,
with newElements: C
) -> Range<Int>
where C: Collection, C.Iterator.Element == UnicodeScalar {
if isUniqueNative {
if let repl = newElements as? String.UnicodeScalarView {
if repl._guts.isFastUTF8 {
return repl._guts.withFastUTF8 {
uniqueNativeReplaceSubrange(
bounds, with: $0, isASCII: repl._guts.isASCII)
}
}
} else if let repl = newElements as? Substring.UnicodeScalarView {
if repl._wholeGuts.isFastUTF8 {
return repl._wholeGuts.withFastUTF8(range: repl._offsetRange) {
uniqueNativeReplaceSubrange(
bounds, with: $0, isASCII: repl._wholeGuts.isASCII)
}
}
}
if #available(SwiftStdlib 5.1, *) {
return uniqueNativeReplaceSubrange(
bounds, with: newElements.lazy.flatMap { $0.utf8 })
} else {
// FIXME: The stdlib should not have a deployment target this ancient.
let c = newElements.reduce(0) { $0 + UTF8.width($1) }
var utf8: [UInt8] = []
utf8.reserveCapacity(c)
utf8 = newElements.reduce(into: utf8) { utf8, next in
next.withUTF8CodeUnits { utf8.append(contentsOf: $0) }
}
return uniqueNativeReplaceSubrange(bounds, with: utf8)
}
}
var result = String.UnicodeScalarView()
// FIXME: It should be okay to get rid of excess capacity
// here. rdar://problem/45635432
if let capacity = self.nativeCapacity {
result.reserveCapacity(capacity)
}
let selfStr = String.UnicodeScalarView(self)
result.append(contentsOf: selfStr[..<bounds.lowerBound])
let i = result._guts.count
result.append(contentsOf: newElements)
let j = result._guts.count
result.append(contentsOf: selfStr[bounds.upperBound...])
self = result._guts
return Range(_uncheckedBounds: (i, j))
}
// - Returns: The encoded offset range of the replaced contents in the result.
internal mutating func uniqueNativeReplaceSubrange(
_ bounds: Range<Index>,
with codeUnits: UnsafeBufferPointer<UInt8>,
isASCII: Bool
) -> Range<Int> {
let neededCapacity =
bounds.lowerBound._encodedOffset
+ codeUnits.count + (self.count - bounds.upperBound._encodedOffset)
reserveCapacity(neededCapacity)
_internalInvariant(bounds.lowerBound.transcodedOffset == 0)
_internalInvariant(bounds.upperBound.transcodedOffset == 0)
let start = bounds.lowerBound._encodedOffset
let end = bounds.upperBound._encodedOffset
updateNativeStorage {
$0.replace(from: start, to: end, with: codeUnits)
}
return Range(_uncheckedBounds: (start, start + codeUnits.count))
}
// - Returns: The encoded offset range of the replaced contents in the result.
internal mutating func uniqueNativeReplaceSubrange<C: Collection>(
_ bounds: Range<Index>,
with codeUnits: C
) -> Range<Int>
where C.Element == UInt8 {
let replCount = codeUnits.count
let neededCapacity =
bounds.lowerBound._encodedOffset
+ replCount + (self.count - bounds.upperBound._encodedOffset)
reserveCapacity(neededCapacity)
_internalInvariant(bounds.lowerBound.transcodedOffset == 0)
_internalInvariant(bounds.upperBound.transcodedOffset == 0)
let start = bounds.lowerBound._encodedOffset
let end = bounds.upperBound._encodedOffset
updateNativeStorage {
$0.replace(
from: start, to: end, with: codeUnits, replacementCount: replCount)
}
return Range(_uncheckedBounds: (start, start + replCount))
}
/// Run `body` to mutate the given `subrange` of this string within
/// `startIndex ..< endIndex`, then update `startIndex` and `endIndex` to be
/// valid positions in the resulting string, addressing the same (logical)
/// locations as in the original string.
///
/// This is used by both `Substring` and `Substring.UnicodeScalarView` to
/// implement their `replaceSubrange` methods.
///
/// - Parameter subrange: A scalar-aligned offset range in this string.
/// - Parameter startIndex: The start index of the substring that performs
/// this operation.
/// - Parameter endIndex: The end index of the substring that performs this
/// operations.
/// - Parameter body: The mutation operation to execute on `self`. The
/// returned offset range must correspond to `subrange` in the resulting
/// string.
internal mutating func mutateSubrangeInSubstring(
subrange: Range<Index>,
startIndex: inout Index,
endIndex: inout Index,
with body: (inout _StringGuts) -> Range<Int>
) {
_internalInvariant(
subrange.lowerBound >= startIndex && subrange.upperBound <= endIndex)
guard _fastPath(isUTF8) else {
// UTF-16 string. The mutation will convert this to the native UTF-8
// encoding, so we need to do some extra work to preserve our bounds.
let utf8StartOffset = String(self).utf8.distance(
from: self.startIndex, to: startIndex)
let oldUTF8Count = String(self).utf8.distance(
from: startIndex, to: endIndex)
let oldUTF8SubrangeCount = String(self).utf8.distance(
from: subrange.lowerBound, to: subrange.upperBound)
let newUTF8Subrange = body(&self)
_internalInvariant(isUTF8)
let newUTF8Count =
oldUTF8Count + newUTF8Subrange.count - oldUTF8SubrangeCount
var newStride = 0
if !newUTF8Subrange.isEmpty {
// Get the character stride in the entire string, not just the substring.
// (Characters in a substring may end beyond the bounds of it.)
newStride = _opaqueCharacterStride(startingAt: utf8StartOffset)
}
startIndex = String.Index(
encodedOffset: utf8StartOffset,
transcodedOffset: 0,
characterStride: newStride)._scalarAligned._knownUTF8
if isOnGraphemeClusterBoundary(startIndex) {
startIndex = startIndex._characterAligned
}
endIndex = String.Index(
encodedOffset: utf8StartOffset + newUTF8Count,
transcodedOffset: 0)._scalarAligned._knownUTF8
return
}
// UTF-8 string.
let oldRange = subrange._encodedOffsetRange
let newRange = body(&self)
let oldBounds = Range(
_uncheckedBounds: (startIndex._encodedOffset, endIndex._encodedOffset))
let newBounds = Range(_uncheckedBounds: (
oldBounds.lowerBound,
oldBounds.upperBound &+ newRange.count &- oldRange.count))
// Update `startIndex` if necessary. The replacement may have invalidated
// its cached character stride and character alignment flag, but not its
// stored offset, encoding, or scalar alignment.
//
// We are exploiting the fact that mutating the string _after_ the scalar
// following the end of the character at `startIndex` cannot possibly change
// the length of that character. (This is true because `index(after:)` never
// needs to look ahead by more than one Unicode scalar.)
let oldStride = startIndex.characterStride ?? 0
if oldRange.lowerBound <= oldBounds.lowerBound &+ oldStride {
var newStride = 0
if !newBounds.isEmpty {
// Get the character stride in the entire string, not just the substring.
// (Characters in a substring may end beyond the bounds of it.)
newStride = _opaqueCharacterStride(startingAt: newBounds.lowerBound)
}
var newStart = String.Index(
encodedOffset: newBounds.lowerBound,
characterStride: newStride
)._scalarAligned._knownUTF8
// Preserve character alignment flag if possible
if startIndex._isCharacterAligned,
(oldRange.lowerBound > oldBounds.lowerBound ||
isOnGraphemeClusterBoundary(newStart)) {
newStart = newStart._characterAligned
}
startIndex = newStart
}
// Update `endIndex`.
if newBounds.upperBound != endIndex._encodedOffset {
endIndex = Index(
_encodedOffset: newBounds.upperBound
)._scalarAligned._knownUTF8
}
}
}
|