[gdb/testsuite] Fix license text in gdb.reverse/map-to-same-line.{c,exp}
[binutils-gdb.git] / gprofng / src / StringBuilder.cc
blob9901b62c1a72aced2c548d0d826074144358c988
1 /* Copyright (C) 2021-2024 Free Software Foundation, Inc.
2 Contributed by Oracle.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
21 #include "config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <values.h>
26 #include <stdarg.h>
27 #include <unistd.h>
29 #include "gp-defs.h"
30 #include "StringBuilder.h"
31 #include "i18n.h"
33 StringBuilder::StringBuilder ()
35 count = 0;
36 maxCapacity = 16;
37 value = (char *) malloc (maxCapacity);
38 memset (value, 0, maxCapacity);
41 StringBuilder::StringBuilder (int capacity)
43 count = 0;
44 maxCapacity = capacity;
45 value = (char *) malloc (maxCapacity);
46 memset (value, 0, maxCapacity);
49 StringBuilder::~StringBuilder ()
51 free (value);
54 void
55 StringBuilder::ensureCapacity (int minimumCapacity)
57 if (minimumCapacity > maxCapacity)
58 expandCapacity (minimumCapacity);
61 void
62 StringBuilder::expandCapacity (int minimumCapacity)
64 int newCapacity = (maxCapacity + 1) * 2;
65 if (newCapacity < 0)
66 newCapacity = MAXINT;
67 else if (minimumCapacity > newCapacity)
68 newCapacity = minimumCapacity;
69 char *newValue = (char *) malloc (newCapacity);
70 maxCapacity = newCapacity;
71 memcpy (newValue, value, count);
72 memset (newValue + count, 0, maxCapacity - count);
73 free (value);
74 value = newValue;
77 void
78 StringBuilder::trimToSize ()
80 if (count < maxCapacity)
82 char *newValue = (char *) malloc (count);
83 maxCapacity = count;
84 memcpy (newValue, value, count);
85 free (value);
86 value = newValue;
90 void
91 StringBuilder::trim ()
93 while (count > 0)
95 if (value[count - 1] != ' ')
96 break;
97 count--;
101 void
102 StringBuilder::setLength (int newLength)
104 if (newLength < 0)
105 return;
106 if (newLength > maxCapacity)
107 expandCapacity (newLength);
108 if (count < newLength)
110 for (; count < newLength; count++)
111 value[count] = '\0';
113 else
114 count = newLength;
117 char
118 StringBuilder::charAt (int index)
120 if (index < 0 || index >= count)
121 return 0;
122 return value[index];
125 void
126 StringBuilder::getChars (int srcBegin, int srcEnd, char dst[], int dstBegin)
128 if (srcBegin < 0)
129 return;
130 if (srcEnd < 0 || srcEnd > count)
131 return;
132 if (srcBegin > srcEnd)
133 return;
134 memcpy (dst + dstBegin, value + srcBegin, srcEnd - srcBegin);
137 void
138 StringBuilder::setCharAt (int index, char ch)
140 if (index < 0 || index >= count)
141 return;
142 value[index] = ch;
145 StringBuilder *
146 StringBuilder::append (StringBuilder *sb)
148 if (sb == NULL)
149 return append (NTXT ("null"));
150 int len = sb->count;
151 int newcount = count + len;
152 if (newcount > maxCapacity)
153 expandCapacity (newcount);
154 sb->getChars (0, len, value, count);
155 count = newcount;
156 return this;
159 StringBuilder *
160 StringBuilder::append (const char str[])
162 int len = (int) strlen (str);
163 int newCount = count + len;
164 if (newCount > maxCapacity)
165 expandCapacity (newCount);
166 memcpy (value + count, str, len);
167 count = newCount;
168 return this;
171 StringBuilder *
172 StringBuilder::append (const char str[], int offset, int len)
174 int newCount = count + len;
175 if (newCount > maxCapacity)
176 expandCapacity (newCount);
177 memcpy (value + count, str + offset, len);
178 count = newCount;
179 return this;
182 StringBuilder *
183 StringBuilder::append (bool b)
185 if (b)
186 append (NTXT ("true"));
187 else
188 append (NTXT ("false"));
189 return this;
192 StringBuilder *
193 StringBuilder::append (char c)
195 int newCount = count + 1;
196 if (newCount > maxCapacity)
198 expandCapacity (newCount);
200 value[count++] = c;
201 return this;
204 StringBuilder *
205 StringBuilder::append (int i)
207 char buf[16];
208 snprintf (buf, sizeof (buf), NTXT ("%d"), i);
209 append (buf);
210 return this;
213 StringBuilder *
214 StringBuilder::append (unsigned int i)
216 char buf[16];
217 snprintf (buf, sizeof (buf), NTXT ("%u"), i);
218 append (buf);
219 return this;
222 StringBuilder *
223 StringBuilder::append (long lng)
225 char buf[32];
226 snprintf (buf, sizeof (buf), NTXT ("%ld"), lng);
227 append (buf);
228 return this;
231 StringBuilder *
232 StringBuilder::append (unsigned long lng)
234 char buf[32];
235 snprintf (buf, sizeof (buf), NTXT ("%lu"), lng);
236 append (buf);
237 return this;
240 StringBuilder *
241 StringBuilder::append (long long lng)
243 char buf[32];
244 snprintf (buf, sizeof (buf), NTXT ("%lld"), lng);
245 append (buf);
246 return this;
249 StringBuilder *
250 StringBuilder::append (unsigned long long lng)
252 char buf[32];
253 snprintf (buf, sizeof (buf), NTXT ("%llu"), lng);
254 append (buf);
255 return this;
258 StringBuilder *
259 StringBuilder::append (float f)
261 char buf[32];
262 snprintf (buf, sizeof (buf), NTXT ("%f"), (double) f);
263 append (buf);
264 return this;
267 StringBuilder *
268 StringBuilder::append (double d)
270 char buf[32];
271 snprintf (buf, sizeof (buf), NTXT ("%f"), d);
272 append (buf);
273 return this;
276 StringBuilder *
277 StringBuilder::_delete (int start, int end)
279 if (start < 0)
280 return this;
281 if (end > count)
282 end = count;
283 if (start > end)
284 return this;
285 int len = end - start;
286 if (len > 0)
288 memcpy (value + start, value + start + len, count - end);
289 count -= len;
291 return this;
294 StringBuilder *
295 StringBuilder::deleteCharAt (int index)
297 if (index < 0 || index >= count)
298 return this;
299 memcpy (value + index, value + index + 1, count - index - 1);
300 count--;
301 return this;
304 bool
305 StringBuilder::endsWith (const char str[])
307 if (str == NULL)
309 if (count == 0)
310 return true;
311 return false;
313 int len = (int) strlen (str);
314 if (len == 0)
315 return true;
316 int start = count - len;
317 if (start < 0)
318 return false;
319 int res = strncmp ((const char *) (value + start), str, len);
320 if (res != 0)
321 return false;
322 return true;
325 StringBuilder *
326 StringBuilder::insert (int index, const char str[], int offset, int len)
328 if (index < 0 || index > count)
329 return this;
330 if (offset < 0 || len < 0 || offset > ((int) strlen (str)) - len)
331 return this;
332 int newCount = count + len;
333 if (newCount > maxCapacity)
334 expandCapacity (newCount);
335 memcpy (value + index + len, value + index, count - index);
336 memcpy (value + index, str + offset, len);
337 count = newCount;
338 return this;
341 StringBuilder *
342 StringBuilder::insert (int offset, const char str[])
344 if (offset < 0 || offset > count)
345 return this;
346 int len = (int) strlen (str);
347 int newCount = count + len;
348 if (newCount > maxCapacity)
349 expandCapacity (newCount);
350 memcpy (value + offset + len, value + offset, count - offset);
351 memcpy (value + offset, str, len);
352 count = newCount;
353 return this;
356 StringBuilder *
357 StringBuilder::insert (int offset, bool b)
359 return insert (offset, b ? NTXT ("true") : NTXT ("false"));
362 StringBuilder *
363 StringBuilder::insert (int offset, char c)
365 int newCount = count + 1;
366 if (newCount > maxCapacity)
367 expandCapacity (newCount);
368 memcpy (value + offset + 1, value + offset, count - offset);
369 value[offset] = c;
370 count = newCount;
371 return this;
374 StringBuilder *
375 StringBuilder::insert (int offset, int i)
377 char buf[16];
378 snprintf (buf, sizeof (buf), NTXT ("%d"), i);
379 insert (offset, buf);
380 return this;
383 StringBuilder *
384 StringBuilder::insert (int offset, long l)
386 char buf[32];
387 snprintf (buf, sizeof (buf), NTXT ("%ld"), l);
388 insert (offset, buf);
389 return this;
392 StringBuilder *
393 StringBuilder::insert (int offset, float f)
395 char buf[32];
396 snprintf (buf, sizeof (buf), NTXT ("%f"), (double) f);
397 insert (offset, buf);
398 return this;
401 StringBuilder *
402 StringBuilder::insert (int offset, double d)
404 char buf[32];
405 snprintf (buf, sizeof (buf), NTXT ("%f"), d);
406 insert (offset, buf);
407 return this;
410 StringBuilder *
411 StringBuilder::reverse ()
413 int n = count - 1;
414 for (int j = (n - 1) >> 1; j >= 0; --j)
416 char temp = value[j];
417 char temp2 = value[n - j];
418 value[j] = temp2;
419 value[n - j] = temp;
421 return this;
424 //String *StringBuilder::toString();
425 char *
426 StringBuilder::toString ()
428 char *str = (char *) malloc (count + 1);
429 memcpy (str, value, count);
430 str[count] = '\0';
431 return str;
434 void
435 StringBuilder::toFile (FILE *fp)
437 append ('\0');
438 count--;
439 fprintf (fp, NTXT ("%s"), value);
442 void
443 StringBuilder::toFileLn (FILE *fp)
445 trim ();
446 append ('\0');
447 count--;
448 fprintf (fp, NTXT ("%s\n"), value);
451 void
452 StringBuilder::write (int fd)
454 if (count > 0)
455 ::write (fd, value, count);
458 StringBuilder *
459 StringBuilder::sprintf (const char *fmt, ...)
461 int cnt;
462 setLength (0);
464 va_list vp;
465 va_start (vp, fmt);
466 cnt = vsnprintf (value, maxCapacity, fmt, vp);
467 va_end (vp);
468 if (cnt < maxCapacity)
470 count = cnt;
471 return this;
474 // Have to count the trailing zero
475 ensureCapacity (cnt + 1);
476 va_start (vp, fmt);
477 count = vsnprintf (value, maxCapacity, fmt, vp);
478 va_end (vp);
479 return this;
482 StringBuilder *
483 StringBuilder::appendf (const char *fmt, ...)
485 va_list vp;
486 va_start (vp, fmt);
487 int cnt = vsnprintf (value + count, maxCapacity - count, fmt, vp);
488 va_end (vp);
489 if (cnt + count < maxCapacity)
491 count += cnt;
492 return this;
495 // Have to count the trailing zero
496 ensureCapacity (count + cnt + 1);
497 va_start (vp, fmt);
498 count += vsnprintf (value + count, maxCapacity - count, fmt, vp);
499 va_end (vp);
500 return this;
504 StringBuilder::indexOf (const char str[])
506 return indexOf (str, 0);
510 StringBuilder::indexOf (const char str[], int fromIndex)
512 int len = (int) strlen (str);
513 if (fromIndex >= count)
514 return len == 0 ? count : -1;
515 if (fromIndex < 0)
516 fromIndex = 0;
517 if (len == 0)
518 return fromIndex;
520 char first = str[0];
521 int max = (count - len);
523 for (int i = fromIndex; i <= max; i++)
525 /* Look for first character. */
526 if (value[i] != first)
527 while (++i <= max && value[i] != first)
529 /* Found first character, now look at the rest of v2 */
530 if (i <= max)
532 int j = i + 1;
533 int end = j + len - 1;
534 for (int k = 1; j < end && value[j] == str[k]; j++, k++)
536 if (j == end) /* Found whole string. */
537 return i;
540 return -1;
544 StringBuilder::lastIndexOf (const char str[])
546 return lastIndexOf (str, count);
550 StringBuilder::lastIndexOf (const char str[], int fromIndex)
553 * Check arguments; return immediately where possible. For
554 * consistency, don't check for null str.
556 int len = (int) strlen (str);
557 int rightIndex = count - len;
558 if (fromIndex < 0)
559 return -1;
560 if (fromIndex > rightIndex)
561 fromIndex = rightIndex;
562 /* Empty string always matches. */
563 if (len == 0)
564 return fromIndex;
566 int strLastIndex = len - 1;
567 char strLastChar = str[strLastIndex];
568 int min = len - 1;
569 int i = min + fromIndex;
571 while (true)
573 while (i >= min && value[i] != strLastChar)
574 i--;
575 if (i < min)
576 return -1;
578 int j = i - 1;
579 int start = j - (len - 1);
580 int k = strLastIndex - 1;
581 while (j > start)
583 if (value[j--] != str[k--])
585 i--;
586 break;
589 if (j == start)
590 return start + 1;