-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringBuilder.cpp
58 lines (46 loc) · 1.37 KB
/
StringBuilder.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
#include "StringBuilder.h"
using namespace OTF;
StringBuilder::StringBuilder(size_t maxLength) {
this->maxLength = maxLength;
buffer = new char[maxLength];
}
StringBuilder::~StringBuilder() {
delete buffer;
}
void StringBuilder::bprintf(char *format, va_list args) {
// Don't do anything if the buffer already contains invalid data.
if (!valid) {
return;
}
length += vsnprintf(&buffer[length], maxLength - length, format, args);
// The builder is invalid if the string fits perfectly in the buffer since there wouldn't be room for the null terminator.
if (length >= maxLength) {
// snprintf will not allow more than the specified number of characters to be written to the buffer, so the length will be the buffer size.
length = maxLength;
valid = false;
}
}
void StringBuilder::bprintf(char *const format, ...) {
va_list args;
va_start(args, format);
bprintf(format, args);
va_end(args);
}
void StringBuilder::bprintf(const __FlashStringHelper *const format, va_list args) {
bprintf((char *) format, args);
}
void StringBuilder::bprintf(const __FlashStringHelper *const format, ...) {
va_list args;
va_start(args, format);
bprintf(format, args);
va_end(args);
}
char *StringBuilder::toString() const {
return &buffer[0];
}
size_t StringBuilder::getLength() const {
return length;
}
bool StringBuilder::isValid() {
return valid;
}