Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/liblcdf/straccum.cc

raw
1 // -*- related-file-name: "../include/lcdf/straccum.hh" -*-
2 /*
3 * straccum.{cc,hh} -- build up strings with operator<<
4 * Eddie Kohler
5 *
6 * Copyright (c) 1999-2000 Massachusetts Institute of Technology
7 * Copyright (c) 2001-2023 Eddie Kohler
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, subject to the conditions
12 * listed in the Click LICENSE file. These conditions include: you must
13 * preserve this copyright notice, and you cannot mention the copyright
14 * holders in advertising related to the Software without their permission.
15 * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
16 * notice is a summary of the Click LICENSE file; the license in that file is
17 * legally binding.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23 #include <lcdf/straccum.hh>
24 #include <lcdf/vector.hh>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <errno.h>
29
30 /** @class StringAccum
31 * @brief Efficiently build up Strings from pieces.
32 *
33 * Like the String class, StringAccum represents a string of characters.
34 * However, unlike a String, a StringAccum is inherently mutable, and
35 * efficiently supports building up a large string from many smaller pieces.
36 *
37 * StringAccum objects support operator<<() operations for most fundamental
38 * data types. A StringAccum is generally built up by operator<<(), and then
39 * turned into a String by the take_string() method. Extracting the String
40 * from a StringAccum does no memory allocation or copying; the StringAccum's
41 * memory is donated to the String.
42 *
43 * <h3>Out-of-memory StringAccums</h3>
44 *
45 * When there is not enough memory to add requested characters to a
46 * StringAccum object, the object becomes a special "out-of-memory"
47 * StringAccum. Out-of-memory objects are contagious: the result of any
48 * concatenation operation involving an out-of-memory StringAccum is another
49 * out-of-memory StringAccum. Appending an out-of-memory String with "sa <<
50 * s" makes "sa" out-of-memory. (However, other usually-equivalent calls,
51 * such as "sa.append(s.begin(), s.end())", <em>do not</em> make "sa"
52 * out-of-memory.) Calling take_string() on an out-of-memory StringAccum
53 * returns an out-of-memory String.
54 *
55 * Out-of-memory StringAccum objects have length zero.
56 */
57
58 const char* StringAccum::double_format = "%.12g";
59
60 void
61 StringAccum::assign_out_of_memory()
62 {
63 assert(_cap >= 0);
64 if (_cap > 0)
65 delete[] (_s - MEMO_SPACE);
66 _s = reinterpret_cast<unsigned char*>(const_cast<char*>(String::out_of_memory_data()));
67 _cap = -1;
68 _len = 0;
69 }
70
71 char*
72 StringAccum::grow(int want)
73 {
74 // can't append to out-of-memory strings
75 if (_cap < 0) {
76 errno = ENOMEM;
77 return 0;
78 }
79
80 int ncap = (_cap ? (_cap + MEMO_SPACE) * 2 : 128) - MEMO_SPACE;
81 while (ncap <= want)
82 ncap = (ncap + MEMO_SPACE) * 2 - MEMO_SPACE;
83
84 unsigned char* n = new unsigned char[ncap + MEMO_SPACE];
85 if (!n) {
86 assign_out_of_memory();
87 errno = ENOMEM;
88 return 0;
89 }
90 n += MEMO_SPACE;
91
92 if (_s) {
93 memcpy(n, _s, _len);
94 delete[] (_s - MEMO_SPACE);
95 }
96 _s = n;
97 _cap = ncap;
98 return reinterpret_cast<char*>(_s + _len);
99 }
100
101 int
102 StringAccum::resize(int len)
103 {
104 assert(len >= 0);
105 if (len > _cap && !grow(len))
106 return -ENOMEM;
107 else {
108 _len = len;
109 return 0;
110 }
111 }
112
113 char*
114 StringAccum::hard_extend(int nadjust, int nreserve)
115 {
116 char* x = grow(_len + nadjust + nreserve);
117 if (x)
118 _len += nadjust;
119 return x;
120 }
121
122 const char*
123 StringAccum::c_str()
124 {
125 if (_len < _cap || grow(_len))
126 _s[_len] = '\0';
127 return reinterpret_cast<char*>(_s);
128 }
129
130 void
131 StringAccum::append_fill(int c, int len)
132 {
133 if (char* s = extend(len))
134 memset(s, c, len);
135 }
136
137 void
138 StringAccum::append_utf8_hard(unsigned ch)
139 {
140 if (ch < 0x80)
141 append((unsigned char) ch);
142 else if (ch < 0x800) {
143 append((unsigned char) (0xC0 + ((ch >> 6) & 0x1F)));
144 append((unsigned char) (0x80 + (ch & 0x3F)));
145 } else if (ch < 0x10000) {
146 append((unsigned char) (0xE0 + ((ch >> 12) & 0x0F)));
147 append((unsigned char) (0x80 + ((ch >> 6) & 0x3F)));
148 append((unsigned char) (0x80 + (ch & 0x3F)));
149 } else if (ch < 0x110000) {
150 append((unsigned char) (0xF0 + ((ch >> 18) & 0x07)));
151 append((unsigned char) (0x80 + ((ch >> 12) & 0x3F)));
152 append((unsigned char) (0x80 + ((ch >> 6) & 0x3F)));
153 append((unsigned char) (0x80 + (ch & 0x3F)));
154 } else
155 append((unsigned char) '?');
156 }
157
158 void
159 StringAccum::hard_append(const char* s, int len)
160 {
161 // We must be careful about calls like "sa.append(sa.begin(), sa.end())";
162 // a naive implementation might use sa's data after freeing it.
163 const char* my_s = reinterpret_cast<char*>(_s);
164
165 if (len <= 0) {
166 // do nothing
167 } else if (_len + len <= _cap) {
168 success:
169 memcpy(_s + _len, s, len);
170 _len += len;
171 } else if (s < my_s || s >= my_s + _cap) {
172 if (grow(_len + len))
173 goto success;
174 } else {
175 unsigned char* old_s = _s;
176 int old_len = _len;
177
178 _s = 0;
179 _len = 0;
180 _cap = 0;
181
182 if (char* new_s = extend(old_len + len)) {
183 memcpy(new_s, old_s, old_len);
184 memcpy(new_s + old_len, s, len);
185 }
186
187 delete[] (old_s - MEMO_SPACE);
188 }
189 }
190
191 void
192 StringAccum::append(const char* s)
193 {
194 hard_append(s, strlen(s));
195 }
196
197 String
198 StringAccum::take_string()
199 {
200 int len = length();
201 int cap = _cap;
202 char* str = reinterpret_cast<char*>(_s);
203 if (len > 0) {
204 _s = 0;
205 _len = _cap = 0;
206 return String::make_claim(str, len, cap);
207 } else if (!out_of_memory())
208 return String();
209 else {
210 clear();
211 return String::make_out_of_memory();
212 }
213 }
214
215 void
216 StringAccum::swap(StringAccum& o)
217 {
218 unsigned char* os = o._s;
219 int olen = o._len, ocap = o._cap;
220 o._s = _s;
221 o._len = _len, o._cap = _cap;
222 _s = os;
223 _len = olen, _cap = ocap;
224 }
225
226 /** @relates StringAccum
227 @brief Append decimal representation of @a i to @a sa.
228 @return @a sa */
229 StringAccum&
230 operator<<(StringAccum& sa, long i)
231 {
232 if (char* x = sa.reserve(24)) {
233 int len = snprintf(x, 24, "%ld", i);
234 sa.adjust_length(len);
235 }
236 return sa;
237 }
238
239 /** @relates StringAccum
240 @brief Append decimal representation of @a u to @a sa.
241 @return @a sa */
242 StringAccum&
243 operator<<(StringAccum& sa, unsigned long u)
244 {
245 if (char* x = sa.reserve(24)) {
246 int len = snprintf(x, 24, "%lu", u);
247 sa.adjust_length(len);
248 }
249 return sa;
250 }
251
252 StringAccum&
253 operator<<(StringAccum& sa, double d)
254 {
255 if (char* x = sa.reserve(256)) {
256 int len = snprintf(x, 256, StringAccum::double_format, d);
257 sa.adjust_length(len);
258 }
259 return sa;
260 }
261
262 StringAccum&
263 StringAccum::snprintf(int n, const char* format, ...)
264 {
265 va_list val;
266 va_start(val, format);
267 if (char* x = reserve(n + 1)) {
268 #if HAVE_VSNPRINTF
269 int len = vsnprintf(x, n + 1, format, val);
270 #else
271 int len = vsprintf(x, format, val);
272 assert(len <= n);
273 #endif
274 adjust_length(len);
275 }
276 va_end(val);
277 return *this;
278 }
279
280 void
281 StringAccum::append_break_lines(const String& text, int linelen, const String& leftmargin)
282 {
283 if (text.length() == 0)
284 return;
285 const char* line = text.begin();
286 const char* ends = text.end();
287 linelen -= leftmargin.length();
288 for (const char* s = line; s < ends; s++) {
289 const char* start = s;
290 while (s < ends && isspace((unsigned char) *s))
291 s++;
292 const char* word = s;
293 while (s < ends && !isspace((unsigned char) *s))
294 s++;
295 if (s - line > linelen && start > line) {
296 *this << leftmargin;
297 append(line, start - line);
298 *this << '\n';
299 line = word;
300 }
301 }
302 if (line < text.end()) {
303 *this << leftmargin;
304 append(line, text.end() - line);
305 *this << '\n';
306 }
307 }