-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Closed
Labels
StringArea → standard library: The `String` typeArea → standard library: The `String` typebugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.standard libraryArea: Standard library umbrellaArea: Standard library umbrellaswift 5.9unexpected behaviorBug: Unexpected behavior or incorrect outputBug: Unexpected behavior or incorrect output
Description
As reported on the Swift forums, String.hasPrefix and String.hasSuffix sometimes have an incorrect result:
"\r\n".hasPrefix("\r") // true
"\r\n".hasPrefix("\r"[...]) // false
"\r\n".hasSuffix("\n") // true
"\r\n".hasSuffix("\n"[...]) // falseAll four of these calls should return false, since those methods should be matching at the character level, not at the Unicode scalar level. The problem is in the String-specific overloads for hasPrefix and hasSuffix in StringLegacy.swift, which don't validate that the string passed as a parameter ends on a character boundary (when both strings are known to be NFC). The calls that pass a substring go to the StringProtocol-based overload, instead, which calls through to the Sequence method starts(with:).
Environment
- Swift
mainbranch
Metadata
Metadata
Assignees
Labels
StringArea → standard library: The `String` typeArea → standard library: The `String` typebugA deviation from expected or documented behavior. Also: expected but undesirable behavior.A deviation from expected or documented behavior. Also: expected but undesirable behavior.standard libraryArea: Standard library umbrellaArea: Standard library umbrellaswift 5.9unexpected behaviorBug: Unexpected behavior or incorrect outputBug: Unexpected behavior or incorrect output