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-04 06:18:24 +08:00
|
|
|
#include "common.h"
|
2016-05-28 05:41:16 +08:00
|
|
|
#include "wcstringutil.h"
|
2014-09-22 10:18:56 +08:00
|
|
|
|
|
|
|
typedef wcstring::size_type size_type;
|
|
|
|
|
2016-05-04 06:18:24 +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) {
|
2018-04-18 02:28:56 +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) {
|
|
|
|
return input.substr(0, max_len - wcslen(ellipsis_str)).append(ellipsis_str);
|
|
|
|
}
|
|
|
|
wcstring output = input.substr(0, max_len - 1);
|
|
|
|
output.push_back(ellipsis_char);
|
|
|
|
return output;
|
|
|
|
}
|
2018-03-12 08:36:10 +08:00
|
|
|
|
2018-03-12 11:02:43 +08:00
|
|
|
wcstring trim(const wcstring &input, const wchar_t *any_of) {
|
|
|
|
auto begin_offset = input.find_first_not_of(any_of);
|
2018-03-12 08:36:10 +08:00
|
|
|
if (begin_offset == wcstring::npos) {
|
|
|
|
return wcstring{};
|
|
|
|
}
|
2018-03-12 11:02:43 +08:00
|
|
|
auto end = input.cbegin() + input.find_last_not_of(any_of);
|
2018-03-12 08:36:10 +08:00
|
|
|
|
|
|
|
wcstring result(input.begin() + begin_offset, end + 1);
|
|
|
|
return result;
|
|
|
|
}
|