###Bug###
Bug
You never reset i
back to TABSPACES - 1
, so only the first tab will be placed correctly. The rest of the time, you will convert four spaces into three spaces plus a tab.
###Inefficient moving of tail###
Inefficient moving of tail
Every time you replace four spaces with a tab, you have a loop that moves the tail of the string forward one step at a time, three times total. First of all, you could speed that up 3x by moving the tail three steps in one pass, perhaps with one call to memmove()
. Second of all, you could avoid the move altogether if you just moved each character into place like this:
while (mgetline(line, MAXLINESIZE) > 0) {
char *tail = linep;
while (*linep) {
*tail++ = *linep;
if (*linep++ == ' ') {
if (++tab == TABSPACES) {
// Erase spaces already copied to tail.
tail -= TABSPACES;
// Replace with tab instead.
*tail++ = '\t';
tab = 0;
}
} else {
tab = 0;
}
}
printf("%s", line);
linep = line;
}
With this solution you can get rid of your kick()
, countx()
, and swap()
functions.
###Bug###
You never reset i
back to TABSPACES - 1
, so only the first tab will be placed correctly. The rest of the time, you will convert four spaces into three spaces plus a tab.
###Inefficient moving of tail###
Every time you replace four spaces with a tab, you have a loop that moves the tail of the string forward one step at a time, three times total. First of all, you could speed that up 3x by moving the tail three steps in one pass, perhaps with one call to memmove()
. Second of all, you could avoid the move altogether if you just moved each character into place like this:
while (mgetline(line, MAXLINESIZE) > 0) {
char *tail = linep;
while (*linep) {
*tail++ = *linep;
if (*linep++ == ' ') {
if (++tab == TABSPACES) {
// Erase spaces already copied to tail.
tail -= TABSPACES;
// Replace with tab instead.
*tail++ = '\t';
tab = 0;
}
} else {
tab = 0;
}
}
printf("%s", line);
linep = line;
}
With this solution you can get rid of your kick()
, countx()
, and swap()
functions.
Bug
You never reset i
back to TABSPACES - 1
, so only the first tab will be placed correctly. The rest of the time, you will convert four spaces into three spaces plus a tab.
Inefficient moving of tail
Every time you replace four spaces with a tab, you have a loop that moves the tail of the string forward one step at a time, three times total. First of all, you could speed that up 3x by moving the tail three steps in one pass, perhaps with one call to memmove()
. Second of all, you could avoid the move altogether if you just moved each character into place like this:
while (mgetline(line, MAXLINESIZE) > 0) {
char *tail = linep;
while (*linep) {
*tail++ = *linep;
if (*linep++ == ' ') {
if (++tab == TABSPACES) {
// Erase spaces already copied to tail.
tail -= TABSPACES;
// Replace with tab instead.
*tail++ = '\t';
tab = 0;
}
} else {
tab = 0;
}
}
printf("%s", line);
linep = line;
}
With this solution you can get rid of your kick()
, countx()
, and swap()
functions.
###Bug###
You never reset i
back to TABSPACES - 1
, so only the first tab will be placed correctly. The rest of the time, you will convert four spaces into three spaces plus a tab.
###Inefficient moving of tail###
Every time you replace four spaces with a tab, you have a loop that moves the tail of the string forward one step at a time, three times total. First of all, you could speed that up 3x by moving the tail three steps in one pass, perhaps with one call to memmove()
. Second of all, you could avoid the move altogether if you just moved each character into place like this:
while (mgetline(line, MAXLINESIZE) > 0) {
char *tail = linep;
while (*linep) {
*tail++ = *linep;
if (*linep++ == ' ') {
if (++tab == TABSPACES) {
// Erase spaces already copied to tail.
tail -= TABSPACES;
// Replace with tab instead.
*tail++ = '\t';
tab = 0;
}
} else {
tab = 0;
}
}
printf("%s", line);
linep = line;
}
With this solution you can get rid of your kick()
, countx()
, and swap()
functions.