Enable splitting a mailbox name (#252)

* Enable splitting a mailbox name

* Extra test cases

* Fix sanity

* Improve docs

* Update MailboxInfo.swift

* Update MailboxName.swift
This commit is contained in:
David Evans authored and GitHub committed 2020-07-09 14:48:47 +01:00
1 parent 93a801b650
commit 0c304df4e2
3 files changed
+62

No files matched your search

@@ -27,6 +27,17 @@ public struct MailboxInfo: Equatable {
self.mailbox = mailbox
self.extensions = extensions
}
/// Splits `mailbox` into constituent path components using the `PathSeparator`. Conversion is lossy and
/// for display purposes only, do not use the return value as a mailbox name.
/// The conversion to display string using heuristics to determine if the byte stream is the modified version of UTF-7 encoding defined in RFC 2152 (which it should be according to RFC 3501) — or if it is UTF-8 data. Many email clients erroneously encode mailbox names as UTF-8.
/// - returns: [`String`] containing path components
public func displayStringComponents() -> [String] {
guard let separator = self.pathSeparator else {
return [String(buffer: mailbox.storage)]
}
return self.mailbox.displayStringComponents(separator: separator)
}
}
// MARK: - Types
@@ -50,6 +50,19 @@ public struct MailboxName: Equatable {
self.storage = bytes
}
}
/// Splits `mailbox` into constituent path components using the `PathSeparator`. Conversion is lossy and
/// for display purposes only, do not use the return value as a mailbox name.
/// The conversion to display string using heuristics to determine if the byte stream is the modified version of UTF-7 encoding defined in RFC 2152 (which it should be according to RFC 3501) — or if it is UTF-8 data. Many email clients erroneously encode mailbox names as UTF-8.
/// - returns: [`String`] containing path components
public func displayStringComponents(separator: Character, omittingEmptySubsequences: Bool = true) -> [String] {
guard let first = separator.asciiValue else {
preconditionFailure("Cannot split on a non-ascii character")
}
return self.storage.readableBytesView
.split(separator: first, omittingEmptySubsequences: omittingEmptySubsequences)
.map { String(decoding: $0, as: Unicode.UTF8.self) }
}
}
// MARK: - CustomStringConvertible
@@ -0,0 +1,38 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
@testable import NIOIMAPCore
import XCTest
class MailboxName_Tests: EncodeTestClass {}
// MARK: - displayStringComponents
extension MailboxName_Tests {
func testSplitting() {
let inputs: [(MailboxName, Character, Bool, [String], UInt)] = [
(.init("ABC"), .init("B"), true, ["A", "C"], #line),
(.init("ABC"), .init("D"), true, ["ABC"], #line),
(.init(""), .init("D"), true, [], #line),
(.init("some/real/mailbox"), .init("/"), true, ["some", "real", "mailbox"], #line),
(.init("mailbox#test"), .init("#"), true, ["mailbox", "test"], #line),
(.init("//test1//test2//"), .init("/"), true, ["test1", "test2"], #line),
(.init("//test1//test2//"), .init("/"), false, ["", "", "test1", "", "test2", "", ""], #line),
]
for (name, character, ommitEmpty, expected, line) in inputs {
XCTAssertEqual(name.displayStringComponents(separator: character, omittingEmptySubsequences: ommitEmpty), expected, line: line)
}
}
}