localelen.c (581B)
1 /* Copyright ©2010 Kris Maglione <maglione.k at Gmail> 2 * Copyright ©2002 by Lucent Technologies. 3 * See LICENSE file for license details. 4 */ 5 #include "fmtdef.h" 6 7 int 8 localelen(char *str, char *end) { 9 mbstate_t state; 10 size_t n, res; 11 12 if(utf8locale()) { 13 if(end) 14 return utfnlen(str, end - str); 15 return utflen(str); 16 } 17 18 state = (mbstate_t){0}; 19 n = 0; 20 for(n=0;;) 21 switch((res = mbrtowc(nil, str, end ? end - str : MB_LEN_MAX, &state))) { 22 case -1: 23 return -1; 24 case 0: 25 case -2: 26 return n; 27 default: 28 n++; 29 str += res; 30 } 31 return n; /* Not reached. */ 32 } 33