Use super trampolines to let us override several more NSString methods (-UTF8String, -cStringUsingEncoding:, and -getCString:maxLength:encoding:) for performance

This commit is contained in:
David Smith
2018-11-08 17:09:17 -08:00
parent 7a68a6905e
commit 8bb6b78e36
8 changed files with 225 additions and 17 deletions

View File

@@ -686,6 +686,18 @@ NSStringAPIs.test("getBytes(_:maxLength:usedLength:encoding:options:range:remain
NSStringAPIs.test("getCString(_:maxLength:encoding:)") {
let s = "abc あかさた"
do {
// A significantly too small buffer
let bufferLength = 1
var buffer = Array(
repeating: CChar(bitPattern: 0xff), count: bufferLength)
let result = s.getCString(&buffer, maxLength: 100,
encoding: .utf8)
expectFalse(result)
let result2 = s.getCString(&buffer, maxLength: 1,
encoding: .utf8)
expectFalse(result2)
}
do {
// The largest buffer that cannot accommodate the string plus null terminator.
let bufferLength = 16
@@ -694,6 +706,9 @@ NSStringAPIs.test("getCString(_:maxLength:encoding:)") {
let result = s.getCString(&buffer, maxLength: 100,
encoding: .utf8)
expectFalse(result)
let result2 = s.getCString(&buffer, maxLength: 16,
encoding: .utf8)
expectFalse(result2)
}
do {
// The smallest buffer where the result can fit.
@@ -708,6 +723,10 @@ NSStringAPIs.test("getCString(_:maxLength:encoding:)") {
encoding: .utf8)
expectTrue(result)
expectEqualSequence(expectedStr, buffer)
let result2 = s.getCString(&buffer, maxLength: 17,
encoding: .utf8)
expectTrue(result2)
expectEqualSequence(expectedStr, buffer)
}
do {
// Limit buffer size with 'maxLength'.