diff options
| author | Mel <mel@rnrd.eu> | 2025-07-09 19:00:53 +0200 |
|---|---|---|
| committer | Mel <mel@rnrd.eu> | 2025-07-09 19:00:53 +0200 |
| commit | 7f2d00ae1889e10eea37153002cbf7296fba1993 (patch) | |
| tree | 2a6490cad86cabad832870fe9c47ec40d03a3da7 /boot | |
| parent | 6a65476f3f2dda2ba2238a14ecd7f086605e0e18 (diff) | |
| download | catskill-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>
Diffstat (limited to 'boot')
| -rw-r--r-- | boot/parse.c | 17 |
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); } |
