-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringBuilder.h
57 lines (45 loc) · 1.87 KB
/
StringBuilder.h
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
#ifndef OTF_STRINGBUILDER_H
#define OTF_STRINGBUILDER_H
#include <Arduino.h>
namespace OTF {
/**
* Wraps a buffer to build a string with repeated calls to sprintf. If any of calls to sprintf cause an error (such
* as exceeding the size of the internal buffer), the error will be silently swallowed and the StringBuilder will be
* marked as invalid. This means that any error checking can occur after the entire string has been built instead of
* a check being required after each individual call to sprintf.
*/
class StringBuilder {
private:
size_t maxLength;
char *buffer;
size_t length = 0;
protected:
bool valid = true;
public:
explicit StringBuilder(size_t maxLength);
~StringBuilder();
/**
* Inserts a string into the buffer at the current position using the same formatting rules as printf. If the operation
* would cause the buffer length to be exceeded or some other error occurs, the StringBuilder will be marked as invalid.
* @param format The format string to pass to sprintf.
* @param ... The format arguments to pass to sprintf.
*/
void bprintf(char *format, va_list args);
void bprintf(char *format, ...);
void bprintf(const __FlashStringHelper *const format, va_list args);
void bprintf(const __FlashStringHelper *const format, ...);
/**
* Returns the null-terminated represented string stored in the underlying buffer.
* @return The null-terminated represented string stored in the underlying buffer.
*/
char *toString() const;
size_t getLength() const;
/**
* Returns a boolean indicating if the string was built successfully without any errors. If false, the behavior
* of toString() is undefined, and the string it returns (which may or may not be null terminated) should NOT
* be used.
*/
bool isValid();
};
}// namespace OTF
#endif