Skip to content

Commit

Permalink
main: Replace last '\n' with '\0' before sorting (Issue universal-cta…
Browse files Browse the repository at this point in the history
…gs#1411)

internalSortTags() sorted lines including the last '\n'. This caused
unexpected results.

This fix is written by Masatake YAMATO.
universal-ctags#1411 (comment)
  • Loading branch information
k-takata committed May 28, 2017
1 parent 068b13c commit a8b4e2a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 12 additions & 2 deletions main/sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ static int compareTags (const void *const one, const void *const two)
}

static void writeSortedTags (
char **const table, const size_t numTags, const bool toStdout)
char **const table, const size_t numTags, const bool toStdout, bool newlineReplaced)
{
MIO *mio;
size_t i;
Expand All @@ -214,8 +214,12 @@ static void writeSortedTags (
* pattern) if this is not an xref file.
*/
if (i == 0 || Option.xref || strcmp (table [i], table [i-1]) != 0)
{
if (mio_puts (mio, table [i]) == EOF)
failedSort (mio, NULL);
else if (newlineReplaced)
mio_putc (mio, '\n');
}
}
if (toStdout)
mio_flush (mio);
Expand All @@ -228,6 +232,7 @@ extern void internalSortTags (const bool toStdout, MIO* mio, size_t numTags)
const char *line;
size_t i;
int (*cmpFunc)(const void *, const void *);
bool newlineReplaced = false;

/* Allocate a table of line pointers to be sorted.
*/
Expand Down Expand Up @@ -260,6 +265,11 @@ extern void internalSortTags (const bool toStdout, MIO* mio, size_t numTags)
failedSort (mio, "out of memory");
DebugStatement ( mallocSize += stringSize; )
strcpy (table [i], line);
if (table[i][line] == '\n')
{
table[i][line] = '\0';
newlineReplaced = true;
}
++i;
}
}
Expand All @@ -270,7 +280,7 @@ extern void internalSortTags (const bool toStdout, MIO* mio, size_t numTags)
*/
qsort (table, numTags, sizeof (*table), cmpFunc);

writeSortedTags (table, numTags, toStdout);
writeSortedTags (table, numTags, toStdout, newlineReplaced);

PrintStatus (("sort memory: %ld bytes\n", (long) mallocSize));
for (i = 0 ; i < numTags ; ++i)
Expand Down
2 changes: 1 addition & 1 deletion win32/appveyor.bat
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ perl -i".bak" -pe "s/^test -n \".DJDIR\"/#$&/" configure
bash -lc "./configure && make"

:: Restore VC binaries
copy vc\*.exe . /y > nul
cp vc\*.exe . /y > nul
touch *.exe

@echo off
Expand Down

0 comments on commit a8b4e2a

Please sign in to comment.