-
Notifications
You must be signed in to change notification settings - Fork 46
/
format.hpp
683 lines (578 loc) · 22.3 KB
/
format.hpp
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
#ifndef _C4_FORMAT_HPP_
#define _C4_FORMAT_HPP_
/** @file format.hpp provides type-safe facilities for formatting arguments
* to string buffers */
#include "c4/charconv.hpp"
#include "c4/blob.hpp"
#include <memory>
#include <functional>
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable: 4800) // forcing value to bool 'true' or 'false' (performance warning)
# pragma warning(disable: 4996) // snprintf/scanf: this function or variable may be unsafe
#endif
namespace c4 {
/** @defgroup formatting_functions Formatting multiple values<->string
* @brief Convert a sequence of values to/from a string.
* @ingroup formatting
*/
/** C-style printing into a buffer
* @ingroup formatting_functions */
size_t sprintf(substr buf, const char * fmt, ...);
//size_t sscanf(csubstr buf, const char *fmt, ...);
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/** @addtogroup generic_tofrom_chars
* @{ */
namespace fmt {
/** a generic class for providing type-specific formatting settings */
template<class T>
struct fmt_wrapper;
/** mark a variable to be written in its default custom format wrapper */
template<class T, class... Args>
inline fmt_wrapper<T> fmt(T &v, Args && ...args)
{
return fmt_wrapper<T>(std::ref(v), std::forward<Args>(args)...);
}
} // namespace fmt
/** @} */
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// formatting integral types
/** @addtogroup generic_tofrom_chars
* @{ */
namespace fmt {
/** format an integral type */
template<class T>
struct integral
{
T val;
T radix;
integral(T val_, uint8_t radix_=10) : val(val_), radix(radix_) {}
};
template<> struct fmt_wrapper< int8_t> : public integral< int8_t> { using integral< int8_t>::integral; };
template<> struct fmt_wrapper< int16_t> : public integral< int16_t> { using integral< int16_t>::integral; };
template<> struct fmt_wrapper< int32_t> : public integral< int32_t> { using integral< int32_t>::integral; };
template<> struct fmt_wrapper< int64_t> : public integral< int64_t> { using integral< int64_t>::integral; };
template<> struct fmt_wrapper< uint8_t> : public integral< uint8_t> { using integral< uint8_t>::integral; };
template<> struct fmt_wrapper<uint16_t> : public integral<uint16_t> { using integral<uint16_t>::integral; };
template<> struct fmt_wrapper<uint32_t> : public integral<uint32_t> { using integral<uint32_t>::integral; };
template<> struct fmt_wrapper<uint64_t> : public integral<uint64_t> { using integral<uint64_t>::integral; };
/** format the integral argument as a hex value */
template<class T>
inline fmt_wrapper<T> hex(T v)
{
static_assert(std::is_integral<T>::value, "must be an integral type");
return fmt_wrapper<T>(v, T(16));
}
/** format the integral argument as an octal value */
template<class T>
inline fmt_wrapper<T> oct(T v)
{
static_assert(std::is_integral<T>::value, "must be an integral type");
return fmt_wrapper<T>(v, T(8));
}
/** format the integral argument as a binary 0-1 value */
template<class T>
inline fmt_wrapper<T> bin(T v)
{
static_assert(std::is_integral<T>::value, "must be an integral type");
return fmt_wrapper<T>(v, T(2));
}
} // namespace fmt
inline size_t to_chars(substr buf, fmt::fmt_wrapper< int8_t> fmt) { return itoa(buf, fmt.val, fmt.radix); }
inline size_t to_chars(substr buf, fmt::fmt_wrapper<int16_t> fmt) { return itoa(buf, fmt.val, fmt.radix); }
inline size_t to_chars(substr buf, fmt::fmt_wrapper<int32_t> fmt) { return itoa(buf, fmt.val, fmt.radix); }
inline size_t to_chars(substr buf, fmt::fmt_wrapper<int64_t> fmt) { return itoa(buf, fmt.val, fmt.radix); }
inline size_t to_chars(substr buf, fmt::fmt_wrapper< uint8_t> fmt) { return utoa(buf, fmt.val, fmt.radix); }
inline size_t to_chars(substr buf, fmt::fmt_wrapper<uint16_t> fmt) { return utoa(buf, fmt.val, fmt.radix); }
inline size_t to_chars(substr buf, fmt::fmt_wrapper<uint32_t> fmt) { return utoa(buf, fmt.val, fmt.radix); }
inline size_t to_chars(substr buf, fmt::fmt_wrapper<uint64_t> fmt) { return utoa(buf, fmt.val, fmt.radix); }
/** @} */
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// formatting real types
/** @addtogroup generic_tofrom_chars
* @{ */
namespace fmt {
template<class T>
struct real
{
T val;
int precision;
RealFormat_e fmt;
real(T v, int prec=-1, RealFormat_e f=FTOA_FLOAT) : val(v), precision(prec), fmt(f) {}
};
template<> struct fmt_wrapper< float> : public real< float> { using real< float>::real; };
template<> struct fmt_wrapper<double> : public real<double> { using real<double>::real; };
} // namespace fmt
inline size_t to_chars(substr buf, fmt::fmt_wrapper< float> fmt) { return ftoa(buf, fmt.val, fmt.precision, fmt.fmt); }
inline size_t to_chars(substr buf, fmt::fmt_wrapper<double> fmt) { return dtoa(buf, fmt.val, fmt.precision, fmt.fmt); }
/** @} */
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// writing raw binary data
/** @addtogroup generic_tofrom_chars
* @{ */
namespace fmt {
/** @see blob_ */
template<class T>
struct raw_wrapper_ : public blob_<T>
{
size_t alignment;
C4_ALWAYS_INLINE raw_wrapper_(blob_<T> data, size_t alignment_) noexcept
:
blob_<T>(data),
alignment(alignment_)
{
C4_ASSERT_MSG(alignment > 0 && (alignment & (alignment - 1)) == 0, "alignment must be a power of two");
}
};
using const_raw_wrapper = raw_wrapper_<cbyte>;
using raw_wrapper = raw_wrapper_<byte>;
/** mark a variable to be written in raw binary format
* @see blob_ */
inline const_raw_wrapper craw(cblob data, size_t alignment=alignof(max_align_t))
{
return const_raw_wrapper(data, alignment);
}
/** mark a variable to be read in raw binary format
* @see blob_ */
inline raw_wrapper raw(blob data, size_t alignment=alignof(max_align_t))
{
return raw_wrapper(data, alignment);
}
} // namespace fmt
/** write a variable in raw binary format */
template<class T>
inline size_t to_chars(substr buf, fmt::const_raw_wrapper r)
{
void * vptr = buf.str;
size_t space = buf.len;
auto ptr = (decltype(buf.str)) std::align(r.alignment, r.len, vptr, space);
if(ptr == nullptr) return r.alignment + r.len;
C4_CHECK(ptr >= r.buf && ptr <= r.buf + r.len);
size_t dim = (ptr - buf.str) + r.len;
if(dim <= buf.len) memcpy(ptr, r.buf, r.len);
return dim;
}
/** read a variable in raw binary format */
template<class T>
inline size_t from_chars(csubstr buf, fmt::raw_wrapper *r)
{
void * vptr = (void*)buf.str;
size_t space = buf.len;
auto ptr = (decltype(buf.str)) std::align(r->alignment, r->len, vptr, space);
C4_CHECK(ptr != nullptr);
C4_CHECK(ptr >= r->buf && ptr <= r->buf + r->len);
//size_t dim = (ptr - buf.str) + r->len;
memcpy(r->buf, ptr, r->len);
return r->len;
}
/** @} */
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/** terminates the variadic recursion
* @ingroup formatting_functions */
inline size_t cat(substr /*buf*/)
{
return 0;
}
/** serialize the arguments, concatenating them to the given fixed-size buffer.
* The buffer size is strictly respected: no writes will occur beyond its end.
* @return the number of characters needed to write all the arguments into the buffer.
* @see catrs() if instead of a fixed-size buffer, a resizeable container is desired
* @see uncat() for the inverse function
* @see catsep() if a separator between each argument is to be used
* @see format() if a format string is desired
* @ingroup formatting_functions */
template<class Arg, class... Args>
size_t cat(substr buf, Arg const& C4_RESTRICT a, Args const& C4_RESTRICT ...more)
{
size_t num = to_chars(buf, a);
buf = buf.len >= num ? buf.sub(num) : substr{};
num += cat(buf, more...);
return num;
}
/** like cat but return a substr instead of a size, checking
* @ingroup formatting_functions */
template<class... Args>
substr cat_sub(substr buf, Args && ...args)
{
size_t sz = cat(buf, std::forward<Args>(args)...);
C4_CHECK(sz <= buf.len);
return {buf.str, sz <= buf.len ? sz : buf.len};
}
//-----------------------------------------------------------------------------
/** terminates the variadic recursion
* @ingroup formatting_functions */
inline size_t uncat(csubstr /*buf*/)
{
return 0;
}
/** deserialize the arguments from the given buffer.
*
* @return the number of characters read from the buffer, or csubstr::npos
* if a conversion was not successful.
* @see cat(). uncat() is the inverse of cat().
* @ingroup formatting_functions */
template<class Arg, class... Args>
size_t uncat(csubstr buf, Arg & C4_RESTRICT a, Args & C4_RESTRICT ...more)
{
size_t out = from_chars_first(buf, &a);
if(C4_UNLIKELY(out == csubstr::npos)) return csubstr::npos;
buf = buf.len >= out ? buf.sub(out) : substr{};
size_t num = uncat(buf, more...);
if(C4_UNLIKELY(num == csubstr::npos)) return csubstr::npos;
return out + num;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
namespace detail {
template<class Sep>
inline size_t catsep_more(substr /*buf*/, Sep const& C4_RESTRICT /*sep*/)
{
return 0;
}
template<class Sep, class Arg, class... Args>
size_t catsep_more(substr buf, Sep const& C4_RESTRICT sep, Arg const& C4_RESTRICT a, Args const& C4_RESTRICT ...more)
{
size_t ret = to_chars(buf, sep), num = ret;
buf = buf.len >= ret ? buf.sub(ret) : substr{};
ret = to_chars(buf, a);
num += ret;
buf = buf.len >= ret ? buf.sub(ret) : substr{};
ret = catsep_more(buf, sep, more...);
num += ret;
return num;
}
template<class Sep>
inline size_t uncatsep_more(csubstr /*buf*/, Sep & /*sep*/)
{
return 0;
}
template<class Sep, class Arg, class... Args>
size_t uncatsep_more(csubstr buf, Sep & C4_RESTRICT sep, Arg & C4_RESTRICT a, Args & C4_RESTRICT ...more)
{
size_t ret = from_chars_first(buf, &sep), num = ret;
if(C4_UNLIKELY(ret == csubstr::npos)) return csubstr::npos;
buf = buf.len >= ret ? buf.sub(ret) : substr{};
ret = from_chars_first(buf, &a);
if(C4_UNLIKELY(ret == csubstr::npos)) return csubstr::npos;
num += ret;
buf = buf.len >= ret ? buf.sub(ret) : substr{};
ret = uncatsep_more(buf, sep, more...);
if(C4_UNLIKELY(ret == csubstr::npos)) return csubstr::npos;
num += ret;
return num;
}
} // namespace detail
/** serialize the arguments, concatenating them to the given fixed-size
* buffer, using a separator between each argument.
* The buffer size is strictly respected: no writes will occur beyond its end.
* @return the number of characters needed to write all the arguments into the buffer.
* @see catseprs() if instead of a fixed-size buffer, a resizeable container is desired
* @see uncatsep() for the inverse function
* @see cat() if no separator is needed
* @see format() if a format string is desired
* @ingroup formatting_functions */
template<class Sep, class Arg, class... Args>
size_t catsep(substr buf, Sep const& C4_RESTRICT sep, Arg const& C4_RESTRICT a, Args const& C4_RESTRICT ...more)
{
size_t num = to_chars(buf, a);
buf = buf.len >= num ? buf.sub(num) : substr{};
num += detail::catsep_more(buf, sep, more...);
return num;
}
/** like catsep but return a substr instead of a size
* @see catsep(). uncatsep() is the inverse of catsep().
* @ingroup formatting_functions */
template<class... Args>
substr catsep_sub(substr buf, Args && ...args)
{
size_t sz = catsep(buf, std::forward<Args>(args)...);
C4_CHECK(sz <= buf.len);
return {buf.str, sz <= buf.len ? sz : buf.len};
}
/** deserialize the arguments from the given buffer, using a separator.
*
* @return the number of characters read from the buffer, or csubstr::npos
* if a conversion was not successful
* @see catsep(). uncatsep() is the inverse of catsep().
* @ingroup formatting_functions */
template<class Sep, class Arg, class... Args>
size_t uncatsep(csubstr buf, Sep & C4_RESTRICT sep, Arg & C4_RESTRICT a, Args & C4_RESTRICT ...more)
{
size_t ret = from_chars_first(buf, &a), num = ret;
if(C4_UNLIKELY(ret == csubstr::npos)) return csubstr::npos;
buf = buf.len >= ret ? buf.sub(ret) : substr{};
ret = detail::uncatsep_more(buf, sep, more...);
if(C4_UNLIKELY(ret == csubstr::npos)) return csubstr::npos;
num += ret;
return num;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/** terminates the variadic recursion
* @ingroup formatting_functions */
inline size_t format(substr buf, csubstr fmt)
{
return to_chars(buf, fmt);
}
/** using a format string, serialize the arguments into the given
* fixed-size buffer.
* The buffer size is strictly respected: no writes will occur beyond its end.
* In the format string, each argument is marked with a compact
* curly-bracket pair: {}. Arguments beyond the last curly bracket pair
* are silently ignored. For example:
* @code{.cpp}
* c4::format(buf, "the {} drank {} {}", "partier", 5, "beers"); // the partier drank 5 beers
* c4::format(buf, "the {} drank {} {}", "programmer", 6, "coffees"); // the programmer drank 6 coffees
* @endcode
* @return the number of characters needed to write into the buffer.
* @see formatrs() if instead of a fixed-size buffer, a resizeable container is desired
* @see unformat() for the inverse function
* @see cat() if no format or separator is needed
* @see catsep() if no format is needed, but a separator must be used
* @ingroup formatting_functions */
template<class Arg, class... Args>
size_t format(substr buf, csubstr fmt, Arg const& C4_RESTRICT a, Args const& C4_RESTRICT ...more)
{
auto pos = fmt.find("{}");
if(C4_UNLIKELY(pos == csubstr::npos))
{
return format(buf, fmt);
}
size_t num = to_chars(buf, fmt.sub(0, pos));
size_t out = num;
buf = buf.len >= num ? buf.sub(num) : substr{};
num = to_chars(buf, a);
out += num;
buf = buf.len >= num ? buf.sub(num) : substr{};
num = format(buf, fmt.sub(pos + 2), more...);
out += num;
return out;
}
/** like format but return a substr instead of a size
* @see format()
* @see catsep(). uncatsep() is the inverse of catsep().
* @ingroup formatting_functions */
template<class... Args>
substr format_sub(substr buf, csubstr fmt, Args && ...args)
{
size_t sz = c4::format(buf, fmt, std::forward<Args>(args)...);
C4_CHECK(sz <= buf.len);
return {buf.str, sz <= buf.len ? sz : buf.len};
}
//-----------------------------------------------------------------------------
/** terminates the variadic recursion
* @ingroup formatting_functions */
inline size_t unformat(csubstr /*buf*/, csubstr /*fmt*/)
{
return 0;
}
/** using a format string, deserialize the arguments from the given
* buffer.
* @return the number of characters read from the buffer, or npos if a conversion failed.
* @see format() this is the inverse function to format().
* @ingroup formatting_functions */
template<class Arg, class... Args>
size_t unformat(csubstr buf, csubstr fmt, Arg & C4_RESTRICT a, Args & C4_RESTRICT ...more)
{
size_t pos = fmt.find("{}");
if(C4_UNLIKELY(pos == csubstr::npos))
{
return unformat(buf, fmt);
}
size_t num = pos;
size_t out = num;
buf = buf.len >= num ? buf.sub(num) : substr{};
num = from_chars_first(buf, &a);
if(C4_UNLIKELY(num == csubstr::npos)) return csubstr::npos;
out += num;
buf = buf.len >= num ? buf.sub(num) : substr{};
num = unformat(buf, fmt.sub(pos + 2), more...);
if(C4_UNLIKELY(num == csubstr::npos)) return csubstr::npos;
out += num;
return out;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/** a tag type
* @see catrs
* @ingroup formatting_functions
* */
struct append_t {};
/** a tag variable
* @see catrs
* @ingroup formatting_functions
* */
constexpr const append_t append = {};
//-----------------------------------------------------------------------------
/** like cat(), but receives a container, and resizes it as needed to contain
* the result. The container is overwritten. To append to it, use the append
* overload.
*
* @see cat()
* @ingroup formatting_functions */
template<class CharOwningContainer, class... Args>
inline void catrs(CharOwningContainer * C4_RESTRICT cont, Args const& C4_RESTRICT ...args)
{
substr buf = to_substr(*cont);
size_t ret = cat(buf, args...);
cont->resize(ret);
if(ret > buf.len)
{
buf = to_substr(*cont);
ret = cat(buf, args...);
if(ret != buf.len)
{
cont->resize(ret);
}
}
}
/** like cat(), but receives a container, and appends to it instead of
* overwriting it. The container is resized as needed to contain the result.
*
* @return the region newly appended to the original container
* @see cat()
* @see catrs()
* @ingroup formatting_functions */
template<class CharOwningContainer, class... Args>
inline csubstr catrs(append_t, CharOwningContainer * C4_RESTRICT cont, Args const& C4_RESTRICT ...args)
{
const size_t pos = cont->size();
substr buf = to_substr(*cont).sub(pos);
size_t ret = cat(buf, args...);
cont->resize(pos + ret);
if(ret > buf.len)
{
buf = to_substr(*cont).sub(pos);
ret = cat(buf, args...);
if(ret != buf.len)
{
cont->resize(pos + ret);
}
}
return to_csubstr(*cont).range(pos, cont->size());
}
//-----------------------------------------------------------------------------
/** like catsep(), but receives a container, and resizes it as needed to contain the result.
* The container is overwritten. To append to the container use the append overload.
* @see catsep()
* @ingroup formatting_functions */
template<class CharOwningContainer, class Sep, class... Args>
inline void catseprs(CharOwningContainer * C4_RESTRICT cont, Sep const& C4_RESTRICT sep, Args const& C4_RESTRICT ...args)
{
substr buf = to_substr(*cont);
size_t ret = catsep(buf, sep, args...);
cont->resize(ret);
if(ret > buf.len)
{
buf = to_substr(*cont);
ret = catsep(buf, sep, args...);
if(ret != buf.len)
{
cont->resize(ret);
}
}
}
/**
* @overload catseprs
* @ingroup formatting_functions */
template<class CharOwningContainer, class Sep, class... Args>
inline CharOwningContainer catseprs(Sep const& C4_RESTRICT sep, Args const& C4_RESTRICT ...args)
{
CharOwningContainer cont;
catseprs(&cont, std::cref(sep), std::forward<Args>(args)...);
return cont;
}
/** like catsep(), but receives a container, and appends the arguments, resizing the
* container as needed to contain the result. The buffer is appended to.
* @ingroup formatting_functions */
template<class CharOwningContainer, class Sep, class... Args>
inline csubstr catseprs(append_t, CharOwningContainer * C4_RESTRICT cont, Sep const& C4_RESTRICT sep, Args const& C4_RESTRICT ...args)
{
const size_t pos = cont->size();
substr buf = to_substr(*cont).sub(pos);
size_t ret = catsep(buf, sep, args...);
cont->resize(pos + ret);
if(ret > buf.len)
{
buf = to_substr(*cont).sub(pos);
ret = catsep(buf, sep, args...);
if(ret != buf.len)
{
cont->resize(pos + ret);
}
}
return to_csubstr(*cont).range(pos, cont->size());
}
//-----------------------------------------------------------------------------
/** like format(), but receives a container, and resizes it as needed
* to contain the result. The container is overwritten. To append to
* the container use the append overload.
* @see format()
* @ingroup formatting_functions */
template<class CharOwningContainer, class... Args>
inline void formatrs(CharOwningContainer * C4_RESTRICT cont, csubstr fmt, Args const& C4_RESTRICT ...args)
{
substr buf = to_substr(*cont);
size_t ret = format(buf, fmt, args...);
cont->resize(ret);
if(ret > buf.len)
{
buf = to_substr(*cont);
ret = format(buf, fmt, args...);
if(ret != buf.len)
{
cont->resize(ret);
}
}
}
/**
* @overload formatrs
* @ingroup formatting_functions */
template<class CharOwningContainer, class... Args>
inline CharOwningContainer formatrs(csubstr fmt, Args const& C4_RESTRICT ...args)
{
CharOwningContainer cont;
formatrs(&cont, fmt, std::forward<Args>(args)...);
return cont;
}
/** like format(), but receives a container, and appends the
* arguments, resizing the container as needed to contain the
* result. The buffer is appended to.
* @return the region newly appended to the original container
* @ingroup formatting_functions */
template<class CharOwningContainer, class... Args>
inline csubstr formatrs(append_t, CharOwningContainer * C4_RESTRICT cont, csubstr fmt, Args const& C4_RESTRICT ...args)
{
const size_t pos = cont->size();
substr buf = to_substr(*cont).sub(pos);
size_t ret = format(buf, fmt, args...);
cont->resize(pos + ret);
if(ret > buf.len)
{
buf = to_substr(*cont).sub(pos);
ret = format(buf, fmt, args...);
if(ret != buf.len)
{
cont->resize(pos + ret);
}
}
return to_csubstr(*cont).range(pos, cont->size());
}
} // namespace c4
#ifdef _MSC_VER
# pragma warning(pop)
#endif
#endif /* _C4_FORMAT_HPP_ */