diff options
author | Brody Kenrick <user.fake@server.userfake> | 2013-12-05 06:56:54 (GMT) |
---|---|---|
committer | Marius Kintel <marius@kintel.net> | 2013-12-08 23:35:35 (GMT) |
commit | 3abf64249fd667f0b7f558ecfbbf35cfe9916a5d (patch) | |
tree | a5e3aa4699cf5b9d27693f8ab5411acdacc2fdb0 /src/value.cc | |
parent | 7075d8d9c4dde62798022bdfe02c5d57e997ba43 (diff) |
Unicode support for strings
Add suport for using unicode strings in .scad files. Support iterating
across them/accessing them via [] and searching.
--------
Add GLIB (to build for test and normal build -- both with installed and
built locally development files).
Add support for unicode chars to length and search builtin functions and
[] for strings.
Added unicode testing functions.
Ad GLIB to library info page.
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; |