File: StrBuf.h

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (443 lines) | stat: -rw-r--r-- 12,782 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#pragma once
#include "Object.h"
#include "TObject.h"
#include "Char.h"
#include "GcArray.h"

#include "Utils/Templates.h"

namespace storm {
	STORM_PKG(core);

	class Str;

	/**
	 * Format description for a StrBuf.
	 *
	 * Use the helper functions to create instances of this object.
	 *
	 * Interesting idea: Make it possible to mark a region inside a StrBuf (possiby consisting of
	 * multiple put-operations) and apply a format to that region as a whole.
	 */
	class StrFmt {
		STORM_VALUE;
	public:
		StrFmt();
		StrFmt(Nat width, Byte digits, Byte flags, Char fill);

		// Min width of the next output.
		Nat width;

		// Fill character.
		Char fill;

		// Flags. Text alignment and float mode.
		Byte flags;

		// Number of digits in float numbers.
		Byte digits;

		// Reset after outputting something. May contain values that affects the formatting.
		void reset();

		// Clear. Restores all properties to values that do not affect another StrFmt.
		void clear();

		// Merge with another StrFmt.
		void merge(const StrFmt &o);

		// Contants for flags.
		enum {
			alignNone = 0x00,
			alignLeft = 0x01,
			alignRight = 0x02,

			alignMask = 0x03, // 0000 0011

			floatNone = 0x00,
			floatSignificant = 0x04,
			floatFixed = 0x08,
			floatScientific = 0x0C,

			floatMask = 0x0C, // 0000 1100

			defaultFlags = alignNone | floatNone,
		};
	};

	// Set the width of the next item that is outputted. It is filled with the character set by
	// `fill` to match the size if it is not already wide enough.
	StrFmt STORM_FN width(Nat width);

	// Left align the next output.
	StrFmt STORM_FN left();

	// Left align the next output and specify a width.
	StrFmt STORM_FN left(Nat width);

	// Right align the next output.
	StrFmt STORM_FN right();

	// Right align the next output and specify a width.
	StrFmt STORM_FN right(Nat width);

	// Set the fill character used to pad output.
	StrFmt STORM_FN fill(Char fill);

	// Set precision of the floating point output without modifying the mode.
	StrFmt STORM_FN precision(Nat digits);

	// Output floating point numbers in decimal form with the specified number of significant
	// digits, at maximum. This is the default.
	StrFmt STORM_FN significant(Nat digits);

	// Output floating point numbers in decimal form with the specified number of decimal places.
	StrFmt STORM_FN fixed(Nat decimals);

	// Output floating point numbers in scientific notation with the specified number of decimal
	// digits.
	StrFmt STORM_FN scientific(Nat digits);

	/**
	 * Hex format for numbers. Use the helper functions below to create instances of this object.
	 */
	class HexFmt {
		STORM_VALUE;
	public:
		HexFmt(Word value, Nat digits);

		// Value.
		Word value;

		// # of digits.
		Nat digits;
	};

	// Create hex formats.
	HexFmt STORM_FN hex(Byte v);
	HexFmt STORM_FN hex(Byte v, Nat digits);
	HexFmt STORM_FN hex(Nat v);
	HexFmt STORM_FN hex(Nat v, Nat digits);
	HexFmt STORM_FN hex(Word v);
	HexFmt STORM_FN hex(Word v, Nat digits);
	HexFmt hex(const void *ptr);

	/**
	 * Mutable string buffer for constructing strings quickly and easily. Approximates the ostream
	 * of std to some extent wrt formatting options.
	 */
	class StrBuf : public Object {
		STORM_CLASS;
	public:
		// Create.
		STORM_CTOR StrBuf();
		StrBuf(const StrBuf &o);
		STORM_CTOR StrBuf(Str *from);

		// Deep copy.
		virtual void STORM_FN deepCopy(CloneEnv *env);

		// Get the string we built.
		virtual Str *STORM_FN toS() const;
		virtual void STORM_FN toS(StrBuf *buf) const;

		// C version.
		wchar *c_str() const;

		// Get an UTF-8 encoded c-string allocated on the GC heap.
		const char *utf8_str() const;

		// Append stuff (these are useful in situations where we can't use the name "<<", in grammars for example)
		// TODO: Rename to "push" to be consistent with Array etc.
		StrBuf *add(const wchar *str);
		StrBuf *addRaw(wchar str);
		StrBuf *STORM_FN add(const Str *str);
		StrBuf *STORM_FN add(const Object *obj);
		StrBuf *STORM_FN add(const TObject *obj);
		StrBuf *STORM_FN add(Bool b);
		StrBuf *STORM_FN add(Byte i);
		StrBuf *STORM_FN add(Int i);
		StrBuf *STORM_FN add(Nat i);
		StrBuf *STORM_FN add(Long i);
		StrBuf *STORM_FN add(Word i);
		StrBuf *STORM_FN add(Float f);
		StrBuf *STORM_FN add(Double d);
		StrBuf *STORM_FN add(Char c);
		StrBuf *STORM_FN add(HexFmt h);

