From 0c304df4e2bbff0a2bb3d87c681187abb444e45e Mon Sep 17 00:00:00 2001 From: David Evans Date: Thu, 9 Jul 2020 14:48:47 +0100 Subject: [PATCH] Enable splitting a mailbox name (#252) * Enable splitting a mailbox name * Extra test cases * Fix sanity * Improve docs * Update MailboxInfo.swift * Update MailboxName.swift --- .../Grammar/Mailbox/MailboxInfo.swift | 11 ++++++ .../Grammar/Mailbox/MailboxName.swift | 13 +++++++ .../Grammar/Mailbox/MailboxName+Tests.swift | 38 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 Tests/NIOIMAPCoreTests/Grammar/Mailbox/MailboxName+Tests.swift diff --git a/Sources/NIOIMAPCore/Grammar/Mailbox/MailboxInfo.swift b/Sources/NIOIMAPCore/Grammar/Mailbox/MailboxInfo.swift index b4dce1a2..ed27c3fd 100644 --- a/Sources/NIOIMAPCore/Grammar/Mailbox/MailboxInfo.swift +++ b/Sources/NIOIMAPCore/Grammar/Mailbox/MailboxInfo.swift @@ -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 diff --git a/Sources/NIOIMAPCore/Grammar/Mailbox/MailboxName.swift b/Sources/NIOIMAPCore/Grammar/Mailbox/MailboxName.swift index c19cc542..b779cf7d 100644 --- a/Sources/NIOIMAPCore/Grammar/Mailbox/MailboxName.swift +++ b/Sources/NIOIMAPCore/Grammar/Mailbox/MailboxName.swift @@ -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 diff --git a/Tests/NIOIMAPCoreTests/Grammar/Mailbox/MailboxName+Tests.swift b/Tests/NIOIMAPCoreTests/Grammar/Mailbox/MailboxName+Tests.swift new file mode 100644 index 00000000..02bdac8c --- /dev/null +++ b/Tests/NIOIMAPCoreTests/Grammar/Mailbox/MailboxName+Tests.swift @@ -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) + } + } +}