xref: /freebsd/contrib/bmake/str.c (revision 226192822cddc30cacecd55bccb48f39c653058c)
1*22619282SSimon J. Gerraty /*	$NetBSD: str.c,v 1.105 2024/07/07 07:50:57 rillig Exp $	*/
23955d011SMarcel Moolenaar 
3dba7b0efSSimon J. Gerraty /*
43955d011SMarcel Moolenaar  * Copyright (c) 1988, 1989, 1990, 1993
53955d011SMarcel Moolenaar  *	The Regents of the University of California.  All rights reserved.
63955d011SMarcel Moolenaar  *
73955d011SMarcel Moolenaar  * This code is derived from software contributed to Berkeley by
83955d011SMarcel Moolenaar  * Adam de Boor.
93955d011SMarcel Moolenaar  *
103955d011SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
113955d011SMarcel Moolenaar  * modification, are permitted provided that the following conditions
123955d011SMarcel Moolenaar  * are met:
133955d011SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
143955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
153955d011SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
163955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
173955d011SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
183955d011SMarcel Moolenaar  * 3. Neither the name of the University nor the names of its contributors
193955d011SMarcel Moolenaar  *    may be used to endorse or promote products derived from this software
203955d011SMarcel Moolenaar  *    without specific prior written permission.
213955d011SMarcel Moolenaar  *
223955d011SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
233955d011SMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
243955d011SMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
253955d011SMarcel Moolenaar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
263955d011SMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
273955d011SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
283955d011SMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
293955d011SMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
303955d011SMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
313955d011SMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
323955d011SMarcel Moolenaar  * SUCH DAMAGE.
333955d011SMarcel Moolenaar  */
343955d011SMarcel Moolenaar 
35dba7b0efSSimon J. Gerraty /*
363955d011SMarcel Moolenaar  * Copyright (c) 1989 by Berkeley Softworks
373955d011SMarcel Moolenaar  * All rights reserved.
383955d011SMarcel Moolenaar  *
393955d011SMarcel Moolenaar  * This code is derived from software contributed to Berkeley by
403955d011SMarcel Moolenaar  * Adam de Boor.
413955d011SMarcel Moolenaar  *
423955d011SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
433955d011SMarcel Moolenaar  * modification, are permitted provided that the following conditions
443955d011SMarcel Moolenaar  * are met:
453955d011SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
463955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
473955d011SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
483955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
493955d011SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
503955d011SMarcel Moolenaar  * 3. All advertising materials mentioning features or use of this software
513955d011SMarcel Moolenaar  *    must display the following acknowledgement:
523955d011SMarcel Moolenaar  *	This product includes software developed by the University of
533955d011SMarcel Moolenaar  *	California, Berkeley and its contributors.
543955d011SMarcel Moolenaar  * 4. Neither the name of the University nor the names of its contributors
553955d011SMarcel Moolenaar  *    may be used to endorse or promote products derived from this software
563955d011SMarcel Moolenaar  *    without specific prior written permission.
573955d011SMarcel Moolenaar  *
583955d011SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
593955d011SMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
603955d011SMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
613955d011SMarcel Moolenaar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
623955d011SMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
633955d011SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
643955d011SMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
653955d011SMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
663955d011SMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
673955d011SMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
683955d011SMarcel Moolenaar  * SUCH DAMAGE.
693955d011SMarcel Moolenaar  */
703955d011SMarcel Moolenaar 
713955d011SMarcel Moolenaar #include "make.h"
723955d011SMarcel Moolenaar 
73956e45f6SSimon J. Gerraty /*	"@(#)str.c	5.8 (Berkeley) 6/1/90"	*/
74*22619282SSimon J. Gerraty MAKE_RCSID("$NetBSD: str.c,v 1.105 2024/07/07 07:50:57 rillig Exp $");
759f45a3c8SSimon J. Gerraty 
769f45a3c8SSimon J. Gerraty 
779f45a3c8SSimon J. Gerraty static HashTable interned_strings;
789f45a3c8SSimon J. Gerraty 
79956e45f6SSimon J. Gerraty 
802c3632d1SSimon J. Gerraty /* Return the concatenation of s1 and s2, freshly allocated. */
813955d011SMarcel Moolenaar char *
str_concat2(const char * s1,const char * s2)822c3632d1SSimon J. Gerraty str_concat2(const char *s1, const char *s2)
833955d011SMarcel Moolenaar {
842c3632d1SSimon J. Gerraty 	size_t len1 = strlen(s1);
852c3632d1SSimon J. Gerraty 	size_t len2 = strlen(s2);
862c3632d1SSimon J. Gerraty 	char *result = bmake_malloc(len1 + len2 + 1);
873955d011SMarcel Moolenaar 	memcpy(result, s1, len1);
883955d011SMarcel Moolenaar 	memcpy(result + len1, s2, len2 + 1);
893841c287SSimon J. Gerraty 	return result;
903955d011SMarcel Moolenaar }
913955d011SMarcel Moolenaar 
922c3632d1SSimon J. Gerraty /* Return the concatenation of s1, s2 and s3, freshly allocated. */
932c3632d1SSimon J. Gerraty char *
str_concat3(const char * s1,const char * s2,const char * s3)942c3632d1SSimon J. Gerraty str_concat3(const char *s1, const char *s2, const char *s3)
953955d011SMarcel Moolenaar {
962c3632d1SSimon J. Gerraty 	size_t len1 = strlen(s1);
972c3632d1SSimon J. Gerraty 	size_t len2 = strlen(s2);
982c3632d1SSimon J. Gerraty 	size_t len3 = strlen(s3);
992c3632d1SSimon J. Gerraty 	char *result = bmake_malloc(len1 + len2 + len3 + 1);
1002c3632d1SSimon J. Gerraty 	memcpy(result, s1, len1);
1012c3632d1SSimon J. Gerraty 	memcpy(result + len1, s2, len2);
1022c3632d1SSimon J. Gerraty 	memcpy(result + len1 + len2, s3, len3 + 1);
1032c3632d1SSimon J. Gerraty 	return result;
1042c3632d1SSimon J. Gerraty }
1052c3632d1SSimon J. Gerraty 
10606b9b3e0SSimon J. Gerraty /*
10706b9b3e0SSimon J. Gerraty  * Fracture a string into an array of words (as delineated by tabs or spaces)
108e2eeea75SSimon J. Gerraty  * taking quotation marks into account.
1092c3632d1SSimon J. Gerraty  *
110d5e0a182SSimon J. Gerraty  * A string that is empty or only contains whitespace nevertheless results in
111d5e0a182SSimon J. Gerraty  * a single word.  This is unexpected in many places, and the caller needs to
112d5e0a182SSimon J. Gerraty  * correct for this edge case.
113d5e0a182SSimon J. Gerraty  *
114b0c40a00SSimon J. Gerraty  * If expand is true, quotes are removed and escape sequences such as \r, \t,
115e2eeea75SSimon J. Gerraty  * etc... are expanded. In this case, return NULL on parse errors.
1162c3632d1SSimon J. Gerraty  *
117e2eeea75SSimon J. Gerraty  * Returns the fractured words, which must be freed later using Words_Free,
118e2eeea75SSimon J. Gerraty  * unless the returned Words.words was NULL.
1192c3632d1SSimon J. Gerraty  */
120b0c40a00SSimon J. Gerraty SubstringWords
Substring_Words(const char * str,bool expand)121b0c40a00SSimon J. Gerraty Substring_Words(const char *str, bool expand)
1222c3632d1SSimon J. Gerraty {
12349caa483SSimon J. Gerraty 	size_t str_len;
1242c3632d1SSimon J. Gerraty 	char *words_buf;
1252c3632d1SSimon J. Gerraty 	size_t words_cap;
126b0c40a00SSimon J. Gerraty 	Substring *words;
1272c3632d1SSimon J. Gerraty 	size_t words_len;
1282c3632d1SSimon J. Gerraty 	char inquote;
1292c3632d1SSimon J. Gerraty 	char *word_start;
1302c3632d1SSimon J. Gerraty 	char *word_end;
1312c3632d1SSimon J. Gerraty 	const char *str_p;
1323955d011SMarcel Moolenaar 
133e2eeea75SSimon J. Gerraty 	/* XXX: why only hspace, not whitespace? */
134e2eeea75SSimon J. Gerraty 	cpp_skip_hspace(&str);	/* skip leading space chars. */
1353955d011SMarcel Moolenaar 
13649caa483SSimon J. Gerraty 	/* words_buf holds the words, separated by '\0'. */
13749caa483SSimon J. Gerraty 	str_len = strlen(str);
13806b9b3e0SSimon J. Gerraty 	words_buf = bmake_malloc(str_len + 1);
1393955d011SMarcel Moolenaar 
140956e45f6SSimon J. Gerraty 	words_cap = str_len / 5 > 50 ? str_len / 5 : 50;
141b0c40a00SSimon J. Gerraty 	words = bmake_malloc((words_cap + 1) * sizeof(words[0]));
1425bcb7424SSimon J. Gerraty 
1435bcb7424SSimon J. Gerraty 	/*
1443955d011SMarcel Moolenaar 	 * copy the string; at the same time, parse backslashes,
14549caa483SSimon J. Gerraty 	 * quotes and build the word list.
1463955d011SMarcel Moolenaar 	 */
14749caa483SSimon J. Gerraty 	words_len = 0;
1483955d011SMarcel Moolenaar 	inquote = '\0';
1492c3632d1SSimon J. Gerraty 	word_start = words_buf;
1502c3632d1SSimon J. Gerraty 	word_end = words_buf;
151dba7b0efSSimon J. Gerraty 	for (str_p = str;; str_p++) {
15249caa483SSimon J. Gerraty 		char ch = *str_p;
15349caa483SSimon J. Gerraty 		switch (ch) {
1543955d011SMarcel Moolenaar 		case '"':
1553955d011SMarcel Moolenaar 		case '\'':
15606b9b3e0SSimon J. Gerraty 			if (inquote != '\0') {
1573955d011SMarcel Moolenaar 				if (inquote == ch)
1583955d011SMarcel Moolenaar 					inquote = '\0';
1593955d011SMarcel Moolenaar 				else
1603955d011SMarcel Moolenaar 					break;
1612c3632d1SSimon J. Gerraty 			} else {
162956e45f6SSimon J. Gerraty 				inquote = ch;
1633955d011SMarcel Moolenaar 				/* Don't miss "" or '' */
16449caa483SSimon J. Gerraty 				if (word_start == NULL && str_p[1] == inquote) {
1653955d011SMarcel Moolenaar 					if (!expand) {
16649caa483SSimon J. Gerraty 						word_start = word_end;
16749caa483SSimon J. Gerraty 						*word_end++ = ch;
1683955d011SMarcel Moolenaar 					} else
16949caa483SSimon J. Gerraty 						word_start = word_end + 1;
17049caa483SSimon J. Gerraty 					str_p++;
1713955d011SMarcel Moolenaar 					inquote = '\0';
1723955d011SMarcel Moolenaar 					break;
1733955d011SMarcel Moolenaar 				}
1743955d011SMarcel Moolenaar 			}
1753955d011SMarcel Moolenaar 			if (!expand) {
17649caa483SSimon J. Gerraty 				if (word_start == NULL)
17749caa483SSimon J. Gerraty 					word_start = word_end;
17849caa483SSimon J. Gerraty 				*word_end++ = ch;
1793955d011SMarcel Moolenaar 			}
1803955d011SMarcel Moolenaar 			continue;
1813955d011SMarcel Moolenaar 		case ' ':
1823955d011SMarcel Moolenaar 		case '\t':
1833955d011SMarcel Moolenaar 		case '\n':
18406b9b3e0SSimon J. Gerraty 			if (inquote != '\0')
1853955d011SMarcel Moolenaar 				break;
18649caa483SSimon J. Gerraty 			if (word_start == NULL)
1873955d011SMarcel Moolenaar 				continue;
1883955d011SMarcel Moolenaar 			/* FALLTHROUGH */
1893955d011SMarcel Moolenaar 		case '\0':
1903955d011SMarcel Moolenaar 			/*
19149caa483SSimon J. Gerraty 			 * end of a token -- make sure there's enough words
1923955d011SMarcel Moolenaar 			 * space and save off a pointer.
1933955d011SMarcel Moolenaar 			 */
19449caa483SSimon J. Gerraty 			if (word_start == NULL)
1953955d011SMarcel Moolenaar 				goto done;
1963955d011SMarcel Moolenaar 
19749caa483SSimon J. Gerraty 			*word_end++ = '\0';
19849caa483SSimon J. Gerraty 			if (words_len == words_cap) {
199b0c40a00SSimon J. Gerraty 				words_cap *= 2;
20012904384SSimon J. Gerraty 				words = bmake_realloc(words,
20112904384SSimon J. Gerraty 				    (words_cap + 1) * sizeof(words[0]));
2023955d011SMarcel Moolenaar 			}
203b0c40a00SSimon J. Gerraty 			words[words_len++] =
204b0c40a00SSimon J. Gerraty 			    Substring_Init(word_start, word_end - 1);
20549caa483SSimon J. Gerraty 			word_start = NULL;
2063955d011SMarcel Moolenaar 			if (ch == '\n' || ch == '\0') {
20706b9b3e0SSimon J. Gerraty 				if (expand && inquote != '\0') {
208b0c40a00SSimon J. Gerraty 					SubstringWords res;
209b0c40a00SSimon J. Gerraty 
21049caa483SSimon J. Gerraty 					free(words);
21149caa483SSimon J. Gerraty 					free(words_buf);
212b0c40a00SSimon J. Gerraty 
213b0c40a00SSimon J. Gerraty 					res.words = NULL;
214b0c40a00SSimon J. Gerraty 					res.len = 0;
215b0c40a00SSimon J. Gerraty 					res.freeIt = NULL;
216b0c40a00SSimon J. Gerraty 					return res;
2173955d011SMarcel Moolenaar 				}
2183955d011SMarcel Moolenaar 				goto done;
2193955d011SMarcel Moolenaar 			}
2203955d011SMarcel Moolenaar 			continue;
2213955d011SMarcel Moolenaar 		case '\\':
2223955d011SMarcel Moolenaar 			if (!expand) {
22349caa483SSimon J. Gerraty 				if (word_start == NULL)
22449caa483SSimon J. Gerraty 					word_start = word_end;
22549caa483SSimon J. Gerraty 				*word_end++ = '\\';
226954401e6SSimon J. Gerraty 				/* catch lonely '\' at end of string */
22749caa483SSimon J. Gerraty 				if (str_p[1] == '\0')
2283955d011SMarcel Moolenaar 					continue;
22949caa483SSimon J. Gerraty 				ch = *++str_p;
2303955d011SMarcel Moolenaar 				break;
2313955d011SMarcel Moolenaar 			}
2323955d011SMarcel Moolenaar 
23349caa483SSimon J. Gerraty 			switch (ch = *++str_p) {
2343955d011SMarcel Moolenaar 			case '\0':
2353955d011SMarcel Moolenaar 			case '\n':
2363955d011SMarcel Moolenaar 				/* hmmm; fix it up as best we can */
2373955d011SMarcel Moolenaar 				ch = '\\';
238e2eeea75SSimon J. Gerraty 				str_p--;
2393955d011SMarcel Moolenaar 				break;
2403955d011SMarcel Moolenaar 			case 'b':
2413955d011SMarcel Moolenaar 				ch = '\b';
2423955d011SMarcel Moolenaar 				break;
2433955d011SMarcel Moolenaar 			case 'f':
2443955d011SMarcel Moolenaar 				ch = '\f';
2453955d011SMarcel Moolenaar 				break;
2463955d011SMarcel Moolenaar 			case 'n':
2473955d011SMarcel Moolenaar 				ch = '\n';
2483955d011SMarcel Moolenaar 				break;
2493955d011SMarcel Moolenaar 			case 'r':
2503955d011SMarcel Moolenaar 				ch = '\r';
2513955d011SMarcel Moolenaar 				break;
2523955d011SMarcel Moolenaar 			case 't':
2533955d011SMarcel Moolenaar 				ch = '\t';
2543955d011SMarcel Moolenaar 				break;
2553955d011SMarcel Moolenaar 			}
2563955d011SMarcel Moolenaar 			break;
2573955d011SMarcel Moolenaar 		}
25849caa483SSimon J. Gerraty 		if (word_start == NULL)
25949caa483SSimon J. Gerraty 			word_start = word_end;
26049caa483SSimon J. Gerraty 		*word_end++ = ch;
2613955d011SMarcel Moolenaar 	}
2622c3632d1SSimon J. Gerraty done:
263b0c40a00SSimon J. Gerraty 	words[words_len] = Substring_Init(NULL, NULL);	/* useful for argv */
264b0c40a00SSimon J. Gerraty 
265b0c40a00SSimon J. Gerraty 	{
266b0c40a00SSimon J. Gerraty 		SubstringWords result;
267b0c40a00SSimon J. Gerraty 
268b0c40a00SSimon J. Gerraty 		result.words = words;
269b0c40a00SSimon J. Gerraty 		result.len = words_len;
270b0c40a00SSimon J. Gerraty 		result.freeIt = words_buf;
271b0c40a00SSimon J. Gerraty 		return result;
272b0c40a00SSimon J. Gerraty 	}
273b0c40a00SSimon J. Gerraty }
274b0c40a00SSimon J. Gerraty 
275b0c40a00SSimon J. Gerraty Words
Str_Words(const char * str,bool expand)276b0c40a00SSimon J. Gerraty Str_Words(const char *str, bool expand)
277b0c40a00SSimon J. Gerraty {
278b0c40a00SSimon J. Gerraty 	SubstringWords swords;
279b0c40a00SSimon J. Gerraty 	Words words;
280b0c40a00SSimon J. Gerraty 	size_t i;
281b0c40a00SSimon J. Gerraty 
282b0c40a00SSimon J. Gerraty 	swords = Substring_Words(str, expand);
283b0c40a00SSimon J. Gerraty 	if (swords.words == NULL) {
284b0c40a00SSimon J. Gerraty 		words.words = NULL;
285b0c40a00SSimon J. Gerraty 		words.len = 0;
286b0c40a00SSimon J. Gerraty 		words.freeIt = NULL;
287b0c40a00SSimon J. Gerraty 		return words;
288b0c40a00SSimon J. Gerraty 	}
289b0c40a00SSimon J. Gerraty 
290b0c40a00SSimon J. Gerraty 	words.words = bmake_malloc((swords.len + 1) * sizeof(words.words[0]));
291b0c40a00SSimon J. Gerraty 	words.len = swords.len;
292b0c40a00SSimon J. Gerraty 	words.freeIt = swords.freeIt;
293b0c40a00SSimon J. Gerraty 	for (i = 0; i < swords.len + 1; i++)
294b0c40a00SSimon J. Gerraty 		words.words[i] = UNCONST(swords.words[i].start);
295b0c40a00SSimon J. Gerraty 	free(swords.words);
296b0c40a00SSimon J. Gerraty 	return words;
2973955d011SMarcel Moolenaar }
2983955d011SMarcel Moolenaar 
2993955d011SMarcel Moolenaar /*
3004fde40d9SSimon J. Gerraty  * Test if a string matches a pattern like "*.[ch]". The pattern matching
3014fde40d9SSimon J. Gerraty  * characters are '*', '?' and '[]', as in fnmatch(3).
3023955d011SMarcel Moolenaar  *
303954401e6SSimon J. Gerraty  * See varmod-match.mk for examples and edge cases.
3043955d011SMarcel Moolenaar  */
305148ee845SSimon J. Gerraty StrMatchResult
Str_Match(const char * str,const char * pat)3063841c287SSimon J. Gerraty Str_Match(const char *str, const char *pat)
3073955d011SMarcel Moolenaar {
308148ee845SSimon J. Gerraty 	StrMatchResult res = { NULL, false };
309d5e0a182SSimon J. Gerraty 	bool asterisk = false;
310d5e0a182SSimon J. Gerraty 	const char *fixed_str = str;
311d5e0a182SSimon J. Gerraty 	const char *fixed_pat = pat;
312148ee845SSimon J. Gerraty 
313148ee845SSimon J. Gerraty match_fixed_length:
314148ee845SSimon J. Gerraty 	str = fixed_str;
315148ee845SSimon J. Gerraty 	pat = fixed_pat;
316148ee845SSimon J. Gerraty 	for (; *pat != '\0' && *pat != '*'; str++, pat++) {
317954401e6SSimon J. Gerraty 		if (*str == '\0')
318148ee845SSimon J. Gerraty 			return res;
3193841c287SSimon J. Gerraty 
320954401e6SSimon J. Gerraty 		if (*pat == '?')	/* match any single character */
321954401e6SSimon J. Gerraty 			continue;
322954401e6SSimon J. Gerraty 
323954401e6SSimon J. Gerraty 		if (*pat == '[') {	/* match a character from a list */
324b0c40a00SSimon J. Gerraty 			bool neg = pat[1] == '^';
3252c3632d1SSimon J. Gerraty 			pat += neg ? 2 : 1;
3263841c287SSimon J. Gerraty 
327148ee845SSimon J. Gerraty 		next_char_in_list:
328148ee845SSimon J. Gerraty 			if (*pat == '\0')
329148ee845SSimon J. Gerraty 				res.error = "Unfinished character list";
330e2eeea75SSimon J. Gerraty 			if (*pat == ']' || *pat == '\0') {
3313841c287SSimon J. Gerraty 				if (neg)
332148ee845SSimon J. Gerraty 					goto end_of_char_list;
333d5e0a182SSimon J. Gerraty 				goto no_match;
3348b054d3cSSimon J. Gerraty 			}
3353841c287SSimon J. Gerraty 			if (*pat == *str)
336148ee845SSimon J. Gerraty 				goto end_of_char_list;
337148ee845SSimon J. Gerraty 			if (pat[1] == '-' && pat[2] == '\0') {
338148ee845SSimon J. Gerraty 				res.error = "Unfinished character range";
339148ee845SSimon J. Gerraty 				res.matched = neg;
340148ee845SSimon J. Gerraty 				return res;
341148ee845SSimon J. Gerraty 			}
3423841c287SSimon J. Gerraty 			if (pat[1] == '-') {
343548bfc56SSimon J. Gerraty 				unsigned char e1 = (unsigned char)pat[0];
344548bfc56SSimon J. Gerraty 				unsigned char c = (unsigned char)*str;
345548bfc56SSimon J. Gerraty 				unsigned char e2 = (unsigned char)pat[2];
346548bfc56SSimon J. Gerraty 				if ((e1 <= c && c <= e2)
347548bfc56SSimon J. Gerraty 				    || (e2 <= c && c <= e1))
348148ee845SSimon J. Gerraty 					goto end_of_char_list;
3493841c287SSimon J. Gerraty 				pat += 2;
3503955d011SMarcel Moolenaar 			}
3513841c287SSimon J. Gerraty 			pat++;
352148ee845SSimon J. Gerraty 			goto next_char_in_list;
353148ee845SSimon J. Gerraty 
354148ee845SSimon J. Gerraty 		end_of_char_list:
355e2eeea75SSimon J. Gerraty 			if (neg && *pat != ']' && *pat != '\0')
356d5e0a182SSimon J. Gerraty 				goto no_match;
357e2eeea75SSimon J. Gerraty 			while (*pat != ']' && *pat != '\0')
3583841c287SSimon J. Gerraty 				pat++;
359*22619282SSimon J. Gerraty 			if (*pat == '\0') {
360*22619282SSimon J. Gerraty 				res.error = "Unfinished character list";
3613841c287SSimon J. Gerraty 				pat--;
362*22619282SSimon J. Gerraty 			}
363954401e6SSimon J. Gerraty 			continue;
3643955d011SMarcel Moolenaar 		}
3653841c287SSimon J. Gerraty 
366954401e6SSimon J. Gerraty 		if (*pat == '\\')	/* match the next character exactly */
3673841c287SSimon J. Gerraty 			pat++;
368d5e0a182SSimon J. Gerraty 		if (*pat != *str) {
369d5e0a182SSimon J. Gerraty 			if (asterisk && str == fixed_str) {
370d5e0a182SSimon J. Gerraty 				while (*str != '\0' && *str != *pat)
371d5e0a182SSimon J. Gerraty 					str++;
372d5e0a182SSimon J. Gerraty 				fixed_str = str;
373d5e0a182SSimon J. Gerraty 				goto match_fixed_length;
3743841c287SSimon J. Gerraty 			}
375d5e0a182SSimon J. Gerraty 			goto no_match;
376d5e0a182SSimon J. Gerraty 		}
377d5e0a182SSimon J. Gerraty 	}
378148ee845SSimon J. Gerraty 
379d5e0a182SSimon J. Gerraty 	if (*pat == '*') {
380148ee845SSimon J. Gerraty 		asterisk = true;
381148ee845SSimon J. Gerraty 		while (*pat == '*')
382148ee845SSimon J. Gerraty 			pat++;
383148ee845SSimon J. Gerraty 		if (*pat == '\0') {
384148ee845SSimon J. Gerraty 			res.matched = true;
385148ee845SSimon J. Gerraty 			return res;
386148ee845SSimon J. Gerraty 		}
387148ee845SSimon J. Gerraty 		fixed_str = str;
388148ee845SSimon J. Gerraty 		fixed_pat = pat;
389148ee845SSimon J. Gerraty 		goto match_fixed_length;
3903841c287SSimon J. Gerraty 	}
391d5e0a182SSimon J. Gerraty 	if (asterisk && *str != '\0') {
392d5e0a182SSimon J. Gerraty 		fixed_str += strlen(str);
393d5e0a182SSimon J. Gerraty 		goto match_fixed_length;
394d5e0a182SSimon J. Gerraty 	}
395d5e0a182SSimon J. Gerraty 	res.matched = *str == '\0';
396d5e0a182SSimon J. Gerraty 	return res;
397d5e0a182SSimon J. Gerraty 
398d5e0a182SSimon J. Gerraty no_match:
399d5e0a182SSimon J. Gerraty 	if (!asterisk)
400d5e0a182SSimon J. Gerraty 		return res;
401d5e0a182SSimon J. Gerraty 	fixed_str++;
402d5e0a182SSimon J. Gerraty 	goto match_fixed_length;
403d5e0a182SSimon J. Gerraty }
4049f45a3c8SSimon J. Gerraty 
4059f45a3c8SSimon J. Gerraty void
Str_Intern_Init(void)4069f45a3c8SSimon J. Gerraty Str_Intern_Init(void)
4079f45a3c8SSimon J. Gerraty {
4089f45a3c8SSimon J. Gerraty 	HashTable_Init(&interned_strings);
4099f45a3c8SSimon J. Gerraty }
4109f45a3c8SSimon J. Gerraty 
411*22619282SSimon J. Gerraty #ifdef CLEANUP
4129f45a3c8SSimon J. Gerraty void
Str_Intern_End(void)4139f45a3c8SSimon J. Gerraty Str_Intern_End(void)
4149f45a3c8SSimon J. Gerraty {
4159f45a3c8SSimon J. Gerraty 	HashTable_Done(&interned_strings);
4169f45a3c8SSimon J. Gerraty }
417*22619282SSimon J. Gerraty #endif
4189f45a3c8SSimon J. Gerraty 
4199f45a3c8SSimon J. Gerraty /* Return a canonical instance of str, with unlimited lifetime. */
4209f45a3c8SSimon J. Gerraty const char *
Str_Intern(const char * str)4219f45a3c8SSimon J. Gerraty Str_Intern(const char *str)
4229f45a3c8SSimon J. Gerraty {
4239f45a3c8SSimon J. Gerraty 	return HashTable_CreateEntry(&interned_strings, str, NULL)->key;
4249f45a3c8SSimon J. Gerraty }
425