diff options
-rw-r--r-- | src/format.cc | 12 | ||||
-rw-r--r-- | src/unistring.h | 17 | ||||
-rw-r--r-- | test/baseline/opt-anon.test | 4 |
3 files changed, 24 insertions, 9 deletions
diff --git a/src/format.cc b/src/format.cc index a9d867bd..69c0b42f 100644 --- a/src/format.cc +++ b/src/format.cc @@ -335,8 +335,13 @@ string format_t::truncate(const unistring& ustr, std::size_t width, if (newlen > width) { unistring temp(*i); - result << temp.extract(0, account_abbrev_length) << ":"; - newlen -= temp.length() - account_abbrev_length; + if (temp.length() > static_cast<std::size_t>(account_abbrev_length)) { + result << temp.extract(0, account_abbrev_length) << ":"; + newlen -= temp.length() - account_abbrev_length; + } else { + result << temp.extract() << ":"; + newlen -= temp.length(); + } } else { result << *i << ":"; } @@ -346,7 +351,8 @@ string format_t::truncate(const unistring& ustr, std::size_t width, // 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()); - buf << ".." << temp.extract(temp.length() - width - 2, width - 2); + assert(temp.length() > width - 2); + buf << ".." << temp.extract(temp.length() - (width - 2), width - 2); } else { buf << result.str(); } diff --git a/src/unistring.h b/src/unistring.h index b1c46cb3..cadf82fe 100644 --- a/src/unistring.h +++ b/src/unistring.h @@ -48,6 +48,8 @@ #include "utils.h" +namespace ledger { + /** * @class unistring * @@ -68,7 +70,7 @@ public: const char * p = input.c_str(); std::size_t len = input.length(); - //assert(utf8::is_valid(p, p + len)); + assert(utf8::is_valid(p, p + len)); utf8::utf8to32(p, p + len, std::back_inserter(utf32chars)); } ~unistring() { @@ -83,9 +85,14 @@ public: const std::size_t len = 0) const { std::string utf8result; - utf8::utf32to8(utf32chars.begin() + begin, - utf32chars.begin() + begin + (len ? len : length()), - std::back_inserter(utf8result)); + std::size_t this_len = length(); + assert(begin <= this_len); + assert(begin + len <= this_len); + if (this_len) + utf8::utf32to8(utf32chars.begin() + begin, + utf32chars.begin() + begin + + (len ? (len > this_len ? this_len : len) : this_len), + std::back_inserter(utf8result)); return utf8result; } }; @@ -108,4 +115,6 @@ inline void justify(std::ostream& out, out << str; } +} // namespace ledger + #endif // _UNISTRING_H diff --git a/test/baseline/opt-anon.test b/test/baseline/opt-anon.test index 4c387b33..fb8acd64 100644 --- a/test/baseline/opt-anon.test +++ b/test/baseline/opt-anon.test @@ -4,8 +4,8 @@ reg --anon Assets:Investments:Vanguard:VMMXX 0.350 VMMXX @ $1.00 Income:Dividends:Vanguard:VMMXX $-0.35 >>>1 -07-Feb-02 6a93dcb30aa7722e967.. ..8000cfb2807d6a978b43 0.350 VMMXX 0.350 VMMXX - ..b52e4fbac0065bfcc300 $-0.35 $-0.35 +07-Feb-02 6a93dcb30aa7722e967.. ..cfb2807d6a978b431fc7 0.350 VMMXX 0.350 VMMXX + ..4fbac0065bfcc300a24e $-0.35 $-0.35 0.350 VMMXX >>>2 === 0 |