Skip to content
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

[SR-14301] Open-source String.localizedStringWithFormat(_:_:…) does not insert Unicode Isolates #4219

Open
swift-ci opened this issue Mar 4, 2021 · 1 comment

Comments

@swift-ci
Copy link
Contributor

swift-ci commented Mar 4, 2021

Previous ID SR-14301
Radar rdar://problem/75097858
Original Reporter fumoboy007 (JIRA User)
Type Bug
Environment

Added the tests in the description to the Foundation test suite and ran them using

Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28)
Target: x86_64-apple-darwin19.6.0
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 0cb11fd67f4fd37a5cd358ffcdc0697f

Issue Description:

According to WWDC 2016 Session 232 (“What's New in International User Interfaces”), iOS 10 and later support Unicode Isolates for bidirectional text. Also according to that session, `NSString.init(format:locale:arguments🙂` should automatically wrap embedded text in Unicode Isolates (U+2068 FIRST STRONG ISOLATE and U+2069 POP DIRECTIONAL ISOLATE).

This is necessary for text dynamically-embedded at the start of the string because the embedded text could be either LTR or RTL. From my understanding, the text display system follows the Unicode Bidirectional Algorithm rule P2, which sets the overall direction of the text to the direction of the first strong character. Without Unicode Isolates, this is problematic because the embedded text could be LTR while the rest of the text is RTL (or vice versa). Wrapping the embedded text in Unicode Isolates solves this problem by telling the algorithm to skip the embedded text when evaluating rule P2.

(Note that iOS had a bug (FB7330789) where Unicode Isolates were not automatically inserted for embedded LTR text. That bug is now fixed in iOS 14.)

The open-source Foundation should match the behavior of Darwin Foundation but in this case, it doesn’t. The following tests should pass but they currently fail:

func test_wrapsEmbeddedRTLTextInUnicodeIsolates() {
    let embeddedText = "\u{0645}"

    let interpolatedText = String.localizedStringWithFormat("a %@",
                                                            embeddedText as NSString)

    let expectedUnicodeScalars = [
        UnicodeScalar(0x61 as UInt8),
        UnicodeScalar(0x20 as UInt8),
        UnicodeScalar(0x2068)!,
        UnicodeScalar(0x645)!,
        UnicodeScalar(0x2069)!
    ]
    XCTAssertEqual(Array(interpolatedText.unicodeScalars), expectedUnicodeScalars)
}

func test_wrapsEmbeddedLTRTextInUnicodeIsolates() {
    let embeddedText = "a"

    let interpolatedText = String.localizedStringWithFormat("\u{0645} %@",
                                                            embeddedText as NSString)

    let expectedUnicodeScalars = [
        UnicodeScalar(0x645)!,
        UnicodeScalar(0x20 as UInt8),
        UnicodeScalar(0x2068)!,
        UnicodeScalar(0x61 as UInt8),
        UnicodeScalar(0x2069)!
    ]
    XCTAssertEqual(Array(interpolatedText.unicodeScalars), expectedUnicodeScalars)
}
@typesanitizer
Copy link

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants