summaryrefslogtreecommitdiffhomepage
path: root/source.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2023-07-11 12:12:21 +0200
committerJo-Philipp Wich <jo@mein.io>2023-07-12 00:38:49 +0200
commit24f1a5617ae3976313ace91970d1994a1ae7cc89 (patch)
tree8cc3508e5c2b10c34190571161f46d879016eed2 /source.c
parent9df91602e3aba0edc4771b220663f442f836f986 (diff)
source: fix source offset accounting
- When skipping the interpreter line, don't count it's newline twice - Fix reporting byte offsets beyond the end of line Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'source.c')
-rw-r--r--source.c30
1 files changed, 17 insertions, 13 deletions
diff --git a/source.c b/source.c
index 3bdc210..ba218ff 100644
--- a/source.c
+++ b/source.c
@@ -74,22 +74,23 @@ size_t
uc_source_get_line(uc_source_t *source, size_t *offset)
{
uc_lineinfo_t *lines = &source->lineinfo;
- size_t i, pos = 0, line = 0, lastoff = 0;
+ size_t i, pos = 0, line = 1, lastoff = 0;
- for (i = 0; i < lines->count; i++) {
- if (lines->entries[i] & 0x80) {
- lastoff = pos;
+ for (i = 0; i <= lines->count; i++) {
+ if (pos >= *offset || i == lines->count) {
+ *offset = (*offset - lastoff) + 1;
+
+ return line;
+ }
+
+ /* don't count first line jump as actual byte */
+ if (i > 0 && (lines->entries[i] & 0x80)) {
line++;
pos++;
+ lastoff = pos;
}
pos += (lines->entries[i] & 0x7f);
-
- if (pos >= *offset) {
- *offset -= lastoff - 1;
-
- return line;
- }
}
return 0;
@@ -125,10 +126,13 @@ uc_source_type_test(uc_source_t *source)
type = UC_SOURCE_TYPE_PRECOMPILED;
}
else {
- uc_source_line_update(source, source->off);
-
- if (c == '\n')
+ if (c == '\n') {
+ uc_source_line_update(source, source->off - 1);
uc_source_line_next(source);
+ }
+ else {
+ uc_source_line_update(source, source->off);
+ }
}
if (fseek(fp, -(long)rlen, SEEK_CUR) == -1)