From 7f2d00ae1889e10eea37153002cbf7296fba1993 Mon Sep 17 00:00:00 2001 From: Mel Date: Wed, 9 Jul 2025 19:00:53 +0200 Subject: Shorten token_span_to_line_span and reduce edge case errors in it Signed-off-by: Mel --- boot/parse.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/boot/parse.c b/boot/parse.c index 2891a0f..aeb4d88 100644 --- a/boot/parse.c +++ b/boot/parse.c @@ -100,17 +100,12 @@ parser_error_is_none(const struct Parser_Error* error) struct Span token_span_to_line_span(struct Span span, struct String source) { - Pos line_start = span.start + 1, line_end = span.end - 1; - // expand `line_start` to start from the beginning of the line, - // and `line_end` to end at the end of the line. - while (line_start > 0) { - if (string_at(source, line_start - 1) == '\n') break; - line_start--; - } - while (line_end < string_length(source)) { - if (string_at(source, line_end) == '\n') break; - line_end++; - } + Pos line_start = span.start, line_end = span.start; + + // go backwards from the start of the token's span to find line start. + while (line_start > 0 && string_at(source, line_start - 1) != '\n') line_start--; + // go forwards from the end of the token's span to find line end. + while (line_end < string_length(source) && string_at(source, line_end) != '\n') line_end++; return span_new(line_start, line_end); } -- cgit 1.4.1