diff options
author | John Wiegley <johnw@newartisans.com> | 2009-01-23 19:49:22 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2009-01-23 19:50:00 -0400 |
commit | 900a92e1158cb178335d16ff0912f5fc5701da32 (patch) | |
tree | 7da8de9f4a95a18659434c1968c8af43ee34f435 /src/format.cc | |
parent | f52e04c2bac1d4900bfe8963f369178f7f76023f (diff) | |
download | fork-ledger-900a92e1158cb178335d16ff0912f5fc5701da32.tar.gz fork-ledger-900a92e1158cb178335d16ff0912f5fc5701da32.tar.bz2 fork-ledger-900a92e1158cb178335d16ff0912f5fc5701da32.zip |
Added support for Unicode text in Ledger files, thanks to 'utfcpp', which can
be located at http://utfcpp.sourceforge.net.
Diffstat (limited to 'src/format.cc')
-rw-r--r-- | src/format.cc | 81 |
1 files changed, 39 insertions, 42 deletions
diff --git a/src/format.cc b/src/format.cc index d1f606d3..a4596761 100644 --- a/src/format.cc +++ b/src/format.cc @@ -308,58 +308,60 @@ void format_t::format(std::ostream& out_str, scope_t& scope) break; } - string temp = out.str(); - - DEBUG("format.expr", "output = \"" << temp << "\""); + unistring temp(out.str()); + string result; if (! elem->has_flags(ELEMENT_FORMATTED) && - elem->max_width > 0 && elem->max_width < temp.length()) - out_str << truncate(temp, elem->max_width); - else - out_str << temp; + elem->max_width > 0 && elem->max_width < temp.length()) { + result = truncate(temp, elem->max_width); + } else { + result = temp.extract(); + for (int i = 0; i < (int)elem->min_width - (int)temp.length(); i++) + result += " "; + } + + out_str << result; } } -string format_t::truncate(const string& str, unsigned int width, +string format_t::truncate(const unistring& ustr, unsigned int width, const bool is_account) { - const unsigned int len = str.length(); - if (len <= width) - return str; - assert(width < 4095); - char buf[4096]; + const unsigned int len = ustr.length(); + if (len <= width) + return ustr.extract(); + + std::ostringstream buf; switch (elision_style) { case TRUNCATE_LEADING: // This method truncates at the beginning. - std::strncpy(buf, str.c_str() + (len - width), width); - buf[0] = '.'; - buf[1] = '.'; + buf << ".." << ustr.extract(len - width, width); break; case TRUNCATE_MIDDLE: // This method truncates in the middle. - std::strncpy(buf, str.c_str(), width / 2); - std::strncpy(buf + width / 2, - str.c_str() + (len - (width / 2 + width % 2)), - width / 2 + width % 2); - buf[width / 2 - 1] = '.'; - buf[width / 2] = '.'; + buf << ustr.extract(0, width / 2) + << ".." + << ustr.extract(len - (width / 2 + width % 2), + width / 2 + width % 2); break; case ABBREVIATE: if (is_account) { std::list<string> parts; string::size_type beg = 0; - for (string::size_type pos = str.find(':'); + string strcopy(ustr.extract()); + for (string::size_type pos = strcopy.find(':'); pos != string::npos; - beg = pos + 1, pos = str.find(':', beg)) - parts.push_back(string(str, beg, pos - beg)); - parts.push_back(string(str, beg)); + beg = pos + 1, pos = strcopy.find(':', beg)) + parts.push_back(string(strcopy, beg, pos - beg)); + parts.push_back(string(strcopy, beg)); + + std::ostringstream result; - string result; unsigned int newlen = len; for (std::list<string>::iterator i = parts.begin(); i != parts.end(); @@ -367,28 +369,26 @@ string format_t::truncate(const string& str, unsigned int width, // Don't contract the last element std::list<string>::iterator x = i; if (++x == parts.end()) { - result += *i; + result << *i; break; } if (newlen > width) { - result += string(*i, 0, abbrev_length); - result += ":"; - newlen -= (*i).length() - abbrev_length; + unistring temp(*i); + result << temp.extract(0, abbrev_length) << ":"; + newlen -= temp.length() - abbrev_length; } else { - result += *i; - result += ":"; + result << *i << ":"; } } if (newlen > width) { // Even abbreviated its too big to show the last account, so // abbreviate all but the last and truncate at the beginning. - std::strncpy(buf, result.c_str() + (result.length() - width), width); - buf[0] = '.'; - buf[1] = '.'; + unistring temp(result.str()); + buf << ".." << temp.extract(temp.length() - width, width); } else { - std::strcpy(buf, result.c_str()); + buf << result.str(); } break; } @@ -396,14 +396,11 @@ string format_t::truncate(const string& str, unsigned int width, case TRUNCATE_TRAILING: // This method truncates at the end (the default). - std::strncpy(buf, str.c_str(), width - 2); - buf[width - 2] = '.'; - buf[width - 1] = '.'; + buf << ustr.extract(0, width -2) << ".."; break; } - buf[width] = '\0'; - return buf; + return buf.str(); } } // namespace ledger |