这个函数可以兼容POSIX的两个版本吗?
Can this function be compliant with two versions of POSIX?
这是 strspn
的概要:
#include <string.h>
size_t strspn(const char *s1, const char *s2);
这是 POSIX 的描述和 return 值。1-2001:
The strspn() function shall compute the length (in bytes) of the maximum initial segment of the string pointed to by s1 which consists entirely of bytes from the string pointed to by s2.
The strspn() function shall return the length of s1; no return value is reserved to indicate an error.
这与 POSIX.1-2017 中的(几乎)相同:
The strspn() function shall compute the length (in bytes) of the maximum initial segment of the string pointed to by s1 which consists entirely of bytes from the string pointed to by s2.
The strspn() function shall return the computed length; no return value is reserved to indicate an error.
strspn
的实现是否有可能同时符合 POSIX.1-2001 和 POSIX.1-2017?怎么样?
是的,计算长度和 s1 的长度通常会不同,这意味着该函数必须违反其中一个描述。
但是第一个显然是措辞错误,因为后者肯定是这个意思,所以没关系 .
没有多少人可以板着脸断言 strspn()
是 strlen()
的笨重廉价仿制品。
这是 POSIX.1-2001.
中的错误
作为the POSIX description of strspn
says:
The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers to the ISO C standard.
而C标准(ISO 9899:1999, 7.21.5.6 strspn
函数)明确表示:
The strspn
function returns the length of the segment.
POSIX 的较新版本修正了措辞,使其与 C 标准表达的意思相同,这也是一直以来的意图。 (它显然在 2006 年被注意到并发生了变化;请参阅 https://www.opengroup.org/austin/docs/austin_330.txt. The current version of strspn
in POSIX 在 CHANGE 中将此(相当隐秘地)称为“SD5-XSH-ERN-182 已应用”历史部分。)
正如 POSIX 所说 "defers to the ISO C standard",我相信在发生冲突时,兼容的实现必须遵循 C 标准,例如在这种情况下。
这是 strspn
的概要:
#include <string.h>
size_t strspn(const char *s1, const char *s2);
这是 POSIX 的描述和 return 值。1-2001:
The strspn() function shall compute the length (in bytes) of the maximum initial segment of the string pointed to by s1 which consists entirely of bytes from the string pointed to by s2.
The strspn() function shall return the length of s1; no return value is reserved to indicate an error.
这与 POSIX.1-2017 中的(几乎)相同:
The strspn() function shall compute the length (in bytes) of the maximum initial segment of the string pointed to by s1 which consists entirely of bytes from the string pointed to by s2.
The strspn() function shall return the computed length; no return value is reserved to indicate an error.
strspn
的实现是否有可能同时符合 POSIX.1-2001 和 POSIX.1-2017?怎么样?
是的,计算长度和 s1 的长度通常会不同,这意味着该函数必须违反其中一个描述。
但是第一个显然是措辞错误,因为后者肯定是这个意思,所以没关系 .
没有多少人可以板着脸断言 strspn()
是 strlen()
的笨重廉价仿制品。
这是 POSIX.1-2001.
中的错误作为the POSIX description of strspn
says:
The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers to the ISO C standard.
而C标准(ISO 9899:1999, 7.21.5.6 strspn
函数)明确表示:
The
strspn
function returns the length of the segment.
POSIX 的较新版本修正了措辞,使其与 C 标准表达的意思相同,这也是一直以来的意图。 (它显然在 2006 年被注意到并发生了变化;请参阅 https://www.opengroup.org/austin/docs/austin_330.txt. The current version of strspn
in POSIX 在 CHANGE 中将此(相当隐秘地)称为“SD5-XSH-ERN-182 已应用”历史部分。)
正如 POSIX 所说 "defers to the ISO C standard",我相信在发生冲突时,兼容的实现必须遵循 C 标准,例如在这种情况下。