是否有一个标准的C函数来计算字符串长度到某个极限?

时间:2022-04-09 19:41:38

Consider a situation: I have a buffer of known length that presumably stores a null-terminated string and I need to know the length of the string.

考虑一个情况:我有一个已知长度的缓冲区,它可能存储一个以null结尾的字符串,我需要知道字符串的长度。

The problem is if I use strlen() and the string turns out to be not null-terminated the program runs into undefined behavior while reading beyond the buffer end. So I'd like to have a function like the following:

问题是,如果我使用strlen(),而字符串并不是空终止的,那么程序在读取缓冲区结束后会运行到未定义的行为。所以我想要一个函数,如下所示:

 size_t strlenUpTo( char* buffer, size_t limit )
 {
     size_t count = 0;
     while( count < limit && *buffer != 0 ) {
        count++;
        buffer++;
     }
     return count;
 }

so that it returns the length of the string but never tries to read beyond the buffer end.

因此,它返回字符串的长度,但从不尝试读取缓冲区结束之外的内容。

Is there such function already in C library or do I have to use my own?

C库中是否已经有这样的功能,还是需要我自己使用?

3 个解决方案

#1


10  

Use memchr(string, 0, limit), as in:

使用memchr(string, 0, limit),如:

size_t strlenUpTo(char *s, size_t n)
{
    char *z = memchr(s, 0, n);
    return z ? z-s : n;
}

#2


10  

According to my documentation, POSIX has size_t strnlen(const char *src, size_t maxlen);

根据我的文档,POSIX有size_t strnlen(const char *src, size_t maxlen);

n = strnlen("foobar", 7); // n = 6;
n = strnlen("foobar", 6); // n = 6;
n = strnlen("foobar", 5); // n = 5;

#3


3  

you can use strnlen. This is it's manpage:

您可以使用strnlen。这是它从:

NAME
       strnlen - determine the length of a fixed-size string

SYNOPSIS
       #include <string.h>

       size_t strnlen(const char *s, size_t maxlen);

DESCRIPTION
       The  strnlen  function returns the number of characters in
       the string pointed to by s, not including the  terminating
       '\0' character, but at most maxlen. In doing this, strnlen
       looks only at the first maxlen characters at s  and  never
       beyond s+maxlen.

RETURN VALUE
       The  strnlen  function  returns strlen(s), if that is less
       than maxlen, or maxlen if there is no '\0' character among
       the first maxlen characters pointed to by s.

CONFORMING TO
       This function is a GNU extension.

SEE ALSO
       strlen(3)

#1


10  

Use memchr(string, 0, limit), as in:

使用memchr(string, 0, limit),如:

size_t strlenUpTo(char *s, size_t n)
{
    char *z = memchr(s, 0, n);
    return z ? z-s : n;
}

#2


10  

According to my documentation, POSIX has size_t strnlen(const char *src, size_t maxlen);

根据我的文档,POSIX有size_t strnlen(const char *src, size_t maxlen);

n = strnlen("foobar", 7); // n = 6;
n = strnlen("foobar", 6); // n = 6;
n = strnlen("foobar", 5); // n = 5;

#3


3  

you can use strnlen. This is it's manpage:

您可以使用strnlen。这是它从:

NAME
       strnlen - determine the length of a fixed-size string

SYNOPSIS
       #include <string.h>

       size_t strnlen(const char *s, size_t maxlen);

DESCRIPTION
       The  strnlen  function returns the number of characters in
       the string pointed to by s, not including the  terminating
       '\0' character, but at most maxlen. In doing this, strnlen
       looks only at the first maxlen characters at s  and  never
       beyond s+maxlen.

RETURN VALUE
       The  strnlen  function  returns strlen(s), if that is less
       than maxlen, or maxlen if there is no '\0' character among
       the first maxlen characters pointed to by s.

CONFORMING TO
       This function is a GNU extension.

SEE ALSO
       strlen(3)