		// Append stuff with the << operator.
		StrBuf &operator <<(const void *ptr);
		StrBuf &operator <<(const wchar *str) { return *add(str); }
		StrBuf &STORM_FN operator <<(const Str *str) { return *add(str); }
		StrBuf &STORM_FN operator <<(const Object *obj) { return *add(obj); }
		StrBuf &STORM_FN operator <<(const TObject *obj) { return *add(obj); }
		StrBuf &STORM_FN operator <<(Bool b) { return *add(b); }
		StrBuf &STORM_FN operator <<(Byte i) { return *add(i); }
		StrBuf &STORM_FN operator <<(Int i) { return *add(i); }
		StrBuf &STORM_FN operator <<(Nat i) { return *add(i); }
		StrBuf &STORM_FN operator <<(Long i) { return *add(i); }
		StrBuf &STORM_FN operator <<(Word i) { return *add(i); }
		StrBuf &STORM_FN operator <<(Float f) { return *add(f); }
		StrBuf &STORM_FN operator <<(Double d) { return *add(d); }
		StrBuf &STORM_FN operator <<(Char c) { return *add(c); }
		StrBuf &STORM_FN operator <<(HexFmt f) { return *add(f); }

#ifdef POSIX
		StrBuf &operator <<(const wchar_t *str) { return *add(str); }
		StrBuf *add(const wchar_t *str);
#endif

		// Formatting options.
		StrBuf &STORM_FN operator <<(StrFmt fmt);

		// Clear.
		void STORM_FN clear();

		// Empty?
		Bool STORM_FN empty() const;
		Bool STORM_FN any() const;

		// Control indentation.
		void STORM_FN indent();
		void STORM_FN dedent();

		// Indentation string.
		void STORM_FN indentBy(Str *str);

		// Get/set the current format.
		inline StrFmt STORM_FN format() { return fmt; }
		inline void STORM_ASSIGN format(StrFmt f) { fmt = f; }

	private:
		// Buffer. Always zero-terminated as memory is filled with zero from the start.
		GcArray<wchar> *buf;

		// Current position in 'buf'.
		Nat pos;

		// Indentation string.
		Str *indentStr;

		// Current indentation.
		Nat indentation;

		// Current format.
		StrFmt fmt;

		// Ensure capacity (excluding the null-terminator).
		void ensure(nat capacity);

		// Was the last character inserted a newline?
		bool lastNewline() const;

		// Insert indentation here if needed.
		void insertIndent();

		// Insert fill character if needed.
		void fill(Nat toOutput);

		// Insert fill character if needed, assuming we will reverse the string afterwards.
		void fillReverse(Nat toOutput);

		// Copy a buffer.
		GcArray<wchar> *copyBuf(GcArray<wchar> *buf) const;
	};


	/**
	 * Indent the StrBuf. Restores the indentation when it goes out of scope.
	 *
	 * Note: Maybe we do not need to expose this to Storm, as we can have other ways of doing the
	 * same thing there.
	 */
	class Indent {
		STORM_VALUE;
	public:
		STORM_CTOR Indent(StrBuf *buf);
		~Indent();

	private:
		StrBuf *buf;
	};


	/**
	 * Save the format in an StrBuf. Restores the format when it goes out of scope.
	 */
	class SaveFormat {
		STORM_VALUE;
	public:
		STORM_CTOR SaveFormat(StrBuf *buf);
		~SaveFormat();

	private:
		StrBuf *buf;
		StrFmt fmt;
	};


	/**
	 * Template magic for making it possible to output value types using a toS member, just like
	 * with class-types.
	 */
	namespace tos_impl {
		// Marker for success/failure. Size needs to be different.
		typedef int Success;
		typedef char Failure;

		// Helper to see if the value U matches the type U.
		template <class U, U> struct Check;

		// Function overloads, similar to those in ToSCall, that checks for the overloads we are after.
		// Priority is carefully selected so that only one is the best match. Need to be a separate
		// template to utilize SFINAE when extracting 'toS'.
		template <class U>
		Success r(Check<void (U::*)(StrBuf *) const, &U::toS> *, short); // exact match
		template <class U>
		Success r(Check<void (U::*)(StrBuf *), &U::toS> *, int); // integer promotion
		template <class U>
		Success r(Check<Str *(U::*)() const, &U::toS> *, float); // float promotion
		template <class U>
		Success r(Check<Str *(U::*)(), &U::toS> *, ...); // varargs
#ifdef CODECALL_OVERLOAD
		// In case CODECALL means something. Note: if this causes errors on GCC, then it should
		// probably not be defined in Utils/Platform.h for that combination.
		template <class U>
		Success r(Check<void (CODECALL U::*)(StrBuf *) const, &U::toS> *, short); // exact match
		template <class U>
		Success r(Check<void (CODECALL U::*)(StrBuf *), &U::toS> *, int); // integer promotion
		template <class U>
		Success r(Check<Str *(CODECALL U::*)() const, &U::toS> *, float); // float promotion
		template <class U>
		Success r(Check<Str *(CODECALL U::*)(), &U::toS> *, ...); // varargs
#endif
		template <class U>
		Failure r(...); // all varargs, last resort to avoid errors

		// Version that only accepts 'const' versions.
		template <class U>
		Success c(Check<void (U::*)(StrBuf *) const, &U::toS> *, short);
		template <class U>
		Success c(Check<Str *(U::*)() const, &U::toS> *, float);
#ifdef CODECALL_OVERLOAD
		template <class U>
		Success c(Check<void (CODECALL U::*)(StrBuf *) const, &U::toS> *, short);
		template <class U>
		Success c(Check<Str *(CODECALL U::*)() const, &U::toS> *, float);
#endif
		template <class U>
		Failure c(...); // all varargs, last resort to avoid errors

#if defined(VISUAL_STUDIO) && VISUAL_STUDIO < 2010
		// Helper to check for whether we have an overload for const or non-const versions: The
		// older MSVC compilers are a bit finnicky here. The exact version is a guess. I do not have
		// access to all of them to test.
		template <class T>
		struct HasToS {
			enum {
				regular = sizeof(r<T>(0, static_cast<short>(1))) == sizeof(Success),
				onlyConst = sizeof(c<T>(0, static_cast<short>(1))) == sizeof(Success),
			};
		};

#else

		// Interestingly, the "old" version fails for newer MSVC versions but works in GCC and
		// CLANG. It seems like the body is instantiated outside of a SFINAE context, so we see
		// errors from toS() being protected, etc. from that.
		template <class T,
				  size_t regularSize = sizeof(r<T>(0, static_cast<short>(1))),
				  size_t constSize = sizeof(c<T>(0, static_cast<short>(1)))>
		struct HasToS {
			enum {
				regular = regularSize == sizeof(Success),
				onlyConst = constSize == sizeof(Success),
			};
		};

#endif

		// Functions that actually call the implementation. Works like above:
		template <class T>
		void callRegularI(StrBuf *to, T &value, void (T::*)(StrBuf *) const, short) {
			value.toS(to);
		}
		template <class T>
		void callRegularI(StrBuf *to, T &value, void (T::*)(StrBuf *), int) {
			value.toS(to);
		}
		template <class T>
		void callRegularI(StrBuf *to, T &value, Str *(T::*)() const, float) {
			*to << value.toS();
		}
		template <class T>
		void callRegularI(StrBuf *to, T &value, Str *(T::*)(), ...) {
			*to << value.toS();
		}
#ifdef CODECALL_OVERLOAD
		template <class T>
		void callRegularI(StrBuf *to, T &value, void (CODECALL T::*)(StrBuf *) const, short) {
			value.toS(to);
		}
		template <class T>
		void callRegularI(StrBuf *to, T &value, void (CODECALL T::*)(StrBuf *), int) {
			value.toS(to);
		}
		template <class T>
		void callRegularI(StrBuf *to, T &value, Str *(CODECALL T::*)() const, float) {
			*to << value.toS();
		}
		template <class T>
		void callRegularI(StrBuf *to, T &value, Str *(CODECALL T::*)(), ...) {
			*to << value.toS();
		}
#endif

		// Version for const:
		template <class T>
		void callConstI(StrBuf *to, const T &value, void (T::*)(StrBuf *) const, short) {
			value.toS(to);
		}
		template <class T>
		void callConstI(StrBuf *to, const T &value, Str *(T::*)() const, float) {
			*to << value.toS();
		}
#ifdef CODECALL_OVERLOAD
		template <class T>
		void callConstI(StrBuf *to, const T &value, void (CODECALL T::*)(StrBuf *) const, short) {
			value.toS(to);
		}
		template <class T>
		void callConstI(StrBuf *to, const T &value, Str *(CODECALL T::*)() const, float) {
			*to << value.toS();
		}
#endif

		// Entry-point:
		template <class T>
		void callRegular(StrBuf *to, T &value) {
			callRegularI(to, value, &T::toS, static_cast<short>(1));
		}
		template <class T>
		void callConst(StrBuf *to, const T &value) {
			callConstI(to, value, &T::toS, static_cast<short>(1));
		}
	}

	// Operator <<, for non-const variants:
	template <class T>
	typename EnableIf<tos_impl::HasToS<T>::regular, StrBuf &>::t
	operator <<(StrBuf &to, T &value) {
		tos_impl::callRegular(&to, value);
		return to;
	}

	// Operator <<, for const variants:
	template <class T>
	typename EnableIf<tos_impl::HasToS<T>::onlyConst, StrBuf &>::t
	operator <<(StrBuf &to, const T &value) {
		tos_impl::callConst(&to, value);
		return to;
	}

}