Skip to content

[String] Naturalize Character #22663

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
Feb 19, 2019
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
16 changes: 15 additions & 1 deletion stdlib/public/core/Character.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ extension Character {
internal func _invariantCheck() {
_internalInvariant(_str.count == 1)
_internalInvariant(_str._guts.isFastUTF8)

// TODO(@eject): Switch to a helper property on StringObject/StringGuts.
_internalInvariant(
_str._guts.isSmall || _str._guts._object._countAndFlags.isTailAllocated)
}
#endif // INTERNAL_CHECKS_ENABLED
}
Expand Down Expand Up @@ -173,7 +177,17 @@ extension Character :
"Can't form a Character from an empty String")
_debugPrecondition(s.index(after: s.startIndex) == s.endIndex,
"Can't form a Character from a String containing more than one extended grapheme cluster")
self.init(unchecked: s)

// TODO(@eject): Switch to a helper property on StringObject/StringGuts.
if _fastPath(
s._guts.isSmall || s._guts._object._countAndFlags.isTailAllocated
) {
self.init(unchecked: s)
return
}

// TODO(@eject): Outline this
self.init(unchecked: s._withUTF8 { String._uncheckedFromUTF8($0) })
}
}

Expand Down
6 changes: 5 additions & 1 deletion stdlib/public/core/StringObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,10 @@ extension _StringObject {
isNativelyStored: set for native stored strings
- `largeAddressBits` holds an instance of `_StringStorage`.
- I.e. the start of the code units is at the stored address + `nativeBias`
isTailAllocated: start of the code units is at the stored address + `nativeBias`
isTailAllocated: contiguous UTF-8 code units starts at address + `nativeBias`
- `isNativelyStored` always implies `isTailAllocated`, but not vice versa
(e.g. literals)
- `isTailAllocated` always implies `isFastUTF8`
TBD: Reserved for future usage
- Setting a TBD bit to 1 must be semantically equivalent to 0
- I.e. it can only be used to "cache" fast-path information in the future
Expand Down Expand Up @@ -1073,6 +1074,9 @@ extension _StringObject {
} else {
_internalInvariant(isLarge)
_internalInvariant(largeCount == count)
if _countAndFlags.isTailAllocated {
_internalInvariant(providesFastUTF8)
}
if providesFastUTF8 && largeFastIsTailAllocated {
_internalInvariant(!isSmall)
_internalInvariant(!largeIsCocoa)
Expand Down
24 changes: 24 additions & 0 deletions test/stdlib/StringBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,29 @@ StringBridgeTests.test("Tagged NSString") {
#endif // not 32bit
}

func returnOne<T>(_ t: T) -> Int { return 1 }
StringBridgeTests.test("Character from NSString") {
// NOTE: Using hard-coded literals to directly construct NSStrings
let ns1 = "A" as NSString
let ns2 = "A\u{301}" as NSString
let ns3 = "𓁹͇͈͉͍͎͊͋͌ͧͨͩͪͫͬͭͮ͏̛͓͔͕͖͙͚̗̘̙̜̹̺̻̼͐͑͒͗͛ͣͤͥͦ̽̾̿̀́͂̓̈́͆ͧͨͩͪͫͬͭͮ͘̚͜͟͢͝͞͠͡ͅ" as NSString

let c1 = Character(ns1 as String)
let c2 = Character(ns2 as String)
let c3 = Character(ns3 as String)

expectEqual("A", String(c1))
expectNotNil(String(c1).utf8.withContiguousStorageIfAvailable(returnOne))

expectEqual("A\u{301}", String(c2))
expectNotNil(String(c2).utf8.withContiguousStorageIfAvailable(returnOne))
expectNil((ns2 as String).utf8.withContiguousStorageIfAvailable(returnOne))

expectEqual("𓁹͇͈͉͍͎͊͋͌ͧͨͩͪͫͬͭͮ͏̛͓͔͕͖͙͚̗̘̙̜̹̺̻̼͐͑͒͗͛ͣͤͥͦ̽̾̿̀́͂̓̈́͆ͧͨͩͪͫͬͭͮ͘̚͜͟͢͝͞͠͡ͅ", String(c3))
expectNotNil(String(c3).utf8.withContiguousStorageIfAvailable(returnOne))
expectNil((ns3 as String).utf8.withContiguousStorageIfAvailable(returnOne))
}


runAllTests()