From 2dae3bbedcdf55983d23fc90bb36111c7eb68fc7 Mon Sep 17 00:00:00 2001 From: Kuang-che Wu Date: Sat, 6 Jun 2020 12:43:46 +0800 Subject: format_t::truncate support wide characters also add unit tests --- src/format.cc | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/format.cc') diff --git a/src/format.cc b/src/format.cc index 5b9baa21..87168ea2 100644 --- a/src/format.cc +++ b/src/format.cc @@ -478,7 +478,7 @@ string format_t::truncate(const unistring& ustr, { assert(width < 4095); - const std::size_t len = ustr.length(); + const std::size_t len = ustr.width(); if (width == 0 || len <= width) return ustr.extract(); @@ -491,14 +491,14 @@ string format_t::truncate(const unistring& ustr, switch (style) { case TRUNCATE_LEADING: // This method truncates at the beginning. - buf << ".." << ustr.extract(len - (width - 2), width - 2); + buf << ".." << ustr.extract_by_width(len - (width - 2), width - 2); break; case TRUNCATE_MIDDLE: // This method truncates in the middle. - buf << ustr.extract(0, (width - 2) / 2) + buf << ustr.extract_by_width(0, (width - 2) / 2) << ".." - << ustr.extract(len - ((width - 2) / 2 + (width - 2) % 2), + << ustr.extract_by_width(len - ((width - 2) / 2 + (width - 2) % 2), (width - 2) / 2 + (width - 2) % 2); break; @@ -545,7 +545,7 @@ string format_t::truncate(const unistring& ustr, for (std::list::iterator i = parts.begin(); i != parts.end(); i++) { - std::size_t l = unistring(*i).length(); + std::size_t l = unistring(*i).width(); DEBUG("format.abbrev", "Segment " << ++index << " is " << l << " chars wide"); lens.push_back(l); @@ -667,8 +667,8 @@ string format_t::truncate(const unistring& ustr, } unistring temp(*i); - if (temp.length() > *l) - result << temp.extract(0, *l) << ":"; + if (temp.width() > *l) + result << temp.extract_by_width(0, *l) << ":"; else result << *i << ":"; } @@ -677,8 +677,8 @@ string format_t::truncate(const unistring& ustr, // Even abbreviated its too big to show the last account, so // abbreviate all but the last and truncate at the beginning. unistring temp(result.str()); - assert(temp.length() > width - 2); - buf << ".." << temp.extract(temp.length() - (width - 2), width - 2); + assert(temp.width() > width - 2); + buf << ".." << temp.extract_by_width(temp.width() - (width - 2), width - 2); } else { buf << result.str(); } @@ -688,7 +688,7 @@ string format_t::truncate(const unistring& ustr, case TRUNCATE_TRAILING: // This method truncates at the end (the default). - buf << ustr.extract(0, width - 2) << ".."; + buf << ustr.extract_by_width(0, width - 2) << ".."; break; } -- cgit v1.2.3