summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/format.cc12
-rw-r--r--src/unistring.h17
2 files changed, 22 insertions, 7 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