2 /* Copyright (C) 1989-2018 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.com)
5 This file is part of groff.
7 groff is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 groff is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "stringclass.h"
26 static char *salloc(int len, int *sizep);
27 static void sfree(char *ptr, int size);
28 static char *sfree_alloc(char *ptr, int size, int len, int *sizep);
29 static char *srealloc(char *ptr, int size, int oldlen, int newlen, int *sizep);
31 static char *salloc(int len, int *sizep)
38 return new char[*sizep = len*2];
41 static void sfree(char *ptr, int)
46 static char *sfree_alloc(char *ptr, int oldsz, int len, int *sizep)
58 return new char[*sizep = len*2];
61 static char *srealloc(char *ptr, int oldsz, int oldlen, int newlen, int *sizep)
63 if (oldsz >= newlen) {
73 char *p = new char[*sizep = newlen*2];
74 if (oldlen < newlen && oldlen != 0)
75 memcpy(p, ptr, oldlen);
81 string::string() : ptr(0), len(0), sz(0)
85 string::string(const char *p, int n) : len(n)
93 string::string(const char *p)
102 ptr = salloc(len, &sz);
108 string::string(char c) : len(1)
110 ptr = salloc(1, &sz);
114 string::string(const string &s) : len(s.len)
116 ptr = salloc(len, &sz);
118 memcpy(ptr, s.ptr, len);
126 string &string::operator=(const string &s)
128 ptr = sfree_alloc(ptr, sz, s.len, &sz);
131 memcpy(ptr, s.ptr, len);
135 string &string::operator=(const char *p)
144 int slen = strlen(p);
145 ptr = sfree_alloc(ptr, sz, slen, &sz);
153 string &string::operator=(char c)
155 ptr = sfree_alloc(ptr, sz, 1, &sz);
161 void string::move(string &s)
174 ptr = srealloc(ptr, sz, len, len + 1, &sz);
177 string &string::operator+=(const char *p)
181 int newlen = len + n;
183 ptr = srealloc(ptr, sz, len, newlen, &sz);
184 memcpy(ptr + len, p, n);
190 string &string::operator+=(const string &s)
193 int newlen = len + s.len;
195 ptr = srealloc(ptr, sz, len, newlen, &sz);
196 memcpy(ptr + len, s.ptr, s.len);
202 void string::append(const char *p, int n)
205 int newlen = len + n;
207 ptr = srealloc(ptr, sz, len, newlen, &sz);
208 memcpy(ptr + len, p, n);
213 string::string(const char *s1, int n1, const char *s2, int n2)
215 assert(n1 >= 0 && n2 >= 0);
222 ptr = salloc(len, &sz);
228 memcpy(ptr + n1, s2, n2);
233 int operator<=(const string &s1, const string &s2)
235 return (s1.len <= s2.len
236 ? s1.len == 0 || memcmp(s1.ptr, s2.ptr, s1.len) <= 0
237 : s2.len != 0 && memcmp(s1.ptr, s2.ptr, s2.len) < 0);
240 int operator<(const string &s1, const string &s2)
242 return (s1.len < s2.len
243 ? s1.len == 0 || memcmp(s1.ptr, s2.ptr, s1.len) <= 0
244 : s2.len != 0 && memcmp(s1.ptr, s2.ptr, s2.len) < 0);
247 int operator>=(const string &s1, const string &s2)
249 return (s1.len >= s2.len
250 ? s2.len == 0 || memcmp(s1.ptr, s2.ptr, s2.len) >= 0
251 : s1.len != 0 && memcmp(s1.ptr, s2.ptr, s1.len) > 0);
254 int operator>(const string &s1, const string &s2)
256 return (s1.len > s2.len
257 ? s2.len == 0 || memcmp(s1.ptr, s2.ptr, s2.len) >= 0
258 : s1.len != 0 && memcmp(s1.ptr, s2.ptr, s1.len) > 0);
261 void string::set_length(int i)
265 ptr = srealloc(ptr, sz, len, i, &sz);
274 int string::search(char c) const
276 char *p = ptr ? (char *)memchr(ptr, c, len) : NULL;
277 return p ? p - ptr : -1;
280 // we silently strip nuls
282 char *string::extract() const
288 for (i = 0; i < n; i++)
291 char *q =(char*)malloc(n + 1 - nnuls);
293 for (i = 0; i < n; i++)
300 void string::remove_spaces()
303 while (l >= 0 && ptr[l] == ' ')
314 char *tmp = new char[sz];
330 void put_string(const string &s, FILE *fp)
332 int len = s.length();
333 const char *ptr = s.contents();
334 for (int i = 0; i < len; i++)
338 string as_string(int i)
340 static char buf[INT_DIGITS + 2];
341 sprintf(buf, "%d", i);