diff options
Diffstat (limited to 'cpp/src/slice2objc/Gen.cpp')
-rw-r--r-- | cpp/src/slice2objc/Gen.cpp | 100 |
1 files changed, 93 insertions, 7 deletions
diff --git a/cpp/src/slice2objc/Gen.cpp b/cpp/src/slice2objc/Gen.cpp index 283efc935e3..1363779f8e9 100644 --- a/cpp/src/slice2objc/Gen.cpp +++ b/cpp/src/slice2objc/Gen.cpp @@ -17,6 +17,8 @@ #include <direct.h> #endif #include <IceUtil/Iterator.h> +#include <IceUtil/Unicode.h> +#include <IceUtil/InputUtil.h> #include <IceUtil/UUID.h> #include <Slice/Checksum.h> #include <Slice/FileTracker.h> @@ -1492,13 +1494,13 @@ Slice::Gen::TypesVisitor::writeConstantValue(IceUtilInternal::Output& out, const out << "@\""; // Opening @" - for(string::const_iterator c = val.begin(); c != val.end(); ++c) + for(size_t i = 0; i < val.size();) { - if(charSet.find(*c) == charSet.end()) + if(charSet.find(val[i]) == charSet.end()) { - unsigned char uc = *c; // char may be signed, so make it positive + unsigned char uc = val[i]; // char may be signed, so make it positive ostringstream s; - s << "\\"; // Print as octal if not in basic source character set + s << "\\"; // Print as octal if not in basic source character set s.width(3); s.fill('0'); s << oct; @@ -1507,11 +1509,95 @@ Slice::Gen::TypesVisitor::writeConstantValue(IceUtilInternal::Output& out, const } else { - out << *c; // Print normally if in basic source character set + switch(val[i]) + { + case '\\': + { + string s = "\\"; + size_t j = i + 1; + for(; j < val.size(); ++j) + { + if(val[j] != '\\') + { + break; + } + s += "\\"; + } + + // + // An even number of slash \ will escape the backslash and + // the codepoint will be interpreted as its charaters + // + // \\U00000041 - ['\\', 'U', '0', '0', '0', '0', '0', '0', '4', '1'] + // \\\U00000041 - ['\\', 'A'] (41 is the codepoint for 'A') + // + if(s.size() % 2 != 0 && (val[j] == 'U' || val[j] == 'u')) + { + // + // Convert codepoint to UTF8 bytes and write the escaped bytes + // + out << s.substr(0, s.size() - 1); + + size_t sz = val[j] == 'U' ? 8 : 4; + string codepoint = val.substr(j + 1, sz); + assert(codepoint.size() == sz); + + IceUtil::Int64 v = IceUtilInternal::strToInt64(codepoint.c_str(), 0, 16); + + + vector<unsigned int> u32buffer; + u32buffer.push_back(static_cast<unsigned int>(v)); + + vector<unsigned char> u8buffer; + + IceUtilInternal::ConversionResult result = convertUTF32ToUTF8(u32buffer, u8buffer, IceUtil::lenientConversion); + switch(result) + { + case conversionOK: + break; + case sourceExhausted: + throw IceUtil::IllegalConversionException(__FILE__, __LINE__, "string source exhausted"); + case sourceIllegal: + throw IceUtil::IllegalConversionException(__FILE__, __LINE__, "string source illegal"); + default: + { + assert(0); + throw IceUtil::IllegalConversionException(__FILE__, __LINE__); + } + } + + ostringstream s; + for(vector<unsigned char>::const_iterator q = u8buffer.begin(); q != u8buffer.end(); ++q) + { + s << "\\"; + s.fill('0'); + s.width(3); + s << oct; + s << static_cast<unsigned int>(*q); + } + out << s.str(); + + i = j + 1 + sz; + } + else + { + out << s; + i = j; + } + continue; + } + case '"': + { + out << "\\"; + break; + } + } + + out << val[i]; // Print normally if in basic source character set } + ++i; } - - out << "\""; // Closing " + out << "\""; // Closing " } else { |