1 /* Concatenate variable number of strings.
2 Copyright (C) 1991, 1994, 2001, 2011 Free Software Foundation, Inc.
3 Written by Fred Fish @ Cygnus Support
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19 Boston, MA 02110-1301, USA. */
24 @deftypefn Extension char* concat (const char *@var{s1}, const char *@var{s2}, @
27 Concatenate zero or more of strings and return the result in freshly
28 @code{xmalloc}ed memory. The argument list is terminated by the first
29 @code{NULL} pointer encountered. Pointers to empty strings are ignored.
40 #include "libiberty.h"
41 #include <sys/types.h> /* size_t */
57 static inline unsigned long vconcat_length (const char *, va_list);
58 static inline unsigned long
59 vconcat_length (const char *first
, va_list args
)
61 unsigned long length
= 0;
64 for (arg
= first
; arg
; arg
= va_arg (args
, const char *))
65 length
+= strlen (arg
);
71 vconcat_copy (char *dst
, const char *first
, va_list args
)
76 for (arg
= first
; arg
; arg
= va_arg (args
, const char *))
78 unsigned long length
= strlen (arg
);
79 memcpy (end
, arg
, length
);
87 /* @undocumented concat_length */
90 concat_length (const char *first
, ...)
94 VA_OPEN (args
, first
);
95 VA_FIXEDARG (args
, const char *, first
);
96 length
= vconcat_length (first
, args
);
102 /* @undocumented concat_copy */
105 concat_copy (char *dst
, const char *first
, ...)
109 VA_OPEN (args
, first
);
110 VA_FIXEDARG (args
, char *, dst
);
111 VA_FIXEDARG (args
, const char *, first
);
112 vconcat_copy (dst
, first
, args
);
113 save_dst
= dst
; /* With K&R C, dst goes out of scope here. */
121 #endif /* __cplusplus */
122 char *libiberty_concat_ptr
;
125 #endif /* __cplusplus */
127 /* @undocumented concat_copy2 */
130 concat_copy2 (const char *first
, ...)
132 VA_OPEN (args
, first
);
133 VA_FIXEDARG (args
, const char *, first
);
134 vconcat_copy (libiberty_concat_ptr
, first
, args
);
137 return libiberty_concat_ptr
;
141 concat (const char *first
, ...)
145 /* First compute the size of the result and get sufficient memory. */
146 VA_OPEN (args
, first
);
147 VA_FIXEDARG (args
, const char *, first
);
148 newstr
= XNEWVEC (char, vconcat_length (first
, args
) + 1);
151 /* Now copy the individual pieces to the result string. */
152 VA_OPEN (args
, first
);
153 VA_FIXEDARG (args
, const char *, first
);
154 vconcat_copy (newstr
, first
, args
);
162 @deftypefn Extension char* reconcat (char *@var{optr}, const char *@var{s1}, @
163 @dots{}, @code{NULL})
165 Same as @code{concat}, except that if @var{optr} is not @code{NULL} it
166 is freed after the string is created. This is intended to be useful
167 when you're extending an existing string or building up a string in a
171 str = reconcat (str, "pre-", str, NULL);
179 reconcat (char *optr
, const char *first
, ...)
183 /* First compute the size of the result and get sufficient memory. */
184 VA_OPEN (args
, first
);
185 VA_FIXEDARG (args
, char *, optr
);
186 VA_FIXEDARG (args
, const char *, first
);
187 newstr
= XNEWVEC (char, vconcat_length (first
, args
) + 1);
190 /* Now copy the individual pieces to the result string. */
191 VA_OPEN (args
, first
);
192 VA_FIXEDARG (args
, char *, optr
);
193 VA_FIXEDARG (args
, const char *, first
);
194 vconcat_copy (newstr
, first
, args
);
195 if (optr
) /* Done before VA_CLOSE so optr stays in scope for K&R C. */
203 #define NULLP (char *)0
205 /* Simple little test driver. */
212 printf ("\"\" = \"%s\"\n", concat (NULLP
));
213 printf ("\"a\" = \"%s\"\n", concat ("a", NULLP
));
214 printf ("\"ab\" = \"%s\"\n", concat ("a", "b", NULLP
));
215 printf ("\"abc\" = \"%s\"\n", concat ("a", "b", "c", NULLP
));
216 printf ("\"abcd\" = \"%s\"\n", concat ("ab", "cd", NULLP
));
217 printf ("\"abcde\" = \"%s\"\n", concat ("ab", "c", "de", NULLP
));
218 printf ("\"abcdef\" = \"%s\"\n", concat ("", "a", "", "bcd", "ef", NULLP
));