summaryrefslogtreecommitdiff
path: root/src/format.h
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-01-23 19:49:22 -0400
committerJohn Wiegley <johnw@newartisans.com>2009-01-23 19:50:00 -0400
commit900a92e1158cb178335d16ff0912f5fc5701da32 (patch)
tree7da8de9f4a95a18659434c1968c8af43ee34f435 /src/format.h
parentf52e04c2bac1d4900bfe8963f369178f7f76023f (diff)
downloadfork-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.h')
-rw-r--r--src/format.h52
1 files changed, 51 insertions, 1 deletions
diff --git a/src/format.h b/src/format.h
index 13a2fff2..fbfe452e 100644
--- a/src/format.h
+++ b/src/format.h
@@ -32,13 +32,63 @@
#ifndef _FORMAT_H
#define _FORMAT_H
+#define SUPPORT_UNICODE 1
+
#include "journal.h"
#include "expr.h"
+#if defined(SUPPORT_UNICODE)
+#include "utf8.h"
+#endif
namespace ledger {
DECLARE_EXCEPTION(format_error, std::runtime_error);
+#if defined(SUPPORT_UNICODE)
+/**
+ * @class unistring
+ *
+ * @brief Abstract working with UTF-32 encoded Unicode strings
+ *
+ * The input to the string is a UTF8 encoded ledger::string, which can
+ * then have its true length be taken, or characters extracted.
+ */
+class unistring
+{
+ std::vector<uint32_t> utf32chars;
+
+public:
+ unistring(const string& input)
+ {
+ TRACE_CTOR(unistring, "");
+
+ const char * p = input.c_str();
+ std::size_t len = input.length();
+
+ VERIFY(utf8::is_valid(p, p + len));
+
+ utf8::utf8to32(p, p + len, std::back_inserter(utf32chars));
+ }
+ ~unistring() {
+ TRACE_DTOR(unistring);
+ }
+
+ std::size_t length() const {
+ return utf32chars.size();
+ }
+
+ string extract(const std::size_t begin = 0,
+ const std::size_t len = 0) const
+ {
+ string utf8result;
+ utf8::utf32to8(utf32chars.begin() + begin,
+ utf32chars.begin() + begin + (len ? len : length()),
+ std::back_inserter(utf8result));
+ return utf8result;
+ }
+};
+#endif
+
class report_t;
class format_t : public noncopyable
@@ -135,7 +185,7 @@ public:
elem->dump(out);
}
- static string truncate(const string& str, unsigned int width,
+ static string truncate(const unistring& str, unsigned int width,
const bool is_account = false);
};