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