-
Notifications
You must be signed in to change notification settings - Fork 45
/
string_utils.cpp
271 lines (208 loc) · 8.74 KB
/
string_utils.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "string_utils.h"
#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <sstream>
extern "C" {
#include <libavutil/pixdesc.h>
}
// Borrowed from https://www.techiedelight.com/implode-a-vector-of-strings-into-a-comma-separated-string-in-cpp/
std::string string_join(const std::vector<std::string>& strings, const std::string& delim) {
return std::accumulate(strings.begin(), strings.end(), std::string(), [&delim](const std::string& x, const std::string& y) { return x.empty() ? y : x + delim + y; });
}
std::vector<std::string> string_split(const std::string& string, char delim) {
std::vector<std::string> tokens;
std::string token;
std::istringstream token_stream(string);
while (std::getline(token_stream, token, delim)) {
tokens.push_back(token);
}
return tokens;
}
std::string format_position(const float position, const bool use_compact) {
const std::string sign = position < 0 ? "-" : "";
const float rounded_millis = std::round(std::fabs(position) * 1000.0F);
const int milliseconds = rounded_millis;
const int seconds = milliseconds / 1000;
const int minutes = seconds / 60;
const int hours = minutes / 60;
if (!use_compact || minutes >= 60) {
return string_sprintf("%s%02d:%02d:%02d.%03d", sign.c_str(), hours, minutes % 60, seconds % 60, milliseconds % 1000);
}
if (seconds >= 60) {
return string_sprintf("%s%02d:%02d.%03d", sign.c_str(), minutes, seconds % 60, milliseconds % 1000);
}
return string_sprintf("%s%d.%03d", sign.c_str(), seconds, milliseconds % 1000);
}
std::string format_duration(const float duration) {
return duration > 0 ? format_position(duration, false) : "unknown duration";
}
std::string to_lower_case(const std::string& str) {
std::string tmp;
std::transform(str.begin(), str.end(), std::back_inserter(tmp), ::tolower);
return tmp;
}
inline bool ci_compare_char(char a, char b) {
return (::tolower(a) == b);
}
std::string::const_iterator string_ci_find(std::string& str, const std::string& query) {
const std::string lower_case_query = to_lower_case(query);
return (search(str.cbegin(), str.cend(), lower_case_query.cbegin(), lower_case_query.cend(), ci_compare_char));
}
std::string stringify_frame_rate(const AVRational frame_rate, const AVFieldOrder field_order) noexcept {
static const std::string postfix = "fps";
// format field order
std::string field_order_str;
switch (field_order) {
case AV_FIELD_PROGRESSIVE:
field_order_str = " (progressive)";
break;
case AV_FIELD_TT:
field_order_str = " (top first)";
break;
case AV_FIELD_BB:
field_order_str = " (bottom first)";
break;
case AV_FIELD_TB:
field_order_str = " (top coded first, swapped)";
break;
case AV_FIELD_BT:
field_order_str = " (bottom coded first, swapped)";
break;
default:
field_order_str = "";
}
// formatting code borrowed (with love!) from libavformat/dump.c
const double d = av_q2d(frame_rate);
const uint64_t v = lrintf(d * 100);
if (!v) {
return string_sprintf("%1.4f %s%s", d, postfix.c_str(), field_order_str.c_str());
} else if (v % 100) {
return string_sprintf("%3.2f %s%s", d, postfix.c_str(), field_order_str.c_str());
} else if (v % (100 * 1000)) {
return string_sprintf("%1.0f %s%s", d, postfix.c_str(), field_order_str.c_str());
}
return string_sprintf("%1.0fk %s%s", d / 1000, postfix.c_str(), field_order_str.c_str());
}
std::string stringify_decoder(const VideoDecoder* video_decoder) noexcept {
return video_decoder->is_hw_accelerated() ? string_sprintf("%s (%s)", video_decoder->codec()->name, video_decoder->hw_accel_name().c_str()) : video_decoder->codec()->name;
}
// Slightly modified from https://stackoverflow.com/questions/63511627/how-can-i-stringify-a-fraction-with-n-decimals-in-c/63511628#63511628
std::string stringify_fraction(const uint64_t num, const uint64_t den, const unsigned precision) {
constexpr unsigned base = 10;
// prevent division by zero if necessary
if (den == 0) {
return "inf";
}
// integral part can be computed using regular division
std::string result = std::to_string(num / den);
// perform first step of long division
// also cancel early if there is no fractional part
uint64_t tmp = num % den;
if (tmp == 0 || precision == 0) {
return result;
}
// reserve characters to avoid unnecessary re-allocation
result.reserve(result.size() + precision + 1);
// fractional part can be computed using long divison
result += '.';
for (size_t i = 0; i < precision; ++i) {
tmp *= base;
char nextDigit = '0' + static_cast<char>(tmp / den);
result.push_back(nextDigit);
tmp %= den;
}
return result;
}
static const uint64_t POWERS_OF_1000[] = {1, 1000, 1000000, 1000000000, 1000000000000, 1000000000000000, 1000000000000000000};
static const uint64_t POWERS_OF_1024[] = {1, 1024, 1048576, 1073741824, 1099511627776, 1125899906842624, 1152921504606846976};
int uint64_log(uint64_t n, const uint64_t* power_table, const size_t table_size) {
int left = 0;
int right = table_size / sizeof(uint64_t) - 1;
// binary search
while (left < right) {
int mid = (left + right + 1) / 2;
if (power_table[mid] <= n) {
left = mid;
} else {
right = mid - 1;
}
}
return left;
}
static const char FILE_SIZE_UNITS[7][3] = {"B", "KB", "MB", "GB", "TB", "PB", "EB"};
// Derived from https://stackoverflow.com/questions/63512258/how-can-i-print-a-human-readable-file-size-in-c-without-a-loop
std::string stringify_file_size(const int64_t size, const unsigned precision) noexcept {
if (size < 0) {
return "unknown size";
}
unsigned unit = uint64_log(size, POWERS_OF_1024, sizeof(POWERS_OF_1024));
std::string result = stringify_fraction(size, POWERS_OF_1024[unit], precision);
result.reserve(result.size() + 5);
result.push_back(' ');
result.push_back(FILE_SIZE_UNITS[unit][0]);
if (unit != 0) {
result.push_back('i');
result.push_back(FILE_SIZE_UNITS[unit][1]);
}
return result;
}
// Derived from https://stackoverflow.com/questions/63512258/how-can-i-print-a-human-readable-file-size-in-c-without-a-loop
std::string stringify_bit_rate(const int64_t bit_rate, const unsigned precision) noexcept {
if (bit_rate == 0) {
return "unknown bitrate";
}
unsigned unit = uint64_log(bit_rate, POWERS_OF_1000, sizeof(POWERS_OF_1000));
std::string result = stringify_fraction(bit_rate, POWERS_OF_1000[unit], precision);
result.reserve(result.size() + 8);
result.push_back(' ');
if (unit != 0) {
char first = FILE_SIZE_UNITS[unit][0];
first += 'a' - 'A';
result.push_back(first);
}
result.append("b/s");
return result;
}
std::string stringify_pixel_format(const AVPixelFormat pixel_format, const AVColorRange color_range, const AVColorSpace color_space, const AVColorPrimaries color_primaries, const AVColorTransferCharacteristic color_trc) noexcept {
std::string color_range_str;
std::string color_space_str;
// code adapted from FFmpeg's avcodec.c (thanks guys!)
auto unknown_if_null = [](const char* str) { return str ? str : "unknown"; };
if (color_range == AVCOL_RANGE_UNSPECIFIED || (color_range_str = av_color_range_name(color_range)).empty()) {
color_range_str = "";
}
if (color_space != AVCOL_SPC_UNSPECIFIED || color_primaries != AVCOL_PRI_UNSPECIFIED || color_trc != AVCOL_TRC_UNSPECIFIED) {
const char* col = unknown_if_null(av_color_space_name(color_space));
const char* pri = unknown_if_null(av_color_primaries_name(color_primaries));
const char* trc = unknown_if_null(av_color_transfer_name(color_trc));
if (strcmp(col, pri) || strcmp(col, trc)) {
color_space_str = string_sprintf("%s/%s/%s", col, pri, trc);
} else {
color_space_str = col;
}
} else {
color_space_str = "";
}
const std::string range_color_space_separator = !color_range_str.empty() && !color_space_str.empty() ? ", " : "";
const std::string range_and_color_space = !color_range_str.empty() || !color_space_str.empty() ? string_sprintf(" (%s%s%s)", color_range_str.c_str(), range_color_space_separator.c_str(), color_space_str.c_str()) : "";
return string_sprintf("%s%s", av_get_pix_fmt_name(pixel_format), range_and_color_space.c_str());
}
void print_wrapped(const std::string& text, const size_t line_length) {
size_t pos = 0;
while (pos < text.length()) {
size_t next_pos = (pos + line_length < text.length()) ? pos + line_length : text.length();
if (next_pos != text.length()) {
size_t space_pos = text.rfind(' ', next_pos);
if (space_pos != std::string::npos && space_pos > pos) {
next_pos = space_pos;
}
}
std::cout << text.substr(pos, next_pos - pos) << std::endl;
pos = text.find_first_not_of(' ', next_pos);
if (pos == std::string::npos) {
break;
}
}
}