xref: /plan9/sys/src/ape/lib/utf/utfecpy.c (revision a84536681645e23c630ce4ef2e5c3b284d4c590b)
1 /*
2  * The authors of this software are Rob Pike and Ken Thompson.
3  *              Copyright (c) 2002 by Lucent Technologies.
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose without fee is hereby granted, provided that this entire notice
6  * is included in all copies of any software which is or includes a copy
7  * or modification of this software and in all copies of the supporting
8  * documentation for such software.
9  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
10  * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
11  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
12  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
13  */
14 #include <stdarg.h>
15 #include <string.h>
16 #include "utf.h"
17 #include "utfdef.h"
18 
19 static void*
20 memccpy(void *a1, void *a2, int c, ulong n)
21 {
22 	uchar *s1, *s2;
23 
24 	s1 = a1;
25 	s2 = a2;
26 	c &= 0xFF;
27 	while(n > 0) {
28 		if((*s1++ = *s2++) == c)
29 			return s1;
30 		n--;
31 	}
32 	return 0;
33 }
34 char*
35 utfecpy(char *to, char *e, char *from)
36 {
37 	char *end;
38 
39 	if(to >= e)
40 		return to;
41 	end = memccpy(to, from, '\0', e - to);
42 	if(end == nil){
43 		end = e-1;
44 		while(end>to && (*--end&0xC0)==0x80)
45 			;
46 		*end = '\0';
47 	}else{
48 		end--;
49 	}
50 	return end;
51 }
52