2016-05-04 06:18:24 +08:00
|
|
|
// Helper functions for working with wcstring.
|
2016-05-19 06:30:21 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2016-05-28 05:41:16 +08:00
|
|
|
#include "wcstringutil.h"
|
2014-09-22 10:18:56 +08:00
|
|
|
|
2019-09-23 06:33:08 +08:00
|
|
|
#include <wctype.h>
|
|
|
|
|
2019-10-14 06:50:48 +08:00
|
|
|
#include "common.h"
|
|
|
|
|
2014-09-22 10:18:56 +08:00
|
|
|
typedef wcstring::size_type size_type;
|
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
wcstring_range wcstring_tok(wcstring &str, const wcstring &needle, wcstring_range last) {
|
2014-09-22 10:18:56 +08:00
|
|
|
size_type pos = last.second == wcstring::npos ? wcstring::npos : last.first;
|
|
|
|
if (pos != wcstring::npos && last.second != wcstring::npos) pos += last.second;
|
|
|
|
if (pos != wcstring::npos && pos != 0) ++pos;
|
2016-05-04 06:18:24 +08:00
|
|
|
if (pos == wcstring::npos || pos >= str.size()) {
|
2014-09-22 10:18:56 +08:00
|
|
|
return std::make_pair(wcstring::npos, wcstring::npos);
|
|
|
|
}
|
|
|
|
|
2016-05-04 06:18:24 +08:00
|
|
|
if (needle.empty()) {
|
2014-09-22 10:18:56 +08:00
|
|
|
return std::make_pair(pos, wcstring::npos);
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = str.find_first_not_of(needle, pos);
|
|
|
|
if (pos == wcstring::npos) return std::make_pair(wcstring::npos, wcstring::npos);
|
|
|
|
|
|
|
|
size_type next_pos = str.find_first_of(needle, pos);
|
2016-05-04 06:18:24 +08:00
|
|
|
if (next_pos == wcstring::npos) {
|
2014-09-22 10:18:56 +08:00
|
|
|
return std::make_pair(pos, wcstring::npos);
|
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
|
|
|
|
str[next_pos] = L'\0';
|
|
|
|
return std::make_pair(pos, next_pos - pos);
|
2014-09-22 10:18:56 +08:00
|
|
|
}
|
2018-03-10 04:52:12 +08:00
|
|
|
|
|
|
|
wcstring truncate(const wcstring &input, int max_len, ellipsis_type etype) {
|
2019-05-05 18:09:25 +08:00
|
|
|
if (input.size() <= (size_t)max_len) {
|
2018-03-10 04:52:12 +08:00
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (etype == ellipsis_type::None) {
|
|
|
|
return input.substr(0, max_len);
|
|
|
|
}
|
|
|
|
if (etype == ellipsis_type::Prettiest) {
|
2019-04-29 06:00:36 +08:00
|
|
|
const wchar_t *ellipsis_str = get_ellipsis_str();
|
2019-03-13 05:06:01 +08:00
|
|
|
return input.substr(0, max_len - std::wcslen(ellipsis_str)).append(ellipsis_str);
|
2018-03-10 04:52:12 +08:00
|
|
|
}
|
|
|
|
wcstring output = input.substr(0, max_len - 1);
|
2019-04-29 06:00:36 +08:00
|
|
|
output.push_back(get_ellipsis_char());
|
2018-03-10 04:52:12 +08:00
|
|
|
return output;
|
|
|
|
}
|
2018-03-12 08:36:10 +08:00
|
|
|
|
2019-08-26 04:37:06 +08:00
|
|
|
wcstring trim(wcstring input) { return trim(std::move(input), L"\t\v \r\n"); }
|
2018-10-02 05:48:38 +08:00
|
|
|
|
2019-08-26 04:37:06 +08:00
|
|
|
wcstring trim(wcstring input, const wchar_t *any_of) {
|
|
|
|
wcstring result = std::move(input);
|
|
|
|
size_t suffix = result.find_last_not_of(any_of);
|
|
|
|
if (suffix == wcstring::npos) {
|
2018-03-12 08:36:10 +08:00
|
|
|
return wcstring{};
|
|
|
|
}
|
2019-08-26 04:37:06 +08:00
|
|
|
result.erase(suffix + 1);
|
2018-03-12 08:36:10 +08:00
|
|
|
|
2019-08-26 04:37:06 +08:00
|
|
|
auto prefix = result.find_first_not_of(any_of);
|
|
|
|
assert(prefix != wcstring::npos && "Should have one non-trimmed character");
|
|
|
|
result.erase(0, prefix);
|
2018-03-12 08:36:10 +08:00
|
|
|
return result;
|
|
|
|
}
|
2019-09-23 06:33:08 +08:00
|
|
|
|
|
|
|
wcstring wcstolower(wcstring input) {
|
|
|
|
wcstring result = std::move(input);
|
|
|
|
std::transform(result.begin(), result.end(), result.begin(), towlower);
|
|
|
|
return result;
|
|
|
|
}
|