summaryrefslogtreecommitdiff
path: root/src/value.cc
diff options
context:
space:
mode:
authorBrody Kenrick <user.fake@server.userfake>2013-12-05 06:56:54 (GMT)
committerBrody Kenrick <user.fake@server.userfake>2013-12-05 07:28:40 (GMT)
commit0717c67c9fa894ecb08dc5de281753a00922d1ee (patch)
tree77baf10b4244a189f1212f3affee08a82a999013 /src/value.cc
parentd3b82dcac0cbd6bb46c3236d1183f84b76b44748 (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.cc18
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;
contact: Jan Huwald // Impressum