xref: /original-bsd/lib/libc/locale/mbrune.c (revision 4092c5cc)
1 /*-
2  * Copyright (c) 1993 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Paul Borman at Krystal Technologies.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)mbrune.c	5.1 (Berkeley) 05/26/93";
13 #endif /* LIBC_SCCS and not lint */
14 
15 #include <limits.h>
16 #include <rune.h>
17 #include <stddef.h>
18 #include <string.h>
19 
20 char *
21 mbrune(string, c)
22 	const char *string;
23 	rune_t c;
24 {
25 	char const *result;
26 	rune_t r;
27 
28 	while ((r = sgetrune(string, MB_LEN_MAX, &result))) {
29 		if (r == c)
30 			return ((char *)string);
31 		string = result == string ? string + 1 : result;
32 	}
33 
34 	return (c == *string ? (char *)string : NULL);
35 }
36 
37 char *
38 mbrrune(string, c)
39 	const char *string;
40 	rune_t c;
41 {
42 	const char *last = 0;
43 	char const *result;
44 	rune_t  r;
45 
46 	while ((r = sgetrune(string, MB_LEN_MAX, &result))) {
47 		if (r == c)
48 			last = string;
49 		string = result == string ? string + 1 : result;
50 	}
51 	return (c == *string ? (char *)string : (char *)last);
52 }
53 
54 char *
55 mbmb(string, pattern)
56 	const char *string;
57 	char *pattern;
58 {
59 	rune_t first, r;
60 	size_t plen, slen;
61 	char const *result;
62 
63 	plen = strlen(pattern);
64 	slen = strlen(string);
65 	if (plen > slen)
66 		return (0);
67 
68 	first = sgetrune(pattern, plen, &result);
69 	if (result == string)
70 		return (0);
71 
72 	while (slen >= plen && (r = sgetrune(string, slen, &result))) {
73 		if (r == first) {
74 			if (strncmp(string, pattern, slen) == 0)
75 				return ((char *) string);
76 		}
77 		if (result == string) {
78 			--slen;
79 			++string;
80 		} else {
81 			slen -= result - string;
82 			string = result;
83 		}
84 	}
85 	return (0);
86 }
87