Bugfix multiline off-by-one (locfile.c)
This commit is contained in:
1 parent
40378a18f7
commit
8cfe11e4e2
2 files changed
+11
-12
No files matched your search
@@ -1,5 +1,7 @@
|
|||||||
*.o
|
*.o
|
||||||
*~
|
*~
|
||||||
|
.*.sw[a-p]
|
||||||
|
tags
|
||||||
|
|
||||||
jq
|
jq
|
||||||
jq.1
|
jq.1
|
||||||
|
|||||||
@@ -18,11 +18,11 @@ void locfile_init(struct locfile* l, const char* data, int length) {
|
|||||||
int line = 1;
|
int line = 1;
|
||||||
for (int i=0; i<length; i++) {
|
for (int i=0; i<length; i++) {
|
||||||
if (data[i] == '\n') {
|
if (data[i] == '\n') {
|
||||||
l->linemap[line] = i;
|
l->linemap[line] = i+1; // at start of line, not of \n
|
||||||
line++;
|
line++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
l->linemap[l->nlines] = length;
|
l->linemap[l->nlines] = length+1; // virtual last \n
|
||||||
}
|
}
|
||||||
|
|
||||||
void locfile_free(struct locfile* l) {
|
void locfile_free(struct locfile* l) {
|
||||||
@@ -31,15 +31,15 @@ void locfile_free(struct locfile* l) {
|
|||||||
|
|
||||||
static int locfile_get_line(struct locfile* l, int pos) {
|
static int locfile_get_line(struct locfile* l, int pos) {
|
||||||
assert(pos < l->length);
|
assert(pos < l->length);
|
||||||
int line = 0;
|
int line = 1;
|
||||||
while (l->linemap[line+1] < pos) line++;
|
while (l->linemap[line] <= pos) line++; // == if pos at start (before, never ==, because pos never on \n)
|
||||||
assert(line < l->nlines);
|
assert(line-1 < l->nlines);
|
||||||
return line;
|
return line-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int locfile_line_length(struct locfile* l, int line) {
|
static int locfile_line_length(struct locfile* l, int line) {
|
||||||
assert(line < l->nlines);
|
assert(line < l->nlines);
|
||||||
return l->linemap[line+1] - l->linemap[line];
|
return l->linemap[line+1] - l->linemap[line] -1; // -1 to omit \n
|
||||||
}
|
}
|
||||||
|
|
||||||
void locfile_locate(struct locfile* l, location loc, const char* fmt, ...) {
|
void locfile_locate(struct locfile* l, location loc, const char* fmt, ...) {
|
||||||
@@ -54,11 +54,8 @@ void locfile_locate(struct locfile* l, location loc, const char* fmt, ...) {
|
|||||||
}
|
}
|
||||||
int startline = locfile_get_line(l, loc.start);
|
int startline = locfile_get_line(l, loc.start);
|
||||||
int offset = l->linemap[startline];
|
int offset = l->linemap[startline];
|
||||||
fprintf(stderr, "HERE1\n%.*s\n", locfile_line_length(l, startline)-(startline!=0), l->data + offset +(startline!=0)); // if not first line, this starts at the '\n' in l
|
fprintf(stderr, "%.*s\n", locfile_line_length(l, startline), l->data + offset);
|
||||||
fprintf(stderr, "HERE2\n%*s", loc.start - offset -(startline!=0), ""); // space padding. If not first line, offset is the '\n' at beginning of line, and one too many
|
fprintf(stderr, "%*s", loc.start - offset, "");
|
||||||
/* ASIDE: because all this code is in locfile.h instead of locfile.c, it recompiles everything, and takes forever */
|
|
||||||
/* I've separated it out, into locfile.h; removed static */
|
|
||||||
/* Problem: a few files include locfile.h to get the *.h it includes... this seems bad to me */
|
|
||||||
for (int i = loc.start;
|
for (int i = loc.start;
|
||||||
i < loc.end && i < offset + locfile_line_length(l, startline);
|
i < loc.end && i < offset + locfile_line_length(l, startline);
|
||||||
i++){
|
i++){
|
||||||
|
|||||||
Reference in new issue
Block a user