uch.c 651 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <stdint.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include "uch.h"
  5. Ch_len
  6. ch_len_f(const char *str)
  7. {
  8. unsigned char c = *str;
  9. Ch_len len = 0;
  10. for (; c >> 7; ++len, c <<= 1);
  11. if (!len)
  12. return 1;
  13. return len;
  14. }
  15. Ch_len
  16. ch_len_b(const char *str)
  17. {
  18. Ch_len len = 1;
  19. --str;
  20. /* ascii, last bit not set */
  21. if (!(*str & (1 << 7)))
  22. return 1;
  23. /* not at start if second to last bit is not set */
  24. for (; !(*str & (1 << 6)); ++len, --str);
  25. return len;
  26. }
  27. Ch_len
  28. ch_len(Uch ch)
  29. {
  30. return ch_len_f((char *) &ch);
  31. }
  32. Uch
  33. uch(const char *str, Ch_len *len)
  34. {
  35. Uch ch = 0;
  36. *len = ch_len_f(str);
  37. memcpy(&ch, str, *len);
  38. return ch;
  39. }