9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

util.c (766B)


      1 #include "sam.h"
      2 
      3 void
      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*
     37 fbufalloc(void)
     38 {
     39 	return emalloc(BUFSIZE);
     40 }
     41 
     42 void
     43 fbuffree(void *f)
     44 {
     45 	free(f);
     46 }
     47 
     48 uint
     49 min(uint a, uint b)
     50 {
     51 	if(a < b)
     52 		return a;
     53 	return b;
     54 }