Skip to content

[stdlib] Fix backwards iteration of word breaking #69728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion stdlib/public/core/StringWordBreaking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,14 @@ extension _StringGuts {
(.zwj, _):
if y != .format && y != .extend && y != .zwj {
state.previousProperty = y
state.previousIndex = state.index

// 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
Expand Down
33 changes: 33 additions & 0 deletions validation-test/stdlib/StringWordBreaking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,39 @@ if #available(SwiftStdlib 5.9, *) {
}
}

// rdar://116652595
//
// We were accidently hanging when rounding word indices for some concoctions of
// strings. In particular, where we had a pair of scalars create a constraint
// for the preceeding pair, but the preceeding extend rules were not taking the
// constraint into consideration.
if #available(SwiftStdlib 5.10, *) {
StringWordBreaking.test("word breaking backward extend constraints") {
let strs = ["日\u{FE0F}:X ", "👨‍👨‍👧‍👦\u{FE0F}:X ", "⛔️:X ", "⛔️·X ", "⛔️:X "]
let strWords = [
["日\u{FE0F}", ":", "X", " "],
["👨‍👨‍👧‍👦\u{FE0F}", ":", "X", " "],
["⛔️", ":", "X", " "],
["⛔️", "·", "X", " "],
["⛔️", ":", "X", " "]
]

for (str, words) in zip(strs, strWords) {
expectEqual(
words,
str._words,
"string: \(String(reflecting: str))"
)

expectEqual(
words.reversed(),
str._wordsBackwards,
"string: \(String(reflecting: str))"
)
}
}
}

// The most simple subclass of NSString that CoreFoundation does not know
// about.
class NonContiguousNSString : NSString {
Expand Down