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


Groups > comp.lang.c > #164406

Re: UTF-8 strings in C

From Bonita Montero <Bonita.Montero@gmail.com>
Newsgroups comp.lang.c
Subject Re: UTF-8 strings in C
Date 2022-01-14 15:42 +0100
Organization A noiseless patient Spider
Message-ID <srs24f$4n1$1@dont-email.me> (permalink)
References (3 earlier) <srh1kj$epo$1@dont-email.me> <srh3b9$q0m$1@dont-email.me> <srhcti$tvd$1@dont-email.me> <87lezih5b5.fsf@tigger.extechop.net> <87h7a6h0lp.fsf@tigger.extechop.net>

Show all headers | View raw


Am 14.01.2022 um 15:07 schrieb Otto J. Makela:
> om@iki.fi (Otto J. Makela) wrote:
> 
>> Took me a while to realize the countl_zero stuff was a C++ library
>> call, with the accompanying syntax.
>>
>> However, this is comp.lang.c, so C solutions are appreciated.
> 
> How about this? It of course does not count the slightly odder
> compositions (like flags) correctly.
> 
> ----
> 
> int utf8Strlen( char const *str ) {
>    int length = 0;
>    int multibyte = 0;
>    unsigned char c;
> 
>    while(c = *str++) {
>      if(multibyte) {
>        multibyte--;
>        if((c & 0xC0) != 0x80 ) // UTF8 encoding error
> 	return -1;
>      } else {
>        length++;
>        if (c < 0x80) // Normal 7-bit ASCII
> 	multibyte = 0;
>        else if ((c & 0xE0) == 0xC0) // Single following byte
> 	multibyte = 1;
>        else if ((c & 0xF0) == 0xE0) // Two following bytes
> 	multibyte = 2;
>        else if ((c & 0xF8) == 0xF0) // Three following bytes
> 	multibyte = 3;
>        else // UTF8 encoding error
> 	return -1;
>      }
>    }
> 
>    if(multibyte) // Ran out of string in the middle of multibyte character
>      return -1;
>    return length;
> }
> 

A lot of unpredictible branches.
That's what I want to prevent with the table.

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


Thread

Re: UTF-8 strings in C Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-10 07:17 +0100
  Re: UTF-8 strings in C Mateusz Viste <mateusz@xyz.invalid> - 2022-01-10 09:32 +0100
    Re: UTF-8 strings in C Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-10 11:26 +0100
      Re: UTF-8 strings in C Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-10 11:55 +0100
        Re: UTF-8 strings in C Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-10 14:38 +0100
          Re: UTF-8 strings in C om@iki.fi (Otto J. Makela) - 2022-01-14 14:26 +0200
            Re: UTF-8 strings in C om@iki.fi (Otto J. Makela) - 2022-01-14 16:07 +0200
              Re: UTF-8 strings in C Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-14 15:42 +0100
              Re: UTF-8 strings in C Philipp Klaus Krause <pkk@spth.de> - 2022-01-15 09:08 +0100
                Re: UTF-8 strings in C om@iki.fi (Otto J. Makela) - 2022-01-22 11:44 +0200
                Re: UTF-8 strings in C Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-22 04:48 -0800
                Re: UTF-8 strings in C Richard Damon <Richard@Damon-Family.org> - 2022-01-22 08:27 -0500
                Re: UTF-8 strings in C James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-22 14:25 -0500
                Re: UTF-8 strings in C om@iki.fi (Otto J. Makela) - 2022-01-27 13:32 +0200
                Re: UTF-8 strings in C om@iki.fi (Otto J. Makela) - 2022-01-27 13:33 +0200
                Re: UTF-8 strings in C James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-27 11:08 -0500
                Re: UTF-8 strings in C Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-02-03 22:23 -0800
          Re: UTF-8 strings in C Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-14 04:41 -0800
            Re: UTF-8 strings in C Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-14 14:43 +0100
              Re: UTF-8 strings in C Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-14 05:45 -0800
                Re: UTF-8 strings in C Mateusz Viste <mateusz@xyz.invalid> - 2022-01-14 15:10 +0100
                Re: UTF-8 strings in C Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-14 15:43 +0100
                Re: UTF-8 strings in C Bart <bc@freeuk.com> - 2022-01-14 15:50 +0000
                Re: UTF-8 strings in C Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-14 17:50 +0100
                Re: UTF-8 strings in C Mateusz Viste <mateusz@xyz.invalid> - 2022-01-14 17:17 +0100
                Re: UTF-8 strings in C Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2022-01-14 08:34 -0800
                Re: UTF-8 strings in C Mateusz Viste <mateusz@xyz.invalid> - 2022-01-14 17:42 +0100
                Re: UTF-8 strings in C Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-14 17:51 +0100
                Re: UTF-8 strings in C Mateusz Viste <mateusz@xyz.invalid> - 2022-01-14 18:07 +0100
                Re: UTF-8 strings in C Bonita Montero <Bonita.Montero@gmail.com> - 2022-01-14 18:19 +0100
                Re: UTF-8 strings in C Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-02-07 09:02 -0800

csiph-web