mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[test] Make string internal testing a little more robust; NFC
Add an isSmall query to Character so testing doesn't have to bake in internal format. Clarify the purpose of the invalid UTF-16 backdoor creation method.
This commit is contained in:
@@ -394,6 +394,12 @@ extension Character {
|
|||||||
guard case .large(let storage) = _representation else { return nil }
|
guard case .large(let storage) = _representation else { return nil }
|
||||||
return storage
|
return storage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@usableFromInline // @testable
|
||||||
|
internal var _isSmall: Bool {
|
||||||
|
guard case .smallUTF16(_) = _representation else { return false }
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Character {
|
extension Character {
|
||||||
|
|||||||
@@ -172,4 +172,13 @@ extension String {
|
|||||||
return String(_largeStorage: storage)
|
return String(_largeStorage: storage)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For testing purposes only, allow ourselves to have invalid contents
|
||||||
|
@usableFromInline // @testable
|
||||||
|
static internal
|
||||||
|
func _fromInvalidUTF16(_ cus: UnsafeBufferPointer<UInt16>) -> String {
|
||||||
|
let storage = _SwiftStringStorage<UTF16.CodeUnit>.create(
|
||||||
|
capacity: cus.count, count: cus.count)
|
||||||
|
_ = storage._initialize(fromCodeUnits: cus, encoding: UTF16.self)
|
||||||
|
return String(_StringGuts(_large: storage))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1255,19 +1255,6 @@ extension _StringGuts {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension _StringGuts {
|
|
||||||
// For testing purposes only. Might be both inefficient and too low-level.
|
|
||||||
// There should be an eventual API on String to accomplish something similar.
|
|
||||||
@usableFromInline // @_testable
|
|
||||||
static internal
|
|
||||||
func _createStringFromUTF16(_ cus: UnsafeBufferPointer<UInt16>) -> String {
|
|
||||||
let storage = _SwiftStringStorage<UTF16.CodeUnit>.create(
|
|
||||||
capacity: cus.count, count: cus.count)
|
|
||||||
_ = storage._initialize(fromCodeUnits: cus, encoding: UTF16.self)
|
|
||||||
return String(_StringGuts(_large: storage))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extension _SwiftStringStorage {
|
extension _SwiftStringStorage {
|
||||||
/// Initialize a piece of freshly allocated storage instance from a sequence
|
/// Initialize a piece of freshly allocated storage instance from a sequence
|
||||||
/// of code units, which is assumed to contain exactly as many code units as
|
/// of code units, which is assumed to contain exactly as many code units as
|
||||||
|
|||||||
@@ -231,12 +231,7 @@ func checkRoundTripThroughCharacter(_ s: String) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isSmallRepresentation(_ s: String) -> Bool {
|
func isSmallRepresentation(_ s: String) -> Bool {
|
||||||
switch Character(s)._representation {
|
return Character(s)._isSmall
|
||||||
case .smallUTF16:
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkUnicodeScalars(_ s: String) {
|
func checkUnicodeScalars(_ s: String) {
|
||||||
|
|||||||
@@ -37,8 +37,8 @@ let replacementCharacter = Character(replacementScalar)
|
|||||||
// UTF16, grapheme clusters composed of multiple Unicode scalars, and
|
// UTF16, grapheme clusters composed of multiple Unicode scalars, and
|
||||||
// invalid UTF16 that should be replaced with replacement characters.
|
// invalid UTF16 that should be replaced with replacement characters.
|
||||||
let winterUTF16 = Array("🏂☃❅❆❄︎⛄️❄️".utf16) + [0xD83C, 0x0020, 0xDF67, 0xD83C]
|
let winterUTF16 = Array("🏂☃❅❆❄︎⛄️❄️".utf16) + [0xD83C, 0x0020, 0xDF67, 0xD83C]
|
||||||
var winter = winterUTF16.withUnsafeBufferPointer { bufPtr in
|
var winter = winterUTF16.withUnsafeBufferPointer {
|
||||||
_StringGuts._createStringFromUTF16(bufPtr)
|
String._fromInvalidUTF16($0)
|
||||||
}
|
}
|
||||||
|
|
||||||
let winterInvalidUTF8: [UTF8.CodeUnit] = replacementUTF8 + ([0x20] as [UTF8.CodeUnit]) + replacementUTF8 + replacementUTF8
|
let winterInvalidUTF8: [UTF8.CodeUnit] = replacementUTF8 + ([0x20] as [UTF8.CodeUnit]) + replacementUTF8 + replacementUTF8
|
||||||
|
|||||||
Reference in New Issue
Block a user