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 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 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
//
//===----------------------------------------------------------------------===//
extension _StringGuts {
internal func roundDownToNearestWord(
_ i: String.Index
) -> String.Index {
_internalInvariant(i._encodedOffset <= count)
let offset = i._encodedOffset
if offset == 0 || offset == count {
return i
}
let start = previousWordIndex(endingAt: offset)
let end = nextWordIndex(startingAt: start)
_internalInvariant(offset <= end, "Word breaking inconsistency")
if offset == end {
return i
}
return String.Index(_encodedOffset: start)
}
@inline(never)
@_effects(releasenone)
internal func nextWordIndex(startingAt i: Int) -> Int {
if _slowPath(isForeign) {
return _foreignNextWordIndex(startingAt: i)
}
return withFastUTF8 { utf8 in
nextWordBoundary(startingAt: i) {
_internalInvariant($0 >= 0)
guard $0 < utf8.count else {
return nil
}
let (scalar, len) = _decodeScalar(utf8, startingAt: $0)
return (scalar, $0 &+ len)
}
}
}
internal func _foreignNextWordIndex(startingAt i: Int) -> Int {
#if _runtime(_ObjC)
return nextWordBoundary(startingAt: i) {
_internalInvariant($0 >= 0)
guard $0 < count else {
return nil
}
let scalars = String.UnicodeScalarView(self)
let idx = String.Index(_encodedOffset: $0)
let scalar = scalars[idx]
let nextIndex = scalars.index(after: idx)
return (scalar, nextIndex._encodedOffset)
}
#else
fatalError("No foreign strings on this platform in this version of Swift.")
#endif
}
internal func previousWordIndex(endingAt i: Int) -> Int {
if _slowPath(isForeign) {
return _foreignPreviousWordIndex(endingAt: i)
}
return withFastUTF8 { utf8 in
previousWordBoundary(endingAt: i) {
_internalInvariant($0 <= count)
guard $0 > 0 else {
return nil
}
let (scalar, len) = _decodeScalar(utf8, endingAt: $0)
return (scalar, $0 &- len)
}
}
}
@inline(never)
internal func _foreignPreviousWordIndex(endingAt i: Int) -> Int {
#if _runtime(_ObjC)
return previousWordBoundary(endingAt: i) {
_internalInvariant($0 <= count)
guard $0 > 0 else {
return nil
}
let scalars = String.UnicodeScalarView(self)
let idx = String.Index(_encodedOffset: $0)
let previousIndex = scalars.index(before: idx)
let scalar = scalars[previousIndex]
return (scalar, previousIndex._encodedOffset)
}
#else
fatalError("No foreign strings on this platform in this version of Swift.")
#endif
}
}
internal enum _WordQuestion {
case checkingRegionalIndicator(count: Int, previousRIIndex: Int)
case requireAHLetter
case requireNumeric
case requireHebrewLetter
}
extension _WordQuestion: Equatable {}
internal struct _WordBreakingState {
var constraint: (question: _WordQuestion, index: Int)? = nil
var index: Int
var previousIndex: Int? = nil
var previousProperty: Unicode._WordBreakProperty? = nil
// When walking forward in a string, we need to not break on emoji flag
// sequences. Emoji flag sequences are composed of 2 regional indicators, so
// when we see our first (.regionalIndicator, .regionalIndicator) decision,
// we need to know to return false in this case. However, if the next scalar
// is another regional indicator, we reach the same decision rule, but in this
// case we actually need to break there's a boundary between emoji flag
// sequences.
var shouldBreakRI = false
}
extension _StringGuts {
// Returns the stride of the next word at the previous boundary offset.
internal func nextWordBoundary(
startingAt index: Int,
nextScalar: (Int) -> (scalar: Unicode.Scalar, end: Int)?
) -> Int {
_precondition(index < endIndex._encodedOffset)
var (scalar, index) = nextScalar(index)!
var state = _WordBreakingState(index: index)
while let (scalar2, nextIndex) = nextScalar(state.index) {
if shouldBreak(between: scalar, and: scalar2, with: &state) {
break
}
scalar = scalar2
state.index = nextIndex
}
// If we have a leftover constraint, return the index
if let constraint = state.constraint {
return constraint.index
}
return state.index
}
// Returns the stride of the previous word at the current boundary offset.
internal func previousWordBoundary(
endingAt index: Int,
previousScalar: (Int) -> (scalar: Unicode.Scalar, start: Int)?
) -> Int {
var (scalar2, index) = previousScalar(index)!
var state = _WordBreakingState(index: index)
while let (scalar, previousIndex) = previousScalar(state.index) {
if shouldBreakBackward(between: scalar, and: scalar2, with: &state) {
break
}
scalar2 = scalar
state.index = previousIndex
}
if let previousIndex = state.previousIndex {
return previousIndex
}
if let constraint = state.constraint {
if let riIndex = handleRIConstraint(constraint, with: state) {
return riIndex
}
return constraint.index
}
return state.index
}
}
extension _StringGuts {
// The "algorithm" that determines whether or not we should break between
// certain word break properties.
//
// This is based off of the Unicode Annex #29 for [Word Boundary
// Rules](https://unicode.org/reports/tr29/#Word_Boundary_Rules).
internal func shouldBreak(
between scalar1: Unicode.Scalar,
and scalar2: Unicode.Scalar,
with state: inout _WordBreakingState
) -> Bool {
// WB3
if scalar1.value == 0xD, scalar2.value == 0xA {
return false
}
let x = Unicode._WordBreakProperty(from: scalar1)
// WB3a, handled here since we don't need to look up `y` for this
if x == .newlineCRLF {
return true
}
let y = Unicode._WordBreakProperty(from: scalar2)
switch (x, y) {
// Fast path: If we know our scalars have no properties the decision is
// trivial and we don't need to crawl to the default statement.
case (.any, .any):
return true
// WB3b
case (_, .newlineCRLF):
return true
// WB3c
case (.zwj, .extendedPictographic):
return false
// WB3d
case (.wSegSpace, .wSegSpace):
return false
// WB4
case (_, .format),
(_, .extend),
(_, .zwj):
if x != .format && x != .extend && x != .zwj {
state.previousProperty = x
}
return false
default:
let newX = state.previousProperty ?? x
return decidePostFormat(between: newX, and: y, with: &state)
}
}
internal func decidePostFormat(
between x: Unicode._WordBreakProperty,
and y: Unicode._WordBreakProperty,
with state: inout _WordBreakingState
) -> Bool {
state.previousProperty = nil
switch (x, y) {
// WB5
case (.aLetter, .aLetter),
(.aLetter, .hebrewLetter),
(.hebrewLetter, .aLetter),
(.hebrewLetter, .hebrewLetter):
return false
// WB6
case (.aLetter, .midLetter),
(.hebrewLetter, .midLetter),
(.aLetter, .midNumLet),
(.hebrewLetter, .midNumLet),
(.aLetter, .singleQuote):
state.constraint = (question: .requireAHLetter, index: state.index)
return false
// WB7
case (.midLetter, .aLetter),
(.midLetter, .hebrewLetter),
(.midNumLet, .aLetter),
(.midNumLet, .hebrewLetter),
(.singleQuote, .aLetter),
(.singleQuote, .hebrewLetter):
if let constraint = state.constraint {
if constraint.question == .requireAHLetter {
state.constraint = nil
return false
}
state.index = constraint.index
return true
}
return true
// WB7a
case (.hebrewLetter, .singleQuote):
return false
// WB7b
case (.hebrewLetter, .doubleQuote):
state.constraint = (question: .requireHebrewLetter, index: state.index)
return false
// WB7c
case (.doubleQuote, .hebrewLetter):
if let constraint = state.constraint {
if constraint.question == .requireHebrewLetter {
state.constraint = nil
return false
}
state.index = constraint.index
return true
}
return true
// WB8
case (.numeric, .numeric):
return false
// WB9
case (.aLetter, .numeric),
(.hebrewLetter, .numeric):
return false
// WB10
case (.numeric, .aLetter),
(.numeric, .hebrewLetter):
return false
// WB11
case (.midNum, .numeric),
(.midNumLet, .numeric),
(.singleQuote, .numeric):
if let constraint = state.constraint {
if constraint.question == .requireNumeric {
state.constraint = nil
return false
}
state.index = constraint.index
return true
}
return true
// WB12
case (.numeric, .midNum),
(.numeric, .midNumLet),
(.numeric, .singleQuote):
state.constraint = (question: .requireNumeric, index: state.index)
return false
// WB13
case (.katakana, .katakana):
return false
// WB13a
case (.aLetter, .extendNumLet),
(.hebrewLetter, .extendNumLet),
(.numeric, .extendNumLet),
(.katakana, .extendNumLet),
(.extendNumLet, .extendNumLet):
return false
// WB13b
case (.extendNumLet, .aLetter),
(.extendNumLet, .hebrewLetter),
(.extendNumLet, .numeric),
(.extendNumLet, .katakana):
return false
// WB15
case (.regionalIndicator, .regionalIndicator):
defer {
state.shouldBreakRI.toggle()
}
return state.shouldBreakRI
default:
return true
}
}
}
extension _StringGuts {
// The "algorithm" that determines whether or not we should break between
// certain word break properties.
//
// This is based off of the Unicode Annex #29 for [Word Boundary
// Rules](https://unicode.org/reports/tr29/#Word_Boundary_Rules).
internal func shouldBreakBackward(
between scalar1: Unicode.Scalar,
and scalar2: Unicode.Scalar,
with state: inout _WordBreakingState
) -> Bool {
// WB3
if scalar1.value == 0xD, scalar2.value == 0xA {
return false
}
let x = Unicode._WordBreakProperty(from: scalar1)
let y = Unicode._WordBreakProperty(from: scalar2)
switch (x, y) {
// Fast path: If we know our scalars have no properties the decision is
// trivial and we don't need to crawl to the default statement.
case (.any, .any):
return true
// WB3a and WB3b
case (.newlineCRLF, _),
(_, .newlineCRLF):
return true
// WB3c
case (.zwj, .extendedPictographic):
return false
// WB3d
case (.wSegSpace, .wSegSpace):
return false
// WB4
case (.format, _),
(.extend, _),
(.zwj, _):
if y != .format && y != .extend && y != .zwj {
state.previousProperty = y
// If we already have a constraint in flight, then use that as our base
// previous index. Otherwise, use where we're at right now.
if let constraint = state.constraint {
state.previousIndex = constraint.index
} else {
state.previousIndex = state.index
}
}
return false
// WB4
case (_, .format),
(_, .extend),
(_, .zwj):
if state.previousProperty != nil {
fallthrough
}
return false
default:
var newY = y
if let previousProperty = state.previousProperty {
newY = previousProperty
}
return decidePostFormatBackward(between: x, and: newY, with: &state)
}
}
internal func decidePostFormatBackward(
between x: Unicode._WordBreakProperty,
and y: Unicode._WordBreakProperty,
with state: inout _WordBreakingState
) -> Bool {
state.previousProperty = nil
switch (x, y) {
case (.any, .any):
return true
// WB5
case (.aLetter, .aLetter),
(.aLetter, .hebrewLetter),
(.hebrewLetter, .aLetter),
(.hebrewLetter, .hebrewLetter):
state.previousIndex = nil
return false
// WB6
case (.aLetter, .midLetter),
(.hebrewLetter, .midLetter),
(.aLetter, .midNumLet),
(.hebrewLetter, .midNumLet),
(.aLetter, .singleQuote):
if let constraint = state.constraint {
if constraint.question == .requireAHLetter {
state.constraint = nil
state.previousIndex = nil
return false
}
state.index = constraint.index
return true
}
return true
// WB7
case (.midLetter, .aLetter),
(.midLetter, .hebrewLetter),
(.midNumLet, .aLetter),
(.midNumLet, .hebrewLetter),
(.singleQuote, .aLetter),
(.singleQuote, .hebrewLetter):
state.constraint = (question: .requireAHLetter, index: state.index)
return false
// WB7a
case (.hebrewLetter, .singleQuote):
state.previousIndex = nil
return false
// WB7b
case (.hebrewLetter, .doubleQuote):
if let constraint = state.constraint {
if constraint.question == .requireHebrewLetter {
state.constraint = nil
state.previousIndex = nil
return false
}
state.index = constraint.index
return true
}
return true
// WB7c
case (.doubleQuote, .hebrewLetter):
state.constraint = (question: .requireHebrewLetter, index: state.index)
return false
// WB8
case (.numeric, .numeric):
state.previousIndex = nil
return false
// WB9
case (.aLetter, .numeric),
(.hebrewLetter, .numeric):
state.previousIndex = nil
return false
// WB10
case (.numeric, .aLetter),
(.numeric, .hebrewLetter):
state.previousIndex = nil
return false
// WB11
case (.midNum, .numeric),
(.midNumLet, .numeric),
(.singleQuote, .numeric):
state.constraint = (question: .requireNumeric, index: state.index)
return false
// WB12
case (.numeric, .midNum),
(.numeric, .midNumLet),
(.numeric, .singleQuote):
if let constraint = state.constraint {
if constraint.question == .requireNumeric {
state.constraint = nil
state.previousIndex = nil
return false
}
state.index = constraint.index
return true
}
return true
// WB13
case (.katakana, .katakana):
state.previousIndex = nil
return false
// WB13a
case (.aLetter, .extendNumLet),
(.hebrewLetter, .extendNumLet),
(.numeric, .extendNumLet),
(.katakana, .extendNumLet),
(.extendNumLet, .extendNumLet):
state.previousIndex = nil
return false
// WB13b
case (.extendNumLet, .aLetter),
(.extendNumLet, .hebrewLetter),
(.extendNumLet, .numeric),
(.extendNumLet, .katakana):
state.previousIndex = nil
return false
// WB15
case (.regionalIndicator, .regionalIndicator):
var riCount = 0
var previousRIIndex = state.index
var constraintIndex = state.index
if let constraint = state.constraint {
if case let .checkingRegionalIndicator(count, riIndex) =
constraint.question {
riCount = count + 1
previousRIIndex = count == 0 ? state.index : riIndex
constraintIndex = constraint.index
}
} else {
if let previousIndex = state.previousIndex {
constraintIndex = previousIndex
}
}
state.constraint = (
question: .checkingRegionalIndicator(
count: riCount,
previousRIIndex: previousRIIndex
),
index: constraintIndex
)
state.previousIndex = nil
return false
default:
return true
}
}
internal func handleRIConstraint(
_ constraint: (question: _WordQuestion, index: Int),
with state: _WordBreakingState
) -> Int? {
if case let .checkingRegionalIndicator(count, previousRIIndex) =
constraint.question {
// If our count is 0, then we were unable to update previousRIIndex.
// However, that index is now equal to state.index.
if count == 0 {
return state.index
}
// We were able to update previousRIIndex!
if count.isMultiple(of: 2) {
return previousRIIndex
}
}
return nil
}
}
|