10 #include <fmt/chrono.h>
16 #include <unordered_set>
58 void StringAppendV(std::string *dst,
const char *
format, va_list ap) {
60 static const int kFixedBufferSize = 1024;
61 char fixed_buffer[kFixedBufferSize];
67 va_copy(backup_ap, ap);
68 int result = vsnprintf(fixed_buffer, kFixedBufferSize,
format, backup_ap);
71 if (
result < kFixedBufferSize) {
74 dst->append(fixed_buffer,
result);
81 va_copy(backup_ap, ap);
89 const int variable_buffer_size =
result + 1;
90 std::unique_ptr<char[]> variable_buffer(
new char[variable_buffer_size]);
93 va_copy(backup_ap, ap);
94 result = vsnprintf(variable_buffer.get(), variable_buffer_size,
format,
99 dst->append(variable_buffer.get(),
result);
103 bool IsNotWhiteSpace(
const int character) {
104 return character !=
' ' && character !=
'\n' && character !=
'\r' &&
120 const std::string &old_str,
121 const std::string &new_str) {
122 if (old_str.empty()) {
126 std::string mod_str = str;
128 mod_str.replace(
position, old_str.size(), new_str);
135 const std::string &old_str,
136 const std::string &new_str) {
137 std::string mod_str = str;
138 size_t start_pos = mod_str.find(old_str);
139 if (start_pos == std::string::npos)
return mod_str;
140 mod_str.replace(start_pos, old_str.length(), new_str);
145 const std::string &old_str,
146 const std::string &new_str) {
147 std::string mod_str = str;
148 size_t start_pos = mod_str.rfind(old_str);
149 if (start_pos == std::string::npos)
return mod_str;
150 mod_str.replace(start_pos, old_str.length(), new_str);
155 return src.find(dst) != std::string::npos;
159 return !prefix.empty() && prefix.size() <= str.size() &&
160 str.substr(0, prefix.size()) == prefix;
164 return !postfix.empty() && postfix.size() <= str.size() &&
165 str.substr(str.size() - postfix.size(), str.size()) == postfix;
169 const std::string &delimiter) {
170 std::ostringstream oss;
171 for (
size_t i = 0; i < strs.size(); ++i) {
173 if (i != strs.size() - 1) {
181 const std::string &delimiters ,
182 bool trim_empty_str ) {
183 std::vector<std::string> tokens;
184 std::string::size_type pos = 0, last_pos = 0;
185 std::string::size_type new_pos;
186 while (pos != std::string::npos) {
187 pos = str.find_first_of(delimiters, last_pos);
188 new_pos = (pos == std::string::npos ? str.length() : pos);
189 if (new_pos != last_pos || !trim_empty_str) {
190 tokens.push_back(str.substr(last_pos, new_pos - last_pos));
192 last_pos = new_pos + 1;
198 const std::string &str,
199 const std::string &delimiters ,
200 bool trim_empty_str ) {
201 std::string::size_type pos = 0, new_pos = 0, last_pos = 0;
202 while (pos != std::string::npos) {
203 pos = str.find_first_of(delimiters, last_pos);
204 new_pos = (pos == std::string::npos ? str.length() : pos);
205 if (new_pos != last_pos || !trim_empty_str) {
206 tokens.push_back(str.substr(last_pos, new_pos - last_pos));
208 last_pos = new_pos + 1;
213 const std::string &delimiters ,
214 bool trim_empty_str ) {
215 std::vector<std::string> tokens;
216 std::string::size_type pos = 0, new_pos = 0, last_pos = 0;
217 while (pos != std::string::npos) {
218 pos = str.find_first_of(delimiters, last_pos);
219 new_pos = (pos == std::string::npos ? str.length() : pos);
220 if (new_pos != last_pos || !trim_empty_str) {
221 tokens.push_back(str.substr(last_pos, new_pos - last_pos));
223 last_pos = new_pos + 1;
229 str.erase(0, str.find_first_not_of(chars));
234 str.erase(str.find_last_not_of(chars) + 1);
238 std::string &
StripString(std::string &str,
const std::string &chars) {
243 std::string out = str;
244 std::transform(str.begin(), str.end(), out.begin(),
245 [](
unsigned char c) { return std::tolower(c); });
250 std::string out = str;
251 std::transform(str.begin(), str.end(), out.begin(),
252 [](
unsigned char c) { return std::toupper(c); });
259 const std::string &valid_chars) {
260 std::unordered_set<char> valid_chars_set;
261 for (
const char &c : valid_chars) {
262 valid_chars_set.insert(c);
264 auto is_word_char = [&valid_chars_set](
const char &c) {
265 return std::isalnum(c) ||
266 valid_chars_set.find(c) != valid_chars_set.end();
269 for (
size_t pos = start_pos; pos < doc.size(); ++pos) {
270 if (!is_word_char(doc[pos])) {
282 usleep(milliseconds * 1000);
287 std::time_t t = std::time(
nullptr);
288 return fmt::format(
"{:%Y-%m-%d-%H-%M-%S}", *std::localtime(&t));
filament::Texture::InternalFormat format
__host__ __device__ float length(float2 v)
Helper functions for the ml ops.
size_t WordLength(const std::string &doc, size_t start_pos, const std::string &valid_chars="_")
std::string StringPrintf(const char *format,...)
void SplitString(std::vector< std::string > &tokens, const std::string &str, const std::string &delimiters=" ", bool trim_empty_str=true)
std::string & StripString(std::string &str, const std::string &chars="\t\n\v\f\r ")
bool StringEndsWith(const std::string &str, const std::string &postfix)
std::string ToLower(const std::string &s)
Convert string to the lower case.
std::string JoinStrings(const std::vector< std::string > &strs, const std::string &delimiter=", ")
std::string & RightStripString(std::string &str, const std::string &chars="\t\n\v\f\r ")
std::string StringReplaceLast(const std::string &str, const std::string &old_str, const std::string &new_str)
bool StringContains(const std::string &src, const std::string &dst)
void Sleep(int milliseconds)
std::string ToUpper(const std::string &s)
Convert string to the upper case.
std::string GetCurrentTimeStamp()
Returns current time stamp.
std::string & LeftStripString(std::string &str, const std::string &chars="\t\n\v\f\r ")
std::vector< std::string > StringSplit(const std::string &str, const std::string &delimiters=" ", bool trim_empty_str=true)
std::string StringReplace(const std::string &str, const std::string &old_str, const std::string &new_str)
std::string StringReplaceFirst(const std::string &str, const std::string &old_str, const std::string &new_str)
bool StringStartsWith(const std::string &str, const std::string &prefix)
Generic file read and write utility for python interface.