-
Notifications
You must be signed in to change notification settings - Fork 1
/
string_examples.cpp
75 lines (62 loc) · 1.39 KB
/
string_examples.cpp
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
/*
* this file demonstrates integrating ggformat with a string class
*
* compile me with "cl.exe string_examples.cpp ggformat.cpp"
* or "g++ -std=c++11 string_examples.cpp ggformat.cpp"
*/
#include "ggformat.h"
template< typename T >
T min( T a, T b ) {
return a < b ? a : b;
}
template< size_t N >
class str {
public:
str() {
clear();
}
template< typename... Rest >
str( const char * fmt, const Rest & ... rest ) {
sprintf( fmt, rest... );
}
void clear() {
buf[ 0 ] = '\0';
length = 0;
}
template< typename T >
void operator+=( const T & x ) {
appendf( "{}", x );
}
template< typename... Rest >
void sprintf( const char * fmt, const Rest & ... rest ) {
size_t copied = ggformat( buf, N, fmt, rest... );
length = min( copied, N - 1 );
}
template< typename... Rest >
void appendf( const char * fmt, const Rest & ... rest ) {
size_t copied = ggformat( buf + length, N - length, fmt, rest... );
length += min( copied, N - length - 1 );
}
const char * c_str() const {
return buf;
}
private:
char buf[ N ];
size_t length;
};
template< size_t N >
void format( FormatBuffer * fb, const str< N > & buf, const FormatOpts & opts ) {
format( fb, buf.c_str(), opts );
}
int main() {
str< 256 > a( "hello {-10}:", "world" );
a += " ";
a += 1;
a += " ";
a += 1.2345;
a += " ";
a += false;
a.appendf( ". {} w{}rld", "goodbye", 0 );
ggprint( "{}\n", a );
return 0;
}