Leo
Leo

Reputation: 1139

Check for substring in a buffer

I use a buffer to receive data from a serial port. The buffer has a fixed length which is 100 and when I receive data I store the length of this data in a variable (index).

I would like to check if the buffer from 0 to index-1 contains a substring.

I've read that I could possibly do it using strstr and strdup but I have two issues with this. First I don't know how to extract a string from buffer and second it would be better if I didn't have to copy the string just to delete it one line later when I look for a substring in it.

My variables are the following:

char output[100];
int index = 0;
char* substring;

And I'd like a function that would return true if substring is in output[0:index] and false otherwise.

Any help or lead would be very appreciated!

Edit: I'm writing a piece of code for an atmel µC so it seems as I'm limited to those functions.

Edit 2: Actually it seems like I can use all the functions from the standard string.h

Edit 3: I checked for sure in my dependencies and under are all the functions I can call.

extern int ffs (int __val) __ATTR_CONST__;
extern int ffsl (long __val) __ATTR_CONST__;
extern int ffsll (long long __val) __ATTR_CONST__;
extern void *memccpy(void *, const void *, int, size_t);
extern void *memchr(const void *, int, size_t) __ATTR_PURE__;
extern int memcmp(const void *, const void *, size_t) __ATTR_PURE__;
extern void *memcpy(void *, const void *, size_t);
extern void *memmem(const void *, size_t, const void *, size_t) __ATTR_PURE__;
extern void *memmove(void *, const void *, size_t);
extern void *memrchr(const void *, int, size_t) __ATTR_PURE__;
extern void *memset(void *, int, size_t);
extern char *strcat(char *, const char *);
extern char *strchr(const char *, int) __ATTR_PURE__;
extern char *strchrnul(const char *, int) __ATTR_PURE__;
extern int strcmp(const char *, const char *) __ATTR_PURE__;
extern char *strcpy(char *, const char *);
extern int strcasecmp(const char *, const char *) __ATTR_PURE__;
extern char *strcasestr(const char *, const char *) __ATTR_PURE__;
extern size_t strcspn(const char *__s, const char *__reject) __ATTR_PURE__;
extern char *strdup(const char *s1);
extern size_t strlcat(char *, const char *, size_t);
extern size_t strlcpy(char *, const char *, size_t);
extern size_t strlen(const char *) __ATTR_PURE__;
extern char *strlwr(char *);
extern char *strncat(char *, const char *, size_t);
extern int strncmp(const char *, const char *, size_t) __ATTR_PURE__;
extern char *strncpy(char *, const char *, size_t);
extern int strncasecmp(const char *, const char *, size_t) __ATTR_PURE__;
extern size_t strnlen(const char *, size_t) __ATTR_PURE__;
extern char *strpbrk(const char *__s, const char *__accept) __ATTR_PURE__;
extern char *strrchr(const char *, int) __ATTR_PURE__;
extern char *strrev(char *);
extern char *strsep(char **, const char *);
extern size_t strspn(const char *__s, const char *__accept) __ATTR_PURE__;
extern char *strstr(const char *, const char *) __ATTR_PURE__;
extern char *strtok(char *, const char *);
extern char *strtok_r(char *, const char *, char **);
extern char *strupr(char *);

Upvotes: 2

Views: 3978

Answers (3)

carlosdc
carlosdc

Reputation: 12152

As Michael suggests, you should perhaps look at strnstr.

Snippet from the documentation:

 #include <string.h>

 char *
 strnstr(const char *s1, const char *s2, size_t n);

The strnstr() function locates the first occurrence of the null-terminated string s2 in the string s1, where not more than n characters are searched.

so it seems like what you want is

char* found = strnstr(output,substring,100);

In case your C-lib is limited, the code is fairly straight forward:

char* strnstr(char* s, char* find, size_t slen)
{
    char c, sc;
    size_t len;

    if ((c = *find++) != '\0') {
        len = strlen(find);
        do {
            do {
                if ((sc = *s++) == '\0' || slen-- < 1)
                    return (NULL);
            } while (sc != c);
            if (len > slen)
                return (NULL);
        } while (strncmp(s, find, len) != 0);
        s--;
    }
    return ((char *)s);
}

Upvotes: 0

SpacedMonkey
SpacedMonkey

Reputation: 2773

From the link you posted I would go with memmem().

void *memmem(const void *s1, 
             size_t len1, 
             const void *s2, 
             size_t len2);

The memmem() function finds the start of the first occurrence of the substring s2 of length len2 in the memory area s1 of length len1.

Upvotes: 5

Keith Nicholas
Keith Nicholas

Reputation: 44308

given your microcontroller lib doesn't have strstr or strnstr....

char* string_first_of(char* s, char* sub)
{
    int n;
    if(s == NULL) return NULL;
    if(sub == NULL) return s;
    n =  strlen(sub);
    while(*s != 0)
    {
        if(strncmp(s, sub, n) == 0)
        {
            return s;
        }
        s++;
    }
    return NULL;    
}

bool string_contains(char* s, char* sub)
{
    return string_first_of(s, sub) != NULL;
}

Upvotes: 0

Related Questions