diff options
author | Marius Kintel <marius@kintel.net> | 2013-12-11 07:50:52 (GMT) |
---|---|---|
committer | Marius Kintel <marius@kintel.net> | 2013-12-11 07:50:52 (GMT) |
commit | 6987ac927907bd2440abd8336cf6fa095a984bab (patch) | |
tree | ad119a79974ae6f875dffd0801154617755ee59c /src/value.cc | |
parent | d14f0be01c06a872a7fb0cef6e0fa67ad7bc4a4f (diff) | |
parent | 509a466ddd903906d290e30e98e5b6b01359b2f4 (diff) |
Merge pull request #561 from brodykenrick/master
Unicode support for search, length and STRING[] accesses (+ a fix for 3rd Party CGAL lib issue on Ubunti)
Diffstat (limited to 'src/value.cc')
-rw-r--r-- | src/value.cc | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/src/value.cc b/src/value.cc index 5afb650..c8a88c6 100644 --- a/src/value.cc +++ b/src/value.cc @@ -36,6 +36,8 @@ #include <boost/format.hpp> #include "boost-utils.h" #include "boosty.h" +/*Unicode support for string lengths and array accesses*/ +#include <glib.h> std::ostream &operator<<(std::ostream &stream, const Filename &filename) { @@ -579,14 +581,28 @@ Value Value::operator-() const } */ +/* + * bracket operation [] detecting multi-byte unicode. + * If the string is multi-byte unicode then the index will offset to the character (2 or 4 byte) and not to the byte. + * A 'normal' string with byte chars are a subset of unicode and still work. + */ class bracket_visitor : public boost::static_visitor<Value> { public: Value operator()(const std::string &str, const double &idx) const { int i = int(idx); Value v; + //Check that the index is positive and less than the size in bytes if ((i >= 0) && (i < (int)str.size())) { - v = Value(str[int(idx)]); + //Ensure character (not byte) index is inside the character/glyph array + if( (unsigned) i < g_utf8_strlen( str.c_str(), str.size() ) ) { + gchar utf8_of_cp[6] = ""; //A buffer for a single unicode character to be copied into + gchar* ptr = g_utf8_offset_to_pointer(str.c_str(), i); + if(ptr) { + g_utf8_strncpy(utf8_of_cp, ptr, 1); + } + v = std::string(utf8_of_cp); + } // std::cout << "bracket_visitor: " << v << "\n"; } return v; |