1 /* $NetBSD: str.c,v 1.94 2022/12/07 10:28:48 rillig Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
39 * This code is derived from software contributed to Berkeley by
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 /* "@(#)str.c 5.8 (Berkeley) 6/1/90" */
74 MAKE_RCSID("$NetBSD: str.c,v 1.94 2022/12/07 10:28:48 rillig Exp $");
77 static HashTable interned_strings;
80 /* Return the concatenation of s1 and s2, freshly allocated. */
82 str_concat2(const char *s1, const char *s2)
84 size_t len1 = strlen(s1);
85 size_t len2 = strlen(s2);
86 char *result = bmake_malloc(len1 + len2 + 1);
87 memcpy(result, s1, len1);
88 memcpy(result + len1, s2, len2 + 1);
92 /* Return the concatenation of s1, s2 and s3, freshly allocated. */
94 str_concat3(const char *s1, const char *s2, const char *s3)
96 size_t len1 = strlen(s1);
97 size_t len2 = strlen(s2);
98 size_t len3 = strlen(s3);
99 char *result = bmake_malloc(len1 + len2 + len3 + 1);
100 memcpy(result, s1, len1);
101 memcpy(result + len1, s2, len2);
102 memcpy(result + len1 + len2, s3, len3 + 1);
107 * Fracture a string into an array of words (as delineated by tabs or spaces)
108 * taking quotation marks into account.
110 * If expand is true, quotes are removed and escape sequences such as \r, \t,
111 * etc... are expanded. In this case, return NULL on parse errors.
113 * Returns the fractured words, which must be freed later using Words_Free,
114 * unless the returned Words.words was NULL.
117 Substring_Words(const char *str, bool expand)
129 /* XXX: why only hspace, not whitespace? */
130 cpp_skip_hspace(&str); /* skip leading space chars. */
132 /* words_buf holds the words, separated by '\0'. */
133 str_len = strlen(str);
134 words_buf = bmake_malloc(str_len + 1);
136 words_cap = str_len / 5 > 50 ? str_len / 5 : 50;
137 words = bmake_malloc((words_cap + 1) * sizeof(words[0]));
140 * copy the string; at the same time, parse backslashes,
141 * quotes and build the word list.
145 word_start = words_buf;
146 word_end = words_buf;
147 for (str_p = str;; str_p++) {
152 if (inquote != '\0') {
159 /* Don't miss "" or '' */
160 if (word_start == NULL && str_p[1] == inquote) {
162 word_start = word_end;
165 word_start = word_end + 1;
172 if (word_start == NULL)
173 word_start = word_end;
182 if (word_start == NULL)
187 * end of a token -- make sure there's enough words
188 * space and save off a pointer.
190 if (word_start == NULL)
194 if (words_len == words_cap) {
196 words = bmake_realloc(words,
197 (words_cap + 1) * sizeof(words[0]));
200 Substring_Init(word_start, word_end - 1);
202 if (ch == '\n' || ch == '\0') {
203 if (expand && inquote != '\0') {
219 if (word_start == NULL)
220 word_start = word_end;
222 /* catch lonely '\' at end of string */
223 if (str_p[1] == '\0')
229 switch (ch = *++str_p) {
232 /* hmmm; fix it up as best we can */
254 if (word_start == NULL)
255 word_start = word_end;
259 words[words_len] = Substring_Init(NULL, NULL); /* useful for argv */
262 SubstringWords result;
264 result.words = words;
265 result.len = words_len;
266 result.freeIt = words_buf;
272 Str_Words(const char *str, bool expand)
274 SubstringWords swords;
278 swords = Substring_Words(str, expand);
279 if (swords.words == NULL) {
286 words.words = bmake_malloc((swords.len + 1) * sizeof(words.words[0]));
287 words.len = swords.len;
288 words.freeIt = swords.freeIt;
289 for (i = 0; i < swords.len + 1; i++)
290 words.words[i] = UNCONST(swords.words[i].start);
296 * XXX: In the extreme edge case that one of the characters is from the basic
297 * execution character set and the other isn't, the result of the comparison
298 * differs depending on whether plain char is signed or unsigned.
300 * An example is the character range from \xE4 to 'a', where \xE4 may come
301 * from U+00E4 'Latin small letter A with diaeresis'.
303 * If char is signed, \xE4 evaluates to -28, the first half of the condition
304 * becomes -28 <= '0' && '0' <= 'a', which evaluates to true.
306 * If char is unsigned, \xE4 evaluates to 228, the second half of the
307 * condition becomes 'a' <= '0' && '0' <= 228, which evaluates to false.
310 in_range(char e1, char c, char e2)
312 return (e1 <= c && c <= e2) || (e2 <= c && c <= e1);
316 * Test if a string matches a pattern like "*.[ch]". The pattern matching
317 * characters are '*', '?' and '[]', as in fnmatch(3).
319 * XXX: this function does not detect or report malformed patterns.
321 * See varmod-match.mk for examples and edge cases.
324 Str_Match(const char *str, const char *pat)
326 for (; *pat != '\0'; pat++, str++) {
327 if (*pat == '*') { /* match any substring */
333 for (; *str != '\0'; str++)
334 if (Str_Match(str, pat))
342 if (*pat == '?') /* match any single character */
345 if (*pat == '[') { /* match a character from a list */
346 bool neg = pat[1] == '^';
350 if (*pat == ']' || *pat == '\0') {
360 if (in_range(pat[0], *str, pat[2]))
366 if (neg && *pat != ']' && *pat != '\0')
368 while (*pat != ']' && *pat != '\0')
375 if (*pat == '\\') /* match the next character exactly */
385 Str_Intern_Init(void)
387 HashTable_Init(&interned_strings);
394 HashTable_Done(&interned_strings);
398 /* Return a canonical instance of str, with unlimited lifetime. */
400 Str_Intern(const char *str)
402 return HashTable_CreateEntry(&interned_strings, str, NULL)->key;