util.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "sam.h"
  10. void
  11. cvttorunes(char *p, int n, Rune *r, int *nb, int *nr, int *nulls)
  12. {
  13. uint8_t *q;
  14. Rune *s;
  15. int j, w;
  16. /*
  17. * Always guaranteed that n bytes may be interpreted
  18. * without worrying about partial runes. This may mean
  19. * reading up to UTFmax-1 more bytes than n; the caller
  20. * knows this. If n is a firm limit, the caller should
  21. * set p[n] = 0.
  22. */
  23. q = (uint8_t*)p;
  24. s = r;
  25. for(j=0; j<n; j+=w){
  26. if(*q < Runeself){
  27. w = 1;
  28. *s = *q++;
  29. }else{
  30. w = chartorune(s, (char*)q);
  31. q += w;
  32. }
  33. if(*s)
  34. s++;
  35. else if(nulls)
  36. *nulls = TRUE;
  37. }
  38. *nb = (char*)q-p;
  39. *nr = s-r;
  40. }
  41. void*
  42. fbufalloc(void)
  43. {
  44. return emalloc(BUFSIZE);
  45. }
  46. void
  47. fbuffree(void *f)
  48. {
  49. free(f);
  50. }
  51. uint
  52. min(uint a, uint b)
  53. {
  54. if(a < b)
  55. return a;
  56. return b;
  57. }