mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-22 04:30:43 +00:00
Use NFC for ModifiedUTF7.encode (#884)
This commit is contained in:
1 parent
b2c94a8bce
commit
6b1b196cd6
2 files changed
+27
No files matched your search
@@ -63,11 +63,16 @@ public enum ModifiedUTF7: Sendable {
|
||||
/// non-ASCII characters are represented using Base64 encoding with `&` as the
|
||||
/// escape character.
|
||||
///
|
||||
/// The string is first converted to Unicode Normalization Form C (NFC),
|
||||
/// e.g. `"e\u{301}"` encodes the same as `"\u{E9}"`.
|
||||
///
|
||||
/// - Parameter string: The Unicode string to encode.
|
||||
/// - Returns: A `ByteBuffer` containing modified UTF-7 bytes.
|
||||
///
|
||||
/// - SeeAlso: [RFC 3501 Section 5.1.3](https://datatracker.ietf.org/doc/html/rfc3501#section-5.1.3)
|
||||
/// - SeeAlso: [RFC 5198 Section 2](https://datatracker.ietf.org/doc/html/rfc5198#section-2)
|
||||
static func encode(_ string: String) -> ByteBuffer {
|
||||
let string = string.nfcNormalized
|
||||
var buffer = ByteBuffer()
|
||||
buffer.reserveCapacity(string.utf8.count)
|
||||
|
||||
@@ -175,3 +180,17 @@ extension ModifiedUTF7 {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension String {
|
||||
/// UTF-8 lead byte of U+0300, the first scalar that may not be NFC.
|
||||
private static let firstNonNFCSafeUTF8Byte: UInt8 = 0xCC
|
||||
|
||||
/// The string in Unicode Normalization Form C (NFC), as required by RFC 5198.
|
||||
fileprivate var nfcNormalized: String {
|
||||
// Scalars below U+0300 are always NFC. Skips the conversion for e.g. ASCII.
|
||||
guard self.utf8.contains(where: { $0 >= Self.firstNonNFCSafeUTF8Byte }) else {
|
||||
return self
|
||||
}
|
||||
return String(decoding: self._nfcCodeUnits, as: UTF8.self)
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,12 @@ struct ModifiedUTF7Tests {
|
||||
("\u{7F}", "&AH8-"),
|
||||
("é\u{1F}", "&AOkAHw-"),
|
||||
("é\u{7F}", "&AOkAfw-"),
|
||||
// Converted to NFC (RFC 5198): decomposed "é" encodes as U+00E9.
|
||||
("Re\u{301}pertoire", "R&AOk-pertoire"),
|
||||
("e\u{301}", "&AOk-"),
|
||||
("\u{212B}", "&AMU-"),
|
||||
// Hangul jamo compose to U+D55C.
|
||||
("\u{1112}\u{1161}\u{11AB}", "&1Vw-"),
|
||||
]
|
||||
)
|
||||
func encodeConvertsStringsToModifiedUTF7(input: String, expected: String) {
|
||||
@@ -102,6 +108,8 @@ struct ModifiedUTF7Tests {
|
||||
arguments: [
|
||||
"&Jjo!",
|
||||
"&U,BTFw-&ZeVnLIqe-",
|
||||
// Not NFC: "e" + U+0301.
|
||||
"e&AwE-",
|
||||
]
|
||||
)
|
||||
func validateRejectsInvalidModifiedUTF7Strings(input: String) {
|
||||
|
||||
Reference in new issue
Block a user