mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-25 04:47:51 +00:00
Store ParserError.file as StaticString
#fileID exceeds the small-string limit, so a String heap-allocates on every throw; parser errors are ordinary control flow.
This commit is contained in:
2 files changed
+13
-7
No files matched your search
@@ -53,7 +53,7 @@ public struct BadCommand: Error {
|
||||
///
|
||||
/// - SeeAlso: [RFC 3501 Section 4](https://datatracker.ietf.org/doc/html/rfc3501#section-4) (grammar)
|
||||
public struct ParserError: Error {
|
||||
static func invalidUTF8(file: String = (#fileID), line: Int = #line) -> Self {
|
||||
static func invalidUTF8(file: StaticString = (#fileID), line: Int = #line) -> Self {
|
||||
ParserError(hint: "Invalid UTF8", file: file, line: line)
|
||||
}
|
||||
|
||||
@@ -63,10 +63,12 @@ public struct ParserError: Error {
|
||||
/// "Invalid UTF8", "Missing CRLF", "Unexpected character", etc. It's intended
|
||||
/// for logging and debugging purposes.
|
||||
public var hint: String
|
||||
var file: String
|
||||
/// `StaticString` rather than `String`: `#fileID` is longer than the 15-byte small-string limit,
|
||||
/// so a `String` here would heap-allocate and retain on *every* throw, hint or no hint.
|
||||
var file: StaticString
|
||||
var line: Int
|
||||
|
||||
init(hint: String = "Unknown", file: String = (#fileID), line: Int = #line) {
|
||||
init(hint: String = "Unknown", file: StaticString = (#fileID), line: Int = #line) {
|
||||
self.hint = hint
|
||||
self.file = file
|
||||
self.line = line
|
||||
|
||||
@@ -79,7 +79,11 @@ extension ParseBuffer {
|
||||
extension ParserLibrary {
|
||||
/// Throws `ParserError.invalidUTF8` if the given `ByteBuffer` doesn't
|
||||
/// contain a valid UTF8 sequence.
|
||||
static func parseBufferAsUTF8(_ buffer: ByteBuffer, file: String = (#fileID), line: Int = #line) throws -> String {
|
||||
static func parseBufferAsUTF8(
|
||||
_ buffer: ByteBuffer,
|
||||
file: StaticString = (#fileID),
|
||||
line: Int = #line
|
||||
) throws -> String {
|
||||
guard let string = String(validatingUTF8Bytes: buffer.readableBytesView) else {
|
||||
throw ParserError.invalidUTF8(file: file, line: line)
|
||||
}
|
||||
@@ -304,7 +308,7 @@ extension ParserLibrary {
|
||||
_ subParsers: [SubParser<T>],
|
||||
buffer: inout ParseBuffer,
|
||||
tracker: StackTracker,
|
||||
file: String = (#fileID),
|
||||
file: StaticString = (#fileID),
|
||||
line: Int = #line
|
||||
) throws -> T {
|
||||
for parser in subParsers {
|
||||
@@ -324,7 +328,7 @@ extension ParserLibrary {
|
||||
_ parser2: SubParser<T>,
|
||||
buffer: inout ParseBuffer,
|
||||
tracker: StackTracker,
|
||||
file: String = (#fileID),
|
||||
file: StaticString = (#fileID),
|
||||
line: Int = #line
|
||||
) throws -> T {
|
||||
do {
|
||||
@@ -349,7 +353,7 @@ extension ParserLibrary {
|
||||
_ parser3: SubParser<T>,
|
||||
buffer: inout ParseBuffer,
|
||||
tracker: StackTracker,
|
||||
file: String = (#fileID),
|
||||
file: StaticString = (#fileID),
|
||||
line: Int = #line
|
||||
) throws -> T {
|
||||
do {
|
||||
|
||||
Reference in new issue
Block a user