Skip to content

Commit 299f663

Browse files
committed
---
yaml --- r: 202131 b: refs/heads/tensorflow c: 5807bb9 h: refs/heads/master i: 202129: 61ee416 202127: 2af0161
1 parent acb4138 commit 299f663

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ refs/tags/swift-DEVELOPMENT-SNAPSHOT-2018-04-25-a: 22f738a831d43aff2b9c9773bcb65
821821
refs/tags/swift-DEVELOPMENT-SNAPSHOT-2018-05-08-a: 7d98cc16689baba5c8a3b90a9329bdcc1a12b4e9
822822
refs/heads/cherr42: a566ad54b073c2c56ac0a705d0a5bed9743135a5
823823
"refs/heads/codable_test_comment_fix": fc8f6824f7f347e1e8db55bff62db385c5728b5a
824-
refs/heads/tensorflow: fb9f7ecca13fa0fc5257ab9dbf065f9b7d1399ad
824+
refs/heads/tensorflow: 5807bb9436258a10c4b68b0fbf0614c2376654ac
825825
refs/tags/swift-4.1-DEVELOPMENT-SNAPSHOT-2018-05-11-a: 8126fd7a652e2f70ad6d76505239e34fb2ef3e1a
826826
refs/tags/swift-4.1-DEVELOPMENT-SNAPSHOT-2018-05-12-a: b3fd3dd84df6717f2e2e9df58c6d7e99fed57086
827827
refs/tags/swift-4.1-DEVELOPMENT-SNAPSHOT-2018-05-13-a: 71135119579039dc321c5f65d870050fe36efda2

branches/tensorflow/stdlib/public/core/UnicodeScalarProperties.swift

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,28 +1004,40 @@ extension Unicode.Scalar.Properties {
10041004
internal func _scalarName(
10051005
_ choice: __swift_stdlib_UCharNameChoice
10061006
) -> String? {
1007-
let initialCapacity = 256
1008-
1009-
var storage = _SwiftStringStorage<UTF8.CodeUnit>.create(
1010-
capacity: initialCapacity,
1011-
count: 0)
1007+
// Attempt to fit it into a small UTF-8 string first. Code points names are
1008+
// guaranteed by the standard to be ASCII only.
1009+
var smallString = _SmallUTF8String()
10121010
var err = __swift_stdlib_U_ZERO_ERROR
1011+
let correctSizeRaw = smallString._withMutableExcessCapacityBytes { ptr in
1012+
return __swift_stdlib_u_charName(
1013+
_value,
1014+
choice,
1015+
ptr.baseAddress._unsafelyUnwrappedUnchecked.assumingMemoryBound(
1016+
to: Int8.self),
1017+
Int32(ptr.count),
1018+
&err)
1019+
}
10131020

1014-
let correctSize = _expandingStorageIfNeeded(&storage) { storage in
1015-
return storage.start.withMemoryRebound(
1016-
to: Int8.self,
1017-
capacity: storage.capacity
1018-
) { storagePtr in
1019-
err = __swift_stdlib_U_ZERO_ERROR
1020-
return __swift_stdlib_u_charName(
1021-
_value, choice, storagePtr, Int32(storage.capacity), &err)
1022-
}
1021+
let correctSize = Int(correctSizeRaw)
1022+
if err.isSuccess {
1023+
if correctSize == 0 { return nil }
1024+
smallString.count = correctSize
1025+
return String(_StringGuts(smallString))
1026+
}
1027+
// If the call wasn't successful, the only error we expect to see is buffer
1028+
// overflow; anything else is a severe error.
1029+
guard err == __swift_stdlib_U_BUFFER_OVERFLOW_ERROR else {
1030+
fatalError("u_charName: Unexpected error retrieving scalar name.")
10231031
}
10241032

1025-
guard err.isSuccess && correctSize > 0 else {
1026-
return nil
1033+
// If it didn't fit, we need to allocate the necessary amount of memory and
1034+
// make a regular ASCII string.
1035+
var (asciiArray, ptr) = [UInt8]._allocateUninitialized(correctSize)
1036+
_ = ptr.withMemoryRebound(to: Int8.self, capacity: correctSize) { int8Ptr in
1037+
err = __swift_stdlib_U_ZERO_ERROR
1038+
__swift_stdlib_u_charName(_value, choice, int8Ptr, correctSizeRaw, &err)
10271039
}
1028-
return String(_storage: storage)
1040+
return String._fromASCII(asciiArray)
10291041
}
10301042

10311043
/// The published name of the scalar.

0 commit comments

Comments
 (0)