Skip to content

Commit 96de131

Browse files
committed
Delegate string conversion to ICU only when encoding is EUC-JP.
In response to: #1296 (comment)
1 parent ba59831 commit 96de131

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

Sources/FoundationEssentials/String/String+IO.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@ extension String {
189189
}
190190
}
191191
self = bytes.withContiguousStorageIfAvailable(buildString) ?? Array(bytes).withUnsafeBufferPointer(buildString)
192+
case .japaneseEUC:
193+
// Here we catch encodings that are supported by Foundation Framework
194+
// but are not supported by corelibs-foundation.
195+
// We delegate conversion to ICU.
196+
guard let string = (
197+
bytes.withContiguousStorageIfAvailable({ _icuMakeStringFromBytes($0, encoding: encoding) }) ??
198+
Array(bytes).withUnsafeBufferPointer({ _icuMakeStringFromBytes($0, encoding: encoding) })
199+
) else {
200+
return nil
201+
}
202+
self = string
192203
#endif
193204
default:
194205
#if FOUNDATION_FRAMEWORK
@@ -207,14 +218,8 @@ extension String {
207218
return nil
208219
}
209220
#else
210-
func makeString(from bytes: UnsafeBufferPointer<UInt8>) -> String? {
211-
return (
212-
_cfMakeStringFromBytes(bytes, encoding: encoding.rawValue) ??
213-
_icuMakeStringFromBytes(bytes, encoding: encoding)
214-
)
215-
}
216-
if let string = (bytes.withContiguousStorageIfAvailable({ makeString(from: $0) }) ??
217-
Array(bytes).withUnsafeBufferPointer({ makeString(from: $0) })) {
221+
if let string = (bytes.withContiguousStorageIfAvailable({ _cfMakeStringFromBytes($0, encoding: encoding.rawValue) }) ??
222+
Array(bytes).withUnsafeBufferPointer({ _cfMakeStringFromBytes($0, encoding: encoding.rawValue) })) {
218223
self = string
219224
} else {
220225
return nil

Sources/FoundationEssentials/String/StringProtocol+Essentials.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,18 +254,19 @@ extension String {
254254
buffer.appendElement(value)
255255
}
256256
}
257+
case .japaneseEUC:
258+
// Here we catch encodings that are supported by Foundation Framework
259+
// but are not supported by corelibs-foundation.
260+
// We delegate conversion to ICU.
261+
return _icuStringEncodingConvert(string: self, using: encoding, allowLossyConversion: allowLossyConversion)
257262
#endif
258263
default:
259264
#if FOUNDATION_FRAMEWORK
260265
// Other encodings, defer to the CoreFoundation implementation
261266
return _ns.data(using: encoding.rawValue, allowLossyConversion: allowLossyConversion)
262267
#else
263-
return (
264-
// Attempt an up-call into swift-corelibs-foundation, which can defer to the CoreFoundation implementation
265-
_cfStringEncodingConvert(string: self, using: encoding.rawValue, allowLossyConversion: allowLossyConversion) ??
266-
// Or attempt an up-call into ICU via FoundationInternationalization
267-
_icuStringEncodingConvert(string: self, using: encoding, allowLossyConversion: allowLossyConversion)
268-
)
268+
// Attempt an up-call into swift-corelibs-foundation, which can defer to the CoreFoundation implementation
269+
return _cfStringEncodingConvert(string: self, using: encoding.rawValue, allowLossyConversion: allowLossyConversion)
269270
#endif
270271
}
271272
}

0 commit comments

Comments
 (0)