utfnlen.c 317 B

1234567891011121314151617181920212223242526
  1. #include <u.h>
  2. #include <libc.h>
  3. int
  4. utfnlen(char *s, long m)
  5. {
  6. int c;
  7. long n;
  8. Rune rune;
  9. char *es;
  10. es = s + m;
  11. for(n = 0; s < es; n++) {
  12. c = *(uchar*)s;
  13. if(c < Runeself){
  14. if(c == '\0')
  15. break;
  16. s++;
  17. continue;
  18. }
  19. if(!fullrune(s, es-s))
  20. break;
  21. s += chartorune(&rune, s);
  22. }
  23. return n;
  24. }