Name n3665, alx-0028r4 - add st*rspn(), st*rcspn(), wc*rspn(), wc*rcspn() Principles - Codify existing practice to address evident deficiencies. - Enable secure programming Category Standardize common string APIs Author Alejandro Colomar History r0 (2025-06-14): - Initial draft. r1 (2025-06-26): - tfix. - s/Description/Rationale/ r2 (2025-06-27; n3620): - wfix. r3 (2025-07-03): - tfix. r4 (2025-07-27; n3665): - Add note about DoS. Rationale Searching for a span starting at the end of a string is a common operation. It allows easily removing trailing white space in a line, for example: stpcpy(stprspn(buf, " \t\n"), ""); A variant that searches for the complementary span, stprcspn() is interesting for example for finding the basename of a path: stprcspn(s, "/"); DoS concerns While one may be concerned that these APIs may promote DoS attacks by searching in a loop within a loop, these APIs are designed to be used with (short) string literals as the second argument. It would only be a concern if the second argument can be controlled by a user, but that would be unusual. Prior art Solaris provides strrspn(3GEN), although it returns a pointer. This is inconsistent with the existing strspn(3) family of functions, which returns an offset. Design decisions For returning a pointer, a better name for the Solaris API would have been stprspn(). Let's add it here as stprspn(), and turn Solaris into non-conforming in this regard. Proposed wording Based on N3550. 7.28.5.1 Add 'stprcspn' and 'stprspn' in the first sentence. 7.28.5 String handling :: Search functions ## New sections after 7.28.5.4 ("The strcspn function") +7.28.5.4+1 The strrcspn function + +Synopsis +1 #include + size_t strrcspn(const char *s, const char *reject); + +Description +2 The strrcspn function + computes the offset of the maximum final segment + of the string pointed to by s + which consists entirely of + characters not from + the string pointed to by reject. + +Returns +3 The strrcspn function returns + the offset of the segment + from the beginning of the string pointed to by s. + +7.28.5.4+2 The stprcspn generic function + +Synopsis +1 #include + QChar *stprcspn(QChar *s, const char *reject); + +Description +2 The stprcspn generic function + is equivalent to strrcspn, + except for the return value. + +Returns +3 The stprcspn generic function returns + s + strrcspn(s, reject) ## New sections after 7.28.5.7 ("The strspn function") +7.28.5.7+1 The strrspn function + +Synopsis +1 #include + size_t strrspn(const char *s, const char *accept); + +Description +2 The strrspn function + computes the offset of the maximum final segment + of the string pointed to by s + which consists entirely of + characters from + the string pointed to by accept. + +Returns +3 The strrspn function returns + the offset of the segment + from the beginning of the string pointed to by s. + +7.28.5.4+2 The stprspn generic function + +Synopsis +1 #include + QChar *stprspn(QChar *s, const char *accept); + +Description +2 The stprspn generic function + is equivalent to strrspn, + except for the return value. + +Returns +3 The stprspn generic function returns + s + strrspn(s, accept) 7.28.5.1 Add 'wcprcspn' and 'wcprspn' in the first sentence. 7.33.4.6 General wide string utilities :: Wide string search functions ## New sections after 7.33.4.6.3 ("The wcscspn function") +7.33.4.6.3+1 The wcsrcspn function + +Synopsis +1 #include + size_t wcsrcspn(const wchar_t *s, const wchar_t *reject); + +Description +2 The wcsrcspn function + is equivalent to strrcspn + except that it handles wide strings. + +7.33.4.6.3+2 The wcprcspn generic function + +Synopsis +1 #include + QWchar_t *wcprcspn(QWchar_t *p, const wchar_t *reject); + +Description +2 The wcprcspn generic function + is equivalent to stprcspn + except that it handles wide strings. ## New sections after 7.33.4.6.6 ("The wcsspn function") +7.33.4.6.6+1 The wcsrspn function + +Synopsis +1 #include + size_t wcsrspn(const wchar_t *s, const wchar_t *accept); + +Description +2 The wcsrspn function + is equivalent to strrspn + except that it handles wide strings. + +7.33.4.6.6+2 The wcprspn generic function + +Synopsis +1 #include + QWchar_t *wcprspn(QWchar_t *p, const wchar_t *accept); + +Description +2 The wcprspn generic function + is equivalent to stprspn + except that it handles wide strings. 7.35.18 String handling Add 'stprcspn' and 'stprspn' in the second paragraph. 7.35.21 Extended multibyte and wide character utilities Add 'wcprcspn' and 'wcprspn' in the third paragraph.