I have a simple question:
Is there a way to do a strlen()
-like count of characters in zero-terminated char16_t
array?
C++11 char16_t strlen-equivalent function
use
char_traits<char16_t>::length(your_pointer)
see 21.2.3.2 struct char_traits<char16_t>
and table 62 of the C++11-Std.
Use pointers :), create a duplicate pointer to the start, then loop through it while (*endptr++);
, then the length is given by endptr - startptr
. You can actually template this, however, its possible the the compile won't generate the same intrinsic code it does for strlen
(for different sizes ofc).
ouch, that looks ugly, although it is very very short... BTW I'm not insulting your code, its just strange that there is no overload for strlen or wstrlen... –
Matterhorn
@NoSense: if you've ever looked at the CRT source for MS or GCC, you'll see they use this method for strlen et al. Ofc its not as fast as the assembly level stuff like SSE 4.1 string instructions or good old
REP SCAS
, but thats what intrinsic are for ;) there are articles on sse based versions(the gcc even has a few variants it uses), but this restricts your hardware –
Hydrobomb Ok, cool, as a matter of fact i'm using GCC. Does gcc have some wrapper function around asm stuff. I mean something like __wstrlen(const char16_t * str) –
Matterhorn
@NoSense: last i looked gcc prefered to hardcode overloads to the symbol name to get the code generator to output 'special' assembly, it doesn't actually have and assembly functions (this excludes libraries, and afaik glibc has no assembly either) –
Hydrobomb
ok, I always thought that nice way to make SSE instructions popular was to have wrapper functions that on old HW revert to slow implementation but still compile to legit code... Can you hear me Intel, can you hear me AMD? –
Matterhorn
Hey guys,
char_traits<>
have been since forever. Use them! –
Advisable @johannes: no, but neither would the
char_traits
under msvc 2010 when i poked into it thanks to towi :) –
Hydrobomb @Johannes: Seems like the question didn't ask about counting glyphs. –
Colloid
Necrolis answer includes the NULL byte in the length, which probably is not what you want. strlen() does not include the NULL byte in the length.
Adapting their answer:
static size_t char16len(uint16_t *startptr)
{
uint16_t *endptr = startptr;
while (*endptr) {
endptr++;
}
return endptr - startptr;
}
I realize there is an accepted C++ answer, but this answer is useful for anyone who stumbles on this post using C (like I did).
© 2022 - 2024 — McMap. All rights reserved.