Skip to content

Commit cadd213

Browse files
committed
Fix getInputColumnNumber() on the last line with CRLF
See also universal-ctags#4266 and universal-ctags#4097.
1 parent 97254c3 commit cadd213

File tree

9 files changed

+33
-21
lines changed

9 files changed

+33
-21
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php function a() {} ?>
2+
<h1>A</h1>
3+
<?php function b() {} ?>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
input.php text eol=crlf
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--sort=no
2+
--extras=+g
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a input.php /^<?php function a() {} ?>$/;" f
2+
b input.php /^<?php function b() {} ?>$/;" f
3+
A input.php /^<h1>A<\/h1>$/;" h

main/read.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,24 @@ extern long translateFileOffset (unsigned long offset)
221221
return getAreaStartOffset () + offset;
222222
}
223223

224+
static compoundPos* getInputFileCompoundPosForLine (unsigned int line)
225+
{
226+
int index;
227+
if (line > 0)
228+
{
229+
if (File.lineFposMap.count > (line - 1))
230+
index = line - 1;
231+
else if (File.lineFposMap.count != 0)
232+
index = File.lineFposMap.count - 1;
233+
else
234+
index = 0;
235+
}
236+
else
237+
index = 0;
238+
239+
return File.lineFposMap.pos + index;
240+
}
241+
224242
extern int getInputColumnNumber (void)
225243
{
226244
int ret;
@@ -315,14 +333,17 @@ extern int getInputColumnNumber (void)
315333
* | |
316334
* +-----------------+
317335
*
318-
* Q => getInputFileOffsetForLine(File.input.lineNumber)
336+
* Q => getInputFileCompoundPosForLine(File.input.lineNumber)->offset - (File.bomFound? 3: 0)
337+
*
338+
* (File.bomFound? 3: 0) on each side cancel each other, so can be left out.
319339
*
320340
* dz = O + P - Q
321341
*/
322342
unsigned long ln = getInputLineNumber ();
343+
compoundPos *cpos = getInputFileCompoundPosForLine (ln);
323344
ret = getAreaStartOffset ()
324-
+ mio_tell (File.mio) - (File.bomFound? 3: 0)
325-
- getInputFileOffsetForLine(ln)
345+
+ mio_tell (File.mio)
346+
- cpos->offset
326347
- File.ungetchIdx;
327348
}
328349
else
@@ -353,24 +374,6 @@ extern MIOPos getInputFilePosition (void)
353374
return File.filePosition.pos;
354375
}
355376

356-
static compoundPos* getInputFileCompoundPosForLine (unsigned int line)
357-
{
358-
int index;
359-
if (line > 0)
360-
{
361-
if (File.lineFposMap.count > (line - 1))
362-
index = line - 1;
363-
else if (File.lineFposMap.count != 0)
364-
index = File.lineFposMap.count - 1;
365-
else
366-
index = 0;
367-
}
368-
else
369-
index = 0;
370-
371-
return File.lineFposMap.pos + index;
372-
}
373-
374377

375378
/* case areaCoord == AREA_COORD_CURRENT:
376379
* return: [current],

0 commit comments

Comments
 (0)