Skip to content

Commit 42b8f18

Browse files
committed
[TLS] Add a uText to our TLS
ICU's uText APIs offer a cross-encoding means of interacting with grapheme breaking. Storing one on our TLS allows us to explore mised encodings, and even for UTF-16 might be profitable if setText mallocs/frees an internal uText (which seems to be the case). This isn't hooked up to anything yet, but its highly likely we will be using it soon and adding it now enables more rapid development of the UTF-8 prototype, due to it's tie-in with the runtime.
1 parent 1413cb1 commit 42b8f18

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

stdlib/public/core/ThreadLocalStorage.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ internal struct _ThreadLocalStorage {
3838
//
3939
// private
4040
internal var uBreakIterator: OpaquePointer
41+
internal var uText: OpaquePointer
4142

4243
// TODO: Consider saving two, e.g. for character-by-character comparison
4344

@@ -55,8 +56,9 @@ internal struct _ThreadLocalStorage {
5556
// TODO: unowned reference to string owner, base address, and _countAndFlags
5657

5758
// private: Should only be called by _initializeThreadLocalStorage
58-
internal init(_uBreakIterator: OpaquePointer) {
59+
internal init(_uBreakIterator: OpaquePointer, _uText: OpaquePointer) {
5960
self.uBreakIterator = _uBreakIterator
61+
self.uText = _uText
6062
}
6163

6264
// Get the current thread's TLS pointer. On first call for a given thread,
@@ -104,19 +106,26 @@ internal func _destroyTLS(_ ptr: UnsafeMutableRawPointer?) {
104106
internal func _createThreadLocalStorage()
105107
-> UnsafeMutablePointer<_ThreadLocalStorage>
106108
{
107-
// Create and initialize one.
109+
// Allocate and initialize a UBreakIterator and UText.
108110
var err = __swift_stdlib_U_ZERO_ERROR
109111
let newUBreakIterator = __swift_stdlib_ubrk_open(
110112
/*type:*/ __swift_stdlib_UBRK_CHARACTER, /*locale:*/ nil,
111113
/*text:*/ nil, /*textLength:*/ 0, /*status:*/ &err)
112114
_precondition(err.isSuccess, "Unexpected ubrk_open failure")
113115

116+
// utext_openUTF8 needs a valid pointer, even though we won't read from it
117+
var a: Int8 = 0x41
118+
let newUText = __swift_stdlib_utext_openUTF8(
119+
/*ut:*/ nil, /*s:*/ &a, /*len:*/ 1, /*status:*/ &err)
120+
121+
_precondition(err.isSuccess, "Unexpected utext_openUTF8 failure")
122+
114123
let tlsPtr: UnsafeMutablePointer<_ThreadLocalStorage>
115124
= UnsafeMutablePointer<_ThreadLocalStorage>.allocate(
116125
capacity: 1
117126
)
118-
tlsPtr.initialize(
119-
to: _ThreadLocalStorage(_uBreakIterator: newUBreakIterator)
120-
)
127+
tlsPtr.initialize(to: _ThreadLocalStorage(
128+
_uBreakIterator: newUBreakIterator, _uText: newUText))
129+
121130
return tlsPtr
122131
}

0 commit comments

Comments
 (0)