diff options
author | Jo-Philipp Wich <jo@mein.io> | 2023-07-12 01:02:14 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-12 01:02:14 +0200 |
commit | e06f656f5043d5b86f6ccfb7f9b8447800623659 (patch) | |
tree | 8cc3508e5c2b10c34190571161f46d879016eed2 /source.c | |
parent | 62775f4a4492ec2d6f19f2bc0af882de041b0bb7 (diff) | |
parent | 24f1a5617ae3976313ace91970d1994a1ae7cc89 (diff) |
Merge pull request #158 from jow-/offset-accouting
Fixes for source offset tracking
Diffstat (limited to 'source.c')
-rw-r--r-- | source.c | 30 |
1 files changed, 17 insertions, 13 deletions
@@ -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) |