forked from olive-editor/core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstringutils.cpp
More file actions
107 lines (79 loc) · 2.4 KB
/
stringutils.cpp
File metadata and controls
107 lines (79 loc) · 2.4 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/***
This file is part of Oak Video Editor - A fork of original project Olive
Olive - Non-Linear Video Editor
Copyright (C) 2023 Olive Studios LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "util/stringutils.h"
#include <stdarg.h>
#include <stdexcept>
namespace olive::core
{
std::vector<std::string> StringUtils::split(const std::string &s,
char separator)
{
std::vector<std::string> output;
std::string::size_type prev_pos = 0, pos = 0;
while ((pos = s.find(separator, pos)) != std::string::npos) {
std::string substring(s.substr(prev_pos, pos - prev_pos));
output.push_back(substring);
prev_pos = ++pos;
}
output.push_back(s.substr(prev_pos, pos - prev_pos)); // Last word
return output;
}
std::vector<std::string> StringUtils::split_regex(const std::string &s,
const std::regex ®ex)
{
std::vector<std::string> output;
std::sregex_token_iterator iter(s.begin(), s.end(), regex, -1);
std::sregex_token_iterator end;
for (; iter != end; iter++) {
output.push_back(*iter);
}
return output;
}
int StringUtils::to_int(const std::string &s, int base, bool *ok)
{
try {
int x = std::stoi(s, nullptr, base);
if (ok) {
*ok = true;
}
return x;
} catch (const std::invalid_argument &e) {
if (ok) {
*ok = false;
}
return 0;
}
}
std::string StringUtils::format(const char *fmt, ...)
{
va_list ap1, ap2;
va_start(ap1, fmt);
// Need to duplicate because we call vsnprintf twice and it consumes the va_list each time
va_copy(ap2, ap1);
int s = std::vsnprintf(nullptr, 0, fmt, ap1);
// Create string with size, adding 1 because vsnprintf will want to write a null terminator
std::string r;
s++;
r.resize(s);
// Write into string
std::vsnprintf(r.data(), s, fmt, ap2);
// Pop null terminator
r.pop_back();
va_end(ap2);
va_end(ap1);
return r;
}
}