util.c 766 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "sam.h"
  2. void
  3. cvttorunes(char *p, int n, Rune *r, int *nb, int *nr, int *nulls)
  4. {
  5. uchar *q;
  6. Rune *s;
  7. int j, w;
  8. /*
  9. * Always guaranteed that n bytes may be interpreted
  10. * without worrying about partial runes. This may mean
  11. * reading up to UTFmax-1 more bytes than n; the caller
  12. * knows this. If n is a firm limit, the caller should
  13. * set p[n] = 0.
  14. */
  15. q = (uchar*)p;
  16. s = r;
  17. for(j=0; j<n; j+=w){
  18. if(*q < Runeself){
  19. w = 1;
  20. *s = *q++;
  21. }else{
  22. w = chartorune(s, (char*)q);
  23. q += w;
  24. }
  25. if(*s)
  26. s++;
  27. else if(nulls)
  28. *nulls = TRUE;
  29. }
  30. *nb = (char*)q-p;
  31. *nr = s-r;
  32. }
  33. void*
  34. fbufalloc(void)
  35. {
  36. return emalloc(BUFSIZE);
  37. }
  38. void
  39. fbuffree(void *f)
  40. {
  41. free(f);
  42. }
  43. uint
  44. min(uint a, uint b)
  45. {
  46. if(a < b)
  47. return a;
  48. return b;
  49. }