检查缓冲区中的子字符串 [英] Check for substring in a buffer

查看:53
本文介绍了检查缓冲区中的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用缓冲区从串行端口接收数据.缓冲区的固定长度为100,当我收到数据时,我将这些数据的长度存储在变量(索引)中.

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

我想检查从0到index-1的缓冲区是否包含一个子字符串.

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

我读到我可以使用strstr和strdup来做到这一点,但是我有两个问题.首先,我不知道如何从缓冲区中提取一个字符串,其次,如果我不必复制该字符串,而稍后再在其中寻找子字符串时删除一行,那会更好.

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.

我的变量如下:

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

我想要一个如果子字符串在output [0:index]中返回true的函数,否则返回false.

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!

我正在为atmel µC编写一段代码,因此似乎仅限于

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 *);

推荐答案

从您发布的链接中,我会选择 memmem().

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

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

memmem()函数在存储区中查找第一次出现的长度为 len2 的子字符串 s2 的开始长度为 len1 的s1 .

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.

这篇关于检查缓冲区中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