[prev in list] [next in list] [prev in thread] [next in thread] 

List:       icu
Subject:    Re: number of chars in a UTF-8 string
From:       Markus Scherer <markus.scherer () jtcsv ! com>
Date:       2001-09-24 22:48:05
[Download RAW message or body]

Changjian_Sun@i2.com wrote:
> how to find out how many chars in a UTF-8 string ? Thanks

I suggest to move forward using the macro UTF8_FWD_1_SAFE() and to count how many \
times you need to call this:


#include "unicode/utypes.h"

int32_t
countCodePointsInUTF8(const uint8_t *s, int32_t length) {
    int32_t i, count;

    i=count=0;
    while(i<length) {
        UTF8_FWD_1_SAFE(s, i, length);
        ++count;
    }
    return count;
}


If you _know_ that your UTF-8 string is really correct UTF-8 (no illegal byte \
sequences), then you can use the _UNSAFE version of the macro, which is faster.

markus


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic