about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMel <mel@rnrd.eu>2025-07-09 19:00:53 +0200
committerMel <mel@rnrd.eu>2025-07-09 19:00:53 +0200
commit7f2d00ae1889e10eea37153002cbf7296fba1993 (patch)
tree2a6490cad86cabad832870fe9c47ec40d03a3da7
parent6a65476f3f2dda2ba2238a14ecd7f086605e0e18 (diff)
downloadcatskill-7f2d00ae1889e10eea37153002cbf7296fba1993.tar.zst
catskill-7f2d00ae1889e10eea37153002cbf7296fba1993.zip
Shorten token_span_to_line_span and reduce edge case errors in it
Signed-off-by: Mel <mel@rnrd.eu>
-rw-r--r--boot/parse.c17
1 files 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);
 }