std::strcspn(3)
β¨ NAME β¨
std::strcspn β okay bestie, say it with me: std::strcspn. This is the C++ Standard Library glow-up of the classic C strcspn, and it's genuinely one of my faves even though nobody talks about it. π
π SYNOPSIS π
It lives in <cstring>, like all the other string-core girlies:
Defined in header <cstring>
std::size_t strcspn( const char *dest, const char *src );
So here's the vibe: you hand it dest (the byte string you're scanning) and src (a byte string of characters you're avoiding), and it gives you the length of the longest initial chunk of dest that contains only characters that are not in src. The second it hits a character that's in src, the count stops right there. πΈ
And the name?? The name stands for "complementary span" β like, it's the span of the complement of src. Not me being obsessed with a name origin for once. π
π PARAMETERS π
destβ pointer to the null-terminated byte string that's getting analyzed. This is the main character of the operation. πsrcβ pointer to the null-terminated byte string holding the characters you're searching for (i.e. the ones that'll stop the count dead in its tracks). π
π¦ RETURN VALUE π¦
The length of the maximum initial segment of dest that contains only characters not found in the byte string pointed to by src. That's it, that's the tea. π
π EXAMPLE π
Run this code, bestie, I promise it's cute:
// Run this code
#include <cstddef>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
int main()
{
std::string s = "abcde312$#@";
const char* invalid = "*$#";
const std::size_t valid_len = std::strcspn(s.c_str(), invalid);
if (valid_len != s.size())
{
std::cout << std::quoted(s)
<< " contains invalid chars starting at position "
<< valid_len << '\n'
<< std::string(valid_len + 1, '-') << "^\n";
}
}
β Output β
"abcde312$#@" contains invalid chars starting at position 8
---------^
See how it counted abcde312 (that's 8 characters, periodt) and then peaced out the moment it saw $? The little ^ arrow is pointing at the crime scene. ππ
π SEE ALSO π
strspnβ returns the length of the maximum initial segment that consists of only the characters found in another byte string. (function) β the mirror-image sister of this one, honestly iconic.wcscspnβ returns the length of the maximum initial segment that consists of only the wide characters not found in another wide string. (function) β same energy, wide-char edition.strpbrkβ finds the first location of any character from a set of separators. (function) β when you don't want the length, you want the address. π₯find_first_ofβ find first occurrence of characters (public member function ofstd::basic_string<CharT,Traits,Allocator>). β the C++ string-class way of doing the same quest. β¨
Also!! There's a C documentation for strcspn if you ever want to visit the ancestors. π«Ά
π that's the whole page, bestie π β sourced from cppreference.com, dated 2024.06.10, and this is the std::strcspn(3) manual page signing off. go count some spans, you've got this. π§ββοΈβ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms