utfnlen.c 302 B

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