-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstring_stream.cpp
49 lines (40 loc) · 947 Bytes
/
string_stream.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
#include "string_stream.h"
#include <stdarg.h>
namespace foundation
{
namespace string_stream
{
Buffer & printf(Buffer &b, const char *format, ...)
{
va_list args;
va_start(args, format);
int n = vsnprintf(NULL, 0, format, args);
va_end(args);
uint32_t end = array::size(b);
array::resize(b, end + n + 1);
va_start(args, format);
vsnprintf(array::begin(b) + end, n + 1, format, args);
va_end(args);
array::resize(b, end + n);
return b;
}
Buffer & tab(Buffer &b, uint32_t column)
{
uint32_t current_column = 0;
uint32_t i = array::size(b) - 1;
while (i != 0xffffffffu && b[i] != '\n' && b[i] != '\r') {
++current_column;
--i;
}
if (current_column < column)
repeat(b, column - current_column, ' ');
return b;
}
Buffer & repeat(Buffer &b, uint32_t count, char c)
{
for (uint32_t i=0; i<count; ++i)
array::push_back(b, c);
return b;
}
}
}