// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Which two bytes end a line, and how to get a body into and out of the form //! that says CRLF. //! //! RFC 2045 §6.1 calls CRLF the *canonical form* of a text body: the encoding //! is applied to text whose lines are already CRLF-terminated, and a decoder //! converts back to whatever the local convention is on the way out. On a //! platform whose files are LF-terminated that conversion is not decoration — //! a quoted-printable body encoded from LF text has a bare LF inside it, and a //! bare LF in a MIME body is exactly the thing the encoding exists to prevent. //! //! It only applies to `text/*`. A `Content-Type: application/octet-stream` //! body has no lines to canonicalise, and rewriting two bytes inside one would //! corrupt it. const std = @import("std"); const Io = std.Io; const testing = std.testing; pub const Ending = enum { crlf, lf, pub fn bytes(self: Ending) []const u8 { return switch (self) { .crlf => "\r\n", .lf => "\n", }; } }; /// The length `toCanonical` will write: every LF that is not already preceded /// by a CR gains one. pub fn canonicalLen(source: []const u8) usize { var len: usize = source.len; var previous: u8 = 0; for (source) |c| { if (c == '\n' and previous != '\r') len += 1; previous = c; } return len; } /// Writes `source` with every line ending as CRLF. A CR that is not part of a /// line ending is passed through untouched rather than being paired with the /// next LF, because a lone CR in text is a character and not a line break. pub fn toCanonical(w: *Io.Writer, source: []const u8) Io.Writer.Error!void { var start: usize = 0; var previous: u8 = 0; for (source, 0..) |c, i| { if (c == '\n' and previous != '\r') { try w.writeAll(source[start..i]); try w.writeAll("\r\n"); start = i + 1; } previous = c; } try w.writeAll(source[start..]); } /// Writes `source` with every CRLF reduced to `ending`. A CR on its own is /// left where it is: only the pair is a line ending. pub fn fromCanonical(w: *Io.Writer, source: []const u8, ending: Ending) Io.Writer.Error!void { if (ending == .crlf) return w.writeAll(source); var rest = source; while (std.mem.find(u8, rest, "\r\n")) |i| { try w.writeAll(rest[0..i]); try w.writeAll(ending.bytes()); rest = rest[i + 2 ..]; } try w.writeAll(rest); } test canonicalLen { try testing.expectEqual(0, canonicalLen("")); try testing.expectEqual(7, canonicalLen("a\nb\nc")); try testing.expectEqual(7, canonicalLen("a\r\nb\nc")); try testing.expectEqual(7, canonicalLen("a\r\nb\r\nc")); } test toCanonical { var out: Io.Writer.Allocating = .init(testing.allocator); defer out.deinit(); try toCanonical(&out.writer, "a\nb\r\nc\rd\n"); try testing.expectEqualStrings("a\r\nb\r\nc\rd\r\n", out.written()); try testing.expectEqual(canonicalLen("a\nb\r\nc\rd\n"), out.written().len); } test fromCanonical { var out: Io.Writer.Allocating = .init(testing.allocator); defer out.deinit(); try fromCanonical(&out.writer, "a\r\nb\rc\r\n", .lf); try testing.expectEqualStrings("a\nb\rc\n", out.written()); }