]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/concat.c
* config/i386/i386.md (cmpstrnsi): Remove dead code.
[thirdparty/gcc.git] / libiberty / concat.c
CommitLineData
28e9041c 1/* Concatenate variable number of strings.
fbd26352 2 Copyright (C) 1991-2019 Free Software Foundation, Inc.
28e9041c 3 Written by Fred Fish @ Cygnus Support
4
5This file is part of the libiberty library.
6Libiberty is free software; you can redistribute it and/or
7modify it under the terms of the GNU Library General Public
8License as published by the Free Software Foundation; either
9version 2 of the License, or (at your option) any later version.
10
11Libiberty is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14Library General Public License for more details.
15
16You should have received a copy of the GNU Library General Public
17License along with libiberty; see the file COPYING.LIB. If
95b8d1bc 18not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19Boston, MA 02110-1301, USA. */
28e9041c 20
21
22/*
23
bac427a1 24@deftypefn Extension char* concat (const char *@var{s1}, const char *@var{s2}, @
25 @dots{}, @code{NULL})
28e9041c 26
349e8276 27Concatenate zero or more of strings and return the result in freshly
2b0d0b81 28@code{xmalloc}ed memory. The argument list is terminated by the first
29@code{NULL} pointer encountered. Pointers to empty strings are ignored.
28e9041c 30
349e8276 31@end deftypefn
28e9041c 32
28e9041c 33*/
34
35
6c9ffcbc 36#ifdef HAVE_CONFIG_H
37#include "config.h"
38#endif
28e9041c 39#include "ansidecl.h"
40#include "libiberty.h"
c1a60b7d 41#include <sys/types.h> /* size_t */
28e9041c 42
28e9041c 43#include <stdarg.h>
28e9041c 44
222ce6b9 45# if HAVE_STRING_H
46# include <string.h>
47# else
48# if HAVE_STRINGS_H
49# include <strings.h>
50# endif
51# endif
28e9041c 52
e7faea46 53#if HAVE_STDLIB_H
54#include <stdlib.h>
55#endif
56
d94aaf66 57static inline unsigned long vconcat_length (const char *, va_list);
9659201a 58static inline unsigned long
d94aaf66 59vconcat_length (const char *first, va_list args)
28e9041c 60{
9659201a 61 unsigned long length = 0;
62 const char *arg;
28e9041c 63
222ce6b9 64 for (arg = first; arg ; arg = va_arg (args, const char *))
65 length += strlen (arg);
66
9659201a 67 return length;
68}
28e9041c 69
9659201a 70static inline char *
d94aaf66 71vconcat_copy (char *dst, const char *first, va_list args)
9659201a 72{
73 char *end = dst;
74 const char *arg;
222ce6b9 75
222ce6b9 76 for (arg = first; arg ; arg = va_arg (args, const char *))
77 {
9659201a 78 unsigned long length = strlen (arg);
222ce6b9 79 memcpy (end, arg, length);
80 end += length;
28e9041c 81 }
222ce6b9 82 *end = '\000';
9659201a 83
84 return dst;
85}
86
349e8276 87/* @undocumented concat_length */
88
9659201a 89unsigned long
d94aaf66 90concat_length (const char *first, ...)
9659201a 91{
92 unsigned long length;
e14937e9 93 va_list args;
9659201a 94
e14937e9 95 va_start (args, first);
9659201a 96 length = vconcat_length (first, args);
e14937e9 97 va_end (args);
9659201a 98
99 return length;
100}
101
349e8276 102/* @undocumented concat_copy */
103
9659201a 104char *
d94aaf66 105concat_copy (char *dst, const char *first, ...)
9659201a 106{
107 char *save_dst;
e14937e9 108 va_list args;
9659201a 109
e14937e9 110 va_start (args, first);
9659201a 111 vconcat_copy (dst, first, args);
112 save_dst = dst; /* With K&R C, dst goes out of scope here. */
e14937e9 113 va_end (args);
9659201a 114
115 return save_dst;
116}
117
f2d737fc 118#ifdef __cplusplus
119extern "C" {
120#endif /* __cplusplus */
9659201a 121char *libiberty_concat_ptr;
f2d737fc 122#ifdef __cplusplus
123}
124#endif /* __cplusplus */
9659201a 125
349e8276 126/* @undocumented concat_copy2 */
127
9659201a 128char *
d94aaf66 129concat_copy2 (const char *first, ...)
9659201a 130{
e14937e9 131 va_list args;
132 va_start (args, first);
9659201a 133 vconcat_copy (libiberty_concat_ptr, first, args);
e14937e9 134 va_end (args);
9659201a 135
136 return libiberty_concat_ptr;
137}
138
139char *
d94aaf66 140concat (const char *first, ...)
9659201a 141{
142 char *newstr;
e14937e9 143 va_list args;
9659201a 144
145 /* First compute the size of the result and get sufficient memory. */
e14937e9 146 va_start (args, first);
f2d737fc 147 newstr = XNEWVEC (char, vconcat_length (first, args) + 1);
e14937e9 148 va_end (args);
9659201a 149
150 /* Now copy the individual pieces to the result string. */
e14937e9 151 va_start (args, first);
9659201a 152 vconcat_copy (newstr, first, args);
e14937e9 153 va_end (args);
28e9041c 154
222ce6b9 155 return newstr;
28e9041c 156}
157
349e8276 158/*
159
bac427a1 160@deftypefn Extension char* reconcat (char *@var{optr}, const char *@var{s1}, @
161 @dots{}, @code{NULL})
349e8276 162
163Same as @code{concat}, except that if @var{optr} is not @code{NULL} it
164is freed after the string is created. This is intended to be useful
165when you're extending an existing string or building up a string in a
166loop:
167
168@example
169 str = reconcat (str, "pre-", str, NULL);
170@end example
171
172@end deftypefn
173
174*/
175
0be2ebc7 176char *
d94aaf66 177reconcat (char *optr, const char *first, ...)
0be2ebc7 178{
179 char *newstr;
e14937e9 180 va_list args;
0be2ebc7 181
182 /* First compute the size of the result and get sufficient memory. */
e14937e9 183 va_start (args, first);
f2d737fc 184 newstr = XNEWVEC (char, vconcat_length (first, args) + 1);
e14937e9 185 va_end (args);
0be2ebc7 186
187 /* Now copy the individual pieces to the result string. */
e14937e9 188 va_start (args, first);
0be2ebc7 189 vconcat_copy (newstr, first, args);
92f03c00 190 if (optr) /* Done before VA_CLOSE so optr stays in scope for K&R C. */
0be2ebc7 191 free (optr);
e14937e9 192 va_end (args);
0be2ebc7 193
194 return newstr;
195}
196
28e9041c 197#ifdef MAIN
222ce6b9 198#define NULLP (char *)0
28e9041c 199
200/* Simple little test driver. */
201
202#include <stdio.h>
203
204int
d94aaf66 205main (void)
28e9041c 206{
207 printf ("\"\" = \"%s\"\n", concat (NULLP));
208 printf ("\"a\" = \"%s\"\n", concat ("a", NULLP));
209 printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP));
210 printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP));
211 printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP));
212 printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP));
213 printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP));
214 return 0;
215}
216
217#endif