Euphoria
stringbuilder.cc
Go to the documentation of this file.
1 #include "assert/assert.h"
2 
3 #include "base/stringbuilder.h"
4 
5 
6 
7 namespace eu
8 {
9 
10 namespace
11 {
12  bool can_add_to(const std::vector<char>& data)
13  {
14  // empty strings are ok
15  if(data.empty()) { return true; }
16 
17  // string is valid if it doesn't end with a null terminator
18  return *data.rbegin() != '\0';
19  }
20 }
21 
22 
24 {
25  return data.empty() == false;
26 }
27 
28 
30 {
31  data.clear();
32 }
33 
34 
36 {
37  ASSERT(can_add_to(data));
38  data.emplace_back(c);
39  return *this;
40 }
41 
42 
43 StringBuilder& StringBuilder::add_string(const std::string& str)
44 {
45  ASSERT(can_add_to(data));
46  for(char c: str)
47  {
48  data.emplace_back(c);
49  }
50 
51  return *this;
52 }
53 
54 
55 StringBuilder& StringBuilder::add_view(const std::string_view& str)
56 {
57  ASSERT(can_add_to(data));
58  for(char c: str)
59  {
60  data.emplace_back(c);
61  }
62 
63  return *this;
64 }
65 
66 
68 {
69  ASSERT(can_add_to(data));
70  data.emplace_back('\0'); // now we no longer can add to it!
71  return data.data();
72 }
73 
74 }
75 
#define ASSERT(x)
Definition: assert.h:29
Definition: assert.h:90
bool has_content() const
std::string to_string()
Complete the builder and return the resulting string.
StringBuilder & add_view(const std::string_view &str)
std::vector< char > data
Definition: stringbuilder.h:11
StringBuilder & add_string(const std::string &str)
StringBuilder & add_char(char c)