Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有一个标准的C函数来计算字符串长度到某个极限?_C_String - Fatal编程技术网

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

是否有一个标准的C函数来计算字符串长度到某个极限?,c,string,C,String,考虑一种情况:我有一个已知长度的缓冲区,它可能存储一个以null结尾的字符串,我需要知道字符串的长度 问题是,如果我使用strlen(),结果字符串不是以null结尾的,那么程序在读取缓冲区之外的内容时会进入未定义的行为。所以我想要一个如下的函数: size_t strlenUpTo( char* buffer, size_t limit ) { size_t count = 0; while( count < limit && *buffer !=

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

问题是,如果我使用
strlen()
,结果字符串不是以null结尾的,那么程序在读取缓冲区之外的内容时会进入未定义的行为。所以我想要一个如下的函数:

 size_t strlenUpTo( char* buffer, size_t limit )
 {
     size_t count = 0;
     while( count < limit && *buffer != 0 ) {
        count++;
        buffer++;
     }
     return count;
 }
size\u t strlenupot(字符*缓冲区,大小限制)
{
大小\u t计数=0;
while(计数
因此,它返回字符串的长度,但从不尝试读取超出缓冲区末端的内容

C库中已经有这样的函数了,还是我必须使用自己的函数?

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

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

根据我的文档,POSIX具有
size\t strnlen(const char*src,size\t maxlen)


您可以使用
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)
名称
strnlen-确定固定大小字符串的长度
提要
#包括
大小\u t strnlen(常量字符*s,大小\u t maxlen);
描述
strnlen函数返回字符串中的字符数
s指向的字符串,不包括终止字符
“\0”字符,但最多为maxlen。这样做,斯特伦
仅查看s处的第一个maxlen字符,从不查看
超越s+maxlen。
返回值
strnlen函数返回strlen(s),如果小于
大于maxlen,或者如果其中没有“\0”字符,则为maxlen
s指向的第一个maxlen字符。
符合
此函数是GNU扩展。
另见
斯特伦(3)

注意符合本节要求的内容。但它已经过时了;POSIX 2008标准化了此功能。但当然它仍然不是简单的C。请注意,如果您的系统没有
strnlen
,您可以使用我的答案作为替换。命名替换
strnlen
并确保其具有与POSIX
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)