Minor code simplification.
Based on today's CVS (May 23, 2005) .
Below is code changed on info.cpp and below that is a test
program to verify modification.
---------------code changed to this on info.cpp---
static const char * strnrchr(const char * stop, char c,
unsigned int size)
{
const char * i = stop + size;
while (i != stop) {
if (*--i == c)
return i;
}
return 0;
}
---------------test program to test old/new routines---
#include <stdio.h>
char str1[10] = "abcdefghi";
static const char * strnrchr_old(const char * stop, char
c, unsigned int size)
{
const char * i = stop + size - 1;
--stop;
while (i != stop) {
if (*i == c)
return i;
--i;
}
return 0;
}
static const char * strnrchr(const char * stop, char c,
unsigned int size)
{
const char * i = stop + size;
while (i != stop) {
if (*--i == c)
return i;
}
return 0;
}
int main(int argc, char** argv) {
printf("Linux is cool.\n");
/* try various conditions of values here */
printf("%s %s\n", strnrchr_old(str1,'i',8),
strnrchr(str1,'i',8));
return 0;
}
---------------end of main test program---
Logged In: YES
user_id=1138138
Originator: YES
...just collecting another reduction together with an existing reduction.
This one is for config.cpp.
Basically, the code:
while (i != end) {
if (strcmp(key, i->name) == 0)
return i;
++i;
}
return i;
can be written the same as:
while (i != end) {
if (strcmp(key, i->name) != 0)
++i;
else
return i;
}
return i;
or written as:
while (i != end && strcmp(key, i->name) != 0) {
++i;
}
return i;
...which simplifies to:
while (i != end && strcmp(key, i->name))
++i;
return i;
File Added: config.cpp.diff
minor improvement to strnchr() - no segfault on size==0
Logged In: YES
user_id=1138138
Originator: YES
updated info.cpp to have *(--i)
Based against CVS:
http://cvs.savannah.gnu.org/viewvc/\*checkout*/aspell/aspell/common/info.cpp?revision=1.39&pathrev=MAIN
Also note: the original info.cpp may contain an illegal operation if (size==0) because the pointer is loaded from location {const char * i = stop - 1;} which may be located out of permissive bounds.
This diff contains fewer operation for the same result.
File Added: info.cpp.diff
2 minor simplifications
Logged In: YES
user_id=1138138
Originator: YES
Simplified config.cpp further, against CVS:
http://cvs.savannah.gnu.org/viewvc/\*checkout*/aspell/aspell/common/config.cpp?revision=1.79&pathrev=MAIN
File Added: config.cpp.diff
This issue has moved to GitHub: https://github.com/GNUAspell/aspell/issues/294