Add wcstringutil.h truncate function

This commit is contained in:
Mahmoud Al-Qudsi
2018-03-09 14:52:12 -06:00
parent 1fbf810946
commit a991fd5a1a
2 changed files with 27 additions and 0 deletions

View File

@@ -29,3 +29,19 @@ wcstring_range wcstring_tok(wcstring& str, const wcstring& needle, wcstring_rang
str[next_pos] = L'\0';
return std::make_pair(pos, next_pos - pos);
}
wcstring truncate(const wcstring &input, int max_len, ellipsis_type etype) {
if (input.size() <= max_len) {
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;
}

View File

@@ -49,4 +49,15 @@ void split_about(ITER haystack_start, ITER haystack_end, ITER needle_start, ITER
// Trailing component, possibly empty.
output->push_back(wcstring(haystack_cursor, haystack_end));
}
enum class ellipsis_type {
None,
//Prefer niceness over minimalness
Prettiest,
//Make every character count ($ instead of ...)
Shortest,
};
wcstring truncate(const wcstring &input, int max_len, ellipsis_type etype = ellipsis_type::Prettiest);
#endif