Beatmup
string_builder.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include <cstdarg>
21 #include <string>
22 
23 namespace Beatmup {
24  /**
25  Toolset to build a string content.
26  */
27  class StringBuilder {
28  std::string& str;
29  public:
30  StringBuilder(std::string& workspace) :
31  str(workspace)
32  {}
33 
34  StringBuilder& replace(const std::string& search, const std::string& replacement) {
35  size_t pos = 0;
36  while ((pos = str.find(search, pos)) != std::string::npos) {
37  str.replace(pos, search.length(), replacement);
38  pos += replacement.length();
39  }
40  return *this;
41  }
42 
43  StringBuilder& operator()(const std::string& append) {
44  str.append(append);
45  return *this;
46  }
47 
48  StringBuilder& line(const std::string& append) {
49  str.append(append + "\n");
50  return *this;
51  }
52 
53  template<const int BUF_SIZE = 256> StringBuilder& printf(const char* format, ...) {
54  va_list args;
55  va_start(args, format);
56  char buffer[BUF_SIZE];
57 #ifdef _MSC_VER
58  vsnprintf_s
59 #else
60  vsnprintf
61 #endif
62  (buffer, BUF_SIZE, format, args);
63  str.append(buffer);
64  va_end(args);
65  return *this;
66  }
67 
69  str.append("\n");
70  return *this;
71  }
72 
73  operator std::string& () {
74  return str;
75  }
76 
77  void dump(std::string filename);
78  };
79 
80  /**
81  StringBuilder including a string container.
82  */
83  class String : public StringBuilder {
84  private:
85  std::string str;
86  public:
88 
90  str = content.str;
91  }
92 
93  String(const std::string& content): StringBuilder(str) {
94  str = content;
95  }
96 
97  String(std::initializer_list<const char*> lines) : StringBuilder(str) {
98  static const std::string NL("\n");
99  for (auto& line : lines)
100  str += line + NL;
101  }
102 
103  StringBuilder& operator =(const std::string& content) {
104  str = content;
105  return *this;
106  }
107  };
108 }
Toolset to build a string content.
StringBuilder & line(const std::string &append)
StringBuilder(std::string &workspace)
void dump(std::string filename)
StringBuilder & printf(const char *format,...)
StringBuilder & nl()
StringBuilder & operator()(const std::string &append)
StringBuilder & replace(const std::string &search, const std::string &replacement)
StringBuilder including a string container.
String(std::initializer_list< const char * > lines)
std::string str
StringBuilder & operator=(const std::string &content)
String(const String &content)
String(const std::string &content)
JNIEnv jobject jint format
JNIEnv jlong jstring filename
std::string content
JNIEnv jlong jstring jboolean append