Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #123037

Re: A nice "strlen()" for UTF-8 ...

From Lynn McGuire <lynnmcguire5@gmail.com>
Newsgroups comp.lang.c++
Subject Re: A nice "strlen()" for UTF-8 ...
Date 2025-11-21 00:15 -0600
Organization A noiseless patient Spider
Message-ID <10fp024$3fe4a$1@dont-email.me> (permalink)
References <10fkf0b$28ebt$1@raubtier-asyl.eternal-september.org>

Show all headers | View raw


On 11/19/2025 7:00 AM, Bonita Montero wrote:
> #include <iostream>
> #include <string_view>
> #include <bit>
> 
> using namespace std;
> 
> template<bool Validate = false, typename View>
>      requires std::same_as<View, string_view> || std::same_as<View, 
> u8string_view>
> size_t utf8Width( View str )
> {
>      size_t rem = str.end() - str.begin(), w = 0, chunk;
>      for( auto it = str.begin(); rem; rem -= chunk, ++w ) [[likely]]
>      {
>          chunk = countl_one( (unsigned char)*it );
>          chunk += (size_t)!chunk;
>          if constexpr( Validate )
>              if( (*it & 0xC0u) == 0x80u || chunk > 5 || rem < chunk ) 
> [[unlikely]]
>                  return -1;
>          auto end = it + chunk;
>          if constexpr( !Validate )
>              it = end;
>          else
>              while( ++it != end )
>                  if( (*it & 0xC0u) != 0x80u )
>                      return -1;
>      }
>      return w;
> }
> 
> int main()
> {
>      char8_t strU8[] = u8"Hello, 世界!";
>      string_view sv( (char *)strU8 );
>      cout << utf8Width<false>( sv ) << endl;
>      cout << utf8Width<true>( sv ) << endl;
>      u8string_view svU8( strU8 );
>      cout << utf8Width<false>( svU8 ) << endl;
>      cout << utf8Width<true>( svU8 ) << endl;
> }

So, why does strlen care if the text is ASCII or UTF8 ?  Strlen is all 
about the number of bytes used by the text string.

Now if you need the actual number of characters...

Lynn

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

A nice "strlen()" for UTF-8 ... Bonita Montero <Bonita.Montero@gmail.com> - 2025-11-19 14:00 +0100
  Re: A nice "strlen()" for UTF-8 ... Lynn McGuire <lynnmcguire5@gmail.com> - 2025-11-19 12:59 -0600
    Re: A nice "strlen()" for UTF-8 ... Bonita Montero <Bonita.Montero@gmail.com> - 2025-11-20 15:25 +0100
  Re: A nice "strlen()" for UTF-8 ... Lynn McGuire <lynnmcguire5@gmail.com> - 2025-11-21 00:15 -0600
    Re: A nice "strlen()" for UTF-8 ... Bonita Montero <Bonita.Montero@gmail.com> - 2025-11-21 10:42 +0100
      Re: A nice "strlen()" for UTF-8 ... Alfred Peters <miteinere-mail-adresseindereinleitungszeilewirddieseoftzulang@geekmail.de> - 2025-11-23 13:22 +0100
        Re: A nice "strlen()" for UTF-8 ... Bonita Montero <Bonita.Montero@gmail.com> - 2025-11-23 17:35 +0100
          Re: A nice "strlen()" for UTF-8 ... David Brown <david.brown@hesbynett.no> - 2025-11-23 18:18 +0100
            Re: A nice "strlen()" for UTF-8 ... Bonita Montero <Bonita.Montero@gmail.com> - 2025-11-24 13:08 +0100
              Re: A nice "strlen()" for UTF-8 ... David Brown <david.brown@hesbynett.no> - 2025-11-24 14:09 +0100
                Re: A nice "strlen()" for UTF-8 ... Bonita Montero <Bonita.Montero@gmail.com> - 2025-11-24 14:31 +0100
                Re: A nice "strlen()" for UTF-8 ... David Brown <david.brown@hesbynett.no> - 2025-11-24 15:45 +0100
                Re: A nice "strlen()" for UTF-8 ... Bonita Montero <Bonita.Montero@gmail.com> - 2025-11-24 16:32 +0100
                Re: A nice "strlen()" for UTF-8 ... Michael S <already5chosen@yahoo.com> - 2025-11-24 19:16 +0200
                Re: A nice "strlen()" for UTF-8 ... David Brown <david.brown@hesbynett.no> - 2025-11-24 18:32 +0100
                Re: A nice "strlen()" for UTF-8 ... Michael S <already5chosen@yahoo.com> - 2025-11-24 19:51 +0200
                Re: A nice "strlen()" for UTF-8 ... Michael S <already5chosen@yahoo.com> - 2025-11-24 19:54 +0200
                Re: A nice "strlen()" for UTF-8 ... David Brown <david.brown@hesbynett.no> - 2025-11-24 21:14 +0100
            Re: A nice "strlen()" for UTF-8 ... Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2025-11-24 12:53 +0000

csiph-web