~subsetpark/erasmus

516aa628fa79c4ca0e6dd3c7dcd4310f9fb4198f — Zach Smith 2 months ago 4de7964
Handle all trailing punct in error
1 files changed, 12 insertions(+), 6 deletions(-)

M src/note.zig
M src/note.zig => src/note.zig +12 -6
@@ 4,14 4,20 @@ const util = @import("./util.zig");
const mem = std.mem;
const fmt = std.fmt;

const EscapeError = error{ TerminalPeriod, OutOfMemory };
const EscapeError = error{ TerminalPunctuation, OutOfMemory };

// Process a string and escape any delimiters for reference creation.
fn escapeAndLower(s: []const u8, allocator: mem.Allocator) !util.CharBuffer {
    // Check for illegal characters. Right now the only illegal character is a
    // terminal period, which confuses vim, so this is a bit particular.
    if (s.len > 0 and s[s.len - 1] == '.') {
        return EscapeError.TerminalPeriod;
    // trailing punctuation char, which confuses vim, so this is a bit
    // particular.
    if (s.len > 0) {
        switch (s[s.len - 1]) {
            '.', ',', ':', ';', '!' => {
                return EscapeError.TerminalPunctuation;
            },
            else => {},
        }
    }

    // Initialize double the length of `s`, covering the pessimal case where


@@ 37,8 43,8 @@ fn escapeAndLower(s: []const u8, allocator: mem.Allocator) !util.CharBuffer {

fn handleEscapeError(err: EscapeError) void {
    switch (err) {
        EscapeError.TerminalPeriod => {
            std.debug.print("Note references can't end with a period", .{});
        EscapeError.TerminalPunctuation => {
            std.debug.print("Note references can't have trailing punctuation", .{});
            std.os.exit(1);
        },
        error.OutOfMemory => {