source: git/src/str.c @ 147847c

stereo-2025warn-only-for-hanging-survey
Last change on this file since 147847c was 9ba6f86, checked in by Olly Betts <olly@…>, 16 months ago

Fix append of empty string to unallocated dynamic string

  • Property mode set to 100644
File size: 1.9 KB
RevLine 
[9286525]1/* append a string */
[0532954]2/* Copyright (c) Olly Betts 1999, 2014, 2024
[846746e]3 *
[89231c4]4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
[846746e]8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
[89231c4]11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
[846746e]13 *
[89231c4]14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
[ecbc6c18]16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
[846746e]17 */
18
[0532954]19#include <config.h>
20
21#include "str.h"
22
[99d5754]23#include <string.h>
24
25#include "osalloc.h"
26
[0532954]27void s_expand_(string *pstr, int addition) {
28    int new_size = (pstr->len + addition + 33) & ~7;
29    pstr->s = osrealloc(pstr->s, new_size);
30    pstr->capacity = new_size - 1;
[bd2e33a]31}
32
33void
[0532954]34s_catlen(string* pstr, const char *s, int s_len)
[bd2e33a]35{
[9ba6f86]36   if (pstr->capacity - pstr->len < s_len || s_len == 0)
[0532954]37       s_expand_(pstr, s_len);
38   memcpy(pstr->s + pstr->len, s, s_len);
39   pstr->len += s_len;
40   pstr->s[pstr->len] = '\0';
41}
[99d5754]42
[0532954]43extern inline void s_cat(string *pstr, const char *s);
[99d5754]44
[0532954]45extern inline void s_catchar(string *pstr, char c);
[cb3d1e2]46
[0532954]47extern inline void s_clear(string *pstr);
[99d5754]48
[0532954]49extern inline void s_truncate(string *pstr, int new_len);
[99d5754]50
[0532954]51extern inline void s_free(string *pstr);
[99d5754]52
[0532954]53extern inline char *s_steal(string *pstr);
[cb3d1e2]54
[0532954]55extern inline void s_donate(string *pstr, char *s);
56
57extern inline int s_len(const string *pstr);
58
59extern inline bool s_empty(const string *pstr);
60
61extern inline const char *s_str(string *pstr);
62
63extern inline bool s_eqlen(const string *pstr, const char *s, int s_len);
64
65extern inline bool s_eq(const string *pstr, const char *s);
Note: See TracBrowser for help on using the repository browser.