Menu

#111 strnrchr() code reduction, file info.cpp

0.61
closed
None
1
2016-12-11
2005-05-23
No

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---

Discussion

  • Kevin Atkinson

    Kevin Atkinson - 2005-05-31
    • assigned_to: nobody --> kevina
     
  • Kevin Atkinson

    Kevin Atkinson - 2005-05-31
    • priority: 5 --> 1
     
  • Jose Da Silva

    Jose Da Silva - 2006-12-12

    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

     
  • Jose Da Silva

    Jose Da Silva - 2007-10-20

    minor improvement to strnchr() - no segfault on size==0

     
  • Jose Da Silva

    Jose Da Silva - 2007-10-20

    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

     
  • Jose Da Silva

    Jose Da Silva - 2007-10-20

    2 minor simplifications

     
  • Jose Da Silva

    Jose Da Silva - 2007-10-20

    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

     
  • Kevin Atkinson

    Kevin Atkinson - 2011-06-26
    • milestone: --> 0.61
     
  • Kevin Atkinson

    Kevin Atkinson - 2016-12-11

    This issue has moved to GitHub: https://github.com/GNUAspell/aspell/issues/294

     
  • Kevin Atkinson

    Kevin Atkinson - 2016-12-11
    • Status: open --> closed