什么是strspn?

时间:2022-02-01 20:43:19

At work today we were trying to come up with any reason you would use strspn.

在今天的工作中,我们试图想出你将使用strspn的任何理由。

I searched google code to see if it's ever been implemented in a useful way and came up blank. I just can't imagine a situation in which I would really need to know the length of the first segment of a string that contains only characters from another string. Any ideas?

我搜索了谷歌代码,看看它是否曾以有用的方式实现,并且显得空白。我无法想象一种情况,我真的需要知道只包含另一个字符串中字符的字符串的第一段的长度。有任何想法吗?

5 个解决方案

#1


4  

Although you link to the PHP manual, the strspn() function comes from C libraries, along with strlen(), strcpy(), strcmp(), etc.

虽然您链接到PHP手册,但strspn()函数来自C库,以及strlen(),strcpy(),strcmp()等。

strspn() is a convenient alternative to picking through a string character by character, testing if the characters match one of a set of values. It's useful when writing tokenizers. The alternative to strspn() would be lots of repetitive and error-prone code like the following:

strspn()是逐个字符串挑选的一种方便的替代方法,测试字符是否匹配一组值中的一个。在编写tokenizer时很有用。 strspn()的替代方案将是许多重复且容易出错的代码,如下所示:

for (p = stringbuf; *p; p++) {
  if (*p == 'a' || *p == 'b' || *p = 'c' ... || *p == 'z') {
    /* still parsing current token */
  }
}

Can you spot the error? :-)

你能发现错误吗? :-)

Of course in a language with builtin support for regular expression matching, strspn() makes little sense. But when writing a rudimentary parser for a DSL in C, it's pretty nifty.

当然,在内置支持正则表达式匹配的语言中,strspn()没有多大意义。但是当用C语言为DSL编写一个基本的解析器时,它非常漂亮。

#2


1  

It's based on the the ANSI C function strspn(). It can be useful in low-level C parsing code, where there is no high-level string class. It's considerably less useful in PHP, which has lots of useful string parsing functions.

它基于ANSI C函数strspn()。它在低级C语法分析代码中很有用,其中没有高级字符串类。它在PHP中没那么有用,它有很多有用的字符串解析函数。

#3


1  

I think its great for blacklisting and letting the user know from where the error started. Like MySQL returns part of the query from where the error occured.

我认为它非常适合黑名单并让用户知道错误从何处开始。像MySQL一样,从错误发生的地方返回部分查询。

Please see this function, that lets the user know which part of his comment is not valid:

请参阅此功能,让用户知道他的评论的哪一部分无效:

function blacklistChars($yourComment){

$blacklistedChars = "!@#$%^&*()";
$validLength = strcspn($yourComment, $blacklistedChars);
if ($validLength !== strlen($yourComment))
{

    $error = "Your comment contains invalid chars starting from here: `" . 
        substr($yourComment, (int) '-' . $validLength) . "`";

    return $error;
}

return false;
}

$yourComment = "Hello, why can you not type and $ dollar sign in the text?"; 
$yourCommentError = blacklistChars($yourComment);

if ($yourCommentError <> false)
echo $yourCommentError;

#4


0  

Well, by my understanding, its the same thing as this regex:

嗯,根据我的理解,它与这个正则表达式相同:

^[set]*

Where set is the string containing the characters to be found.

其中set是包含要查找的字符的字符串。

You could use it to search for any number or text at the beginning of a string and split.

您可以使用它在字符串的开头搜索任何数字或文本并进行拆分。

It seems it would be useful when porting code to php.

在将代码移植到php时,它似乎很有用。

#5


0  

It is useful specificaly for functions like atoi - where you have a string you want to convert to a number, and you don't want to deal with anything that isn't in the set "-.0123456789"

它特别适用于像atoi这样的函数 - 你有一个你想要转换为数字的字符串,而你不想处理那些不在集合中的任何东西“-.0123456789”

But yes, it has limited use.

但是,它的用途有限。

-Adam

#1


4  

Although you link to the PHP manual, the strspn() function comes from C libraries, along with strlen(), strcpy(), strcmp(), etc.

虽然您链接到PHP手册,但strspn()函数来自C库,以及strlen(),strcpy(),strcmp()等。

strspn() is a convenient alternative to picking through a string character by character, testing if the characters match one of a set of values. It's useful when writing tokenizers. The alternative to strspn() would be lots of repetitive and error-prone code like the following:

strspn()是逐个字符串挑选的一种方便的替代方法,测试字符是否匹配一组值中的一个。在编写tokenizer时很有用。 strspn()的替代方案将是许多重复且容易出错的代码,如下所示:

for (p = stringbuf; *p; p++) {
  if (*p == 'a' || *p == 'b' || *p = 'c' ... || *p == 'z') {
    /* still parsing current token */
  }
}

Can you spot the error? :-)

你能发现错误吗? :-)

Of course in a language with builtin support for regular expression matching, strspn() makes little sense. But when writing a rudimentary parser for a DSL in C, it's pretty nifty.

当然,在内置支持正则表达式匹配的语言中,strspn()没有多大意义。但是当用C语言为DSL编写一个基本的解析器时,它非常漂亮。

#2


1  

It's based on the the ANSI C function strspn(). It can be useful in low-level C parsing code, where there is no high-level string class. It's considerably less useful in PHP, which has lots of useful string parsing functions.

它基于ANSI C函数strspn()。它在低级C语法分析代码中很有用,其中没有高级字符串类。它在PHP中没那么有用,它有很多有用的字符串解析函数。

#3


1  

I think its great for blacklisting and letting the user know from where the error started. Like MySQL returns part of the query from where the error occured.

我认为它非常适合黑名单并让用户知道错误从何处开始。像MySQL一样,从错误发生的地方返回部分查询。

Please see this function, that lets the user know which part of his comment is not valid:

请参阅此功能,让用户知道他的评论的哪一部分无效:

function blacklistChars($yourComment){

$blacklistedChars = "!@#$%^&*()";
$validLength = strcspn($yourComment, $blacklistedChars);
if ($validLength !== strlen($yourComment))
{

    $error = "Your comment contains invalid chars starting from here: `" . 
        substr($yourComment, (int) '-' . $validLength) . "`";

    return $error;
}

return false;
}

$yourComment = "Hello, why can you not type and $ dollar sign in the text?"; 
$yourCommentError = blacklistChars($yourComment);

if ($yourCommentError <> false)
echo $yourCommentError;

#4


0  

Well, by my understanding, its the same thing as this regex:

嗯,根据我的理解,它与这个正则表达式相同:

^[set]*

Where set is the string containing the characters to be found.

其中set是包含要查找的字符的字符串。

You could use it to search for any number or text at the beginning of a string and split.

您可以使用它在字符串的开头搜索任何数字或文本并进行拆分。

It seems it would be useful when porting code to php.

在将代码移植到php时,它似乎很有用。

#5


0  

It is useful specificaly for functions like atoi - where you have a string you want to convert to a number, and you don't want to deal with anything that isn't in the set "-.0123456789"

它特别适用于像atoi这样的函数 - 你有一个你想要转换为数字的字符串,而你不想处理那些不在集合中的任何东西“-.0123456789”

But yes, it has limited use.

但是,它的用途有限。

-Adam