0

I have two different char *, char *string1 which is constant and char *string2 which can change. I retrieve char *string2 from a list.

I want to find the length of the shortest char * to use it in:

strncmp(string1, string2, shortest);.

This will be in a while-loop like below:

...
int shortest;
while (string2) {
    // get the length of the shortest char *
    if (!strncmp(string1, string2, shortest))
        break;    
    string2 = list_next(list); // Returns NULL if there is no elements left
}
...

I can't use strlen(const char *s) because it's too slow for my usecase.

ZeppRock
  • 988
  • 1
  • 10
  • 22
  • 7
    "I can't use strlen(const char *s) because it's too slow for my usecase." Have you benchmarked it? `strlen` isn't typically a complicated function, and I can't imagine any function you write being significantly faster. – Thomas Jager Jul 11 '19 at 13:29
  • 1
    The only faster way I can think of is if you store the length. C strings are null terminated, so getting the length is always O(n). Nothing will be faster than `strlen`. – Fred Larson Jul 11 '19 at 13:30
  • 1
    Have you measured, benchmarked and profiled to make sure this is a hot-spot and serious bottleneck in your program? Don't optimize without measuring and profiling, and only optimize the worst bottlenecks (with plenty of documentation and comments). – Some programmer dude Jul 11 '19 at 13:30
  • 1
    And in your case, why get the length at all? Why not use plain `strcmp` as it will stop when the shortest string ends anyway. – Some programmer dude Jul 11 '19 at 13:32
  • @Someprogrammerdude I need to know if the first `n` characters are the same where `n` is the length of the shortest string, `strcmp` can't do that. – ZeppRock Jul 11 '19 at 13:40
  • So you need to know the length anyway? Then please add it as a requirement in the question body. – Some programmer dude Jul 11 '19 at 14:26
  • "I can't use strlen(const char *s) because it's too slow for my usecase." --> that is incorrect assessment of your _overall_ usecase. – chux - Reinstate Monica Jul 11 '19 at 16:52

2 Answers2

2

Create a struct that contains the pointer and the length. Then you have the length precalculated and checking it should be fast.

An even better idea is to use someone else's string library that already does this for you. Besides calculating string length most of the libraries vastly improve C's buffer security by avoiding the standard string operations.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • Thanks! Did not think to precalculate the length of the string, this should solve my problem where I only have to calculate the length once when I initialize the struct. – ZeppRock Jul 11 '19 at 13:43
1

For the specific case of strncmp, you could implement the comparison function yourself to return the desired result, e.g.:

bool strprefixeq(const char *a, const char *b) {
    while (*a && *b) {
        if (*a++ != *b++) {
            return false;
        }
    }
    return true;
}

(Of course if you have other need for the string length, it is better to precalculate and store it, as suggested.)

Arkku
  • 41,011
  • 10
  • 62
  • 84