diff options
author | Bernard Normier <bernard@zeroc.com> | 2016-10-25 18:35:15 -0400 |
---|---|---|
committer | Bernard Normier <bernard@zeroc.com> | 2016-10-25 18:35:15 -0400 |
commit | 95624751e9d98d1265e0a007e7d7a1186036750b (patch) | |
tree | d13da90f17629496d13d5badb32b04803239d662 /cpp/src/slice2cpp | |
parent | Nuget package updates (diff) | |
download | ice-95624751e9d98d1265e0a007e7d7a1186036750b.tar.bz2 ice-95624751e9d98d1265e0a007e7d7a1186036750b.tar.xz ice-95624751e9d98d1265e0a007e7d7a1186036750b.zip |
Refactored string literal generation and updated identityToString format
Diffstat (limited to 'cpp/src/slice2cpp')
-rw-r--r-- | cpp/src/slice2cpp/Gen.cpp | 255 |
1 files changed, 2 insertions, 253 deletions
diff --git a/cpp/src/slice2cpp/Gen.cpp b/cpp/src/slice2cpp/Gen.cpp index 582f6edf732..978ab8114f2 100644 --- a/cpp/src/slice2cpp/Gen.cpp +++ b/cpp/src/slice2cpp/Gen.cpp @@ -13,8 +13,6 @@ #include <Slice/CPlusPlusUtil.h> #include <IceUtil/Functional.h> #include <IceUtil/Iterator.h> -#include <IceUtil/InputUtil.h> -#include <IceUtil/StringConverter.h> #include <IceUtil/StringUtil.h> #include <Slice/Checksum.h> #include <Slice/FileTracker.h> @@ -81,64 +79,6 @@ isConstexprType(const TypePtr& type) } } -string -u32CodePoint(unsigned int value, bool cpp11) -{ - ostringstream s; - // - // COMPILERFIX: - // With VC++ < 140 characters in the range of 0 to 0x9f cannot be represented - // with a universal character name (UCN). - // - if(!cpp11 && value <= 0x9f) - { - switch(value) - { - case 0x22: - { - s << "\\\""; - break; - } - case 0x5c: - { - s << "\\\\"; - break; - } - default: - { - s << "\\"; - s << oct; - s.width(3); - s.fill('0'); - s << value; - break; - } - } - } - // - // UCN valid characters - // - else - { - s << "\\U"; - s << hex; - s.width(8); - s.fill('0'); - s << value; - } - return s.str(); -} - -void -writeU8Buffer(const vector<unsigned char>& u8buffer, ::IceUtilInternal::Output& out, bool cpp11) -{ - vector<unsigned int> u32buffer = toUTF32(u8buffer); - - for(vector<unsigned int>::const_iterator c = u32buffer.begin(); c != u32buffer.end(); ++c) - { - out << u32CodePoint(*c, cpp11); - } -} string getDeprecateSymbol(const ContainedPtr& p1, const ContainedPtr& p2) @@ -171,207 +111,16 @@ writeConstantValue(IceUtilInternal::Output& out, const TypePtr& type, const Synt BuiltinPtr bp = BuiltinPtr::dynamicCast(type); if(bp && bp->kind() == Builtin::KindString) { - // - // Expand strings into the basic source character set. We can't use isalpha() and the like - // here because they are sensitive to the current locale. - // - static const string basicSourceChars = "abcdefghijklmnopqrstuvwxyz" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "0123456789" - "_{}[]#()<>%:;.?*+-/^&|~!=,\\\"' "; - static const set<char> charSet(basicSourceChars.begin(), basicSourceChars.end()); bool wide = (useWstring & TypeContextUseWstring) || findMetaData(metaData) == "wstring"; if(wide || cpp11) { - // - // Wide strings or C++11 narrow string - // - vector<unsigned char> u8buffer; // Buffer to convert multibyte characters out << (wide ? "L\"" : "u8\""); - for(size_t i = 0; i < value.size();) - { - if(charSet.find(value[i]) == charSet.end()) - { - if(static_cast<unsigned char>(value[i]) < 128) // Single byte character - { - out << u32CodePoint(static_cast<unsigned char>(value[i]), cpp11); - } - else - { - u8buffer.push_back(value[i]); - } - } - else - { - // - // Write any padding characters in the utf8 buffer - // - if(!u8buffer.empty()) - { - writeU8Buffer(u8buffer, out, cpp11); - u8buffer.clear(); - } - - switch(value[i]) - { - case '\\': - { - string s = "\\"; - size_t j = i + 1; - for(; j < value.size(); ++j) - { - if(value[j] != '\\') - { - break; - } - s += "\\"; - } - - // - // An even number of slash \ will escape the backslash and - // the codepoint will be interpreted as its characters - // - // \\U00000041 - ['\\', 'U', '0', '0', '0', '0', '0', '0', '4', '1'] - // \\\U00000041 - ['\\', 'A'] (41 is the codepoint for 'A') - // - if(s.size() % 2 != 0 && (value[j] == 'U' || value[j] == 'u')) - { - // - // Convert codepoint to UTF8 bytes and write the escaped bytes - // - out << s.substr(0, s.size() - 1); - - size_t sz = value[j] == 'U' ? 8 : 4; - string codepoint = value.substr(j + 1, sz); - assert(codepoint.size() == sz); - - IceUtil::Int64 v = IceUtilInternal::strToInt64(codepoint.c_str(), 0, 16); - out << u32CodePoint(static_cast<unsigned int>(v), cpp11); - - i = j + 1 + sz; - } - else - { - out << s; - i = j; - } - continue; - } - case '"': - { - out << "\\"; - break; - } - } - - out << value[i]; // Print normally if in basic source character set - } - i++; - - } - - // - // Write any padding characters in the utf8 buffer - // - if(!u8buffer.empty()) - { - writeU8Buffer(u8buffer, out, cpp11); - u8buffer.clear(); - } + out << toStringLiteral(value, "\a\b\f\n\r\t\v", "?", UCN, cpp11 ? 0 : 0x9F + 1); out << "\""; } else // C++98 narrow strings { - out << "\""; // Opening " - - for(size_t i = 0; i < value.size();) - { - if(charSet.find(value[i]) == charSet.end()) - { - unsigned char uc = value[i]; // char may be signed, so make it positive - ostringstream s; - s << "\\"; // Print as octal if not in basic source character set - s.width(3); - s.fill('0'); - s << oct; - s << static_cast<unsigned>(uc); - out << s.str(); - } - else - { - switch(value[i]) - { - case '\\': - { - string s = "\\"; - size_t j = i + 1; - for(; j < value.size(); ++j) - { - if(value[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 && (value[j] == 'U' || value[j] == 'u')) - { - // - // Convert codepoint to UTF8 bytes and write the escaped bytes - // - out << s.substr(0, s.size() - 1); - - size_t sz = value[j] == 'U' ? 8 : 4; - string codepoint = value.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 = fromUTF32(u32buffer); - - 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 << value[i]; // Print normally if in basic source character set - } - ++i; - } - out << "\""; // Closing " + out << "\"" << toStringLiteral(value, "\a\b\f\n\r\t\v", "?", Octal, 0) << "\""; } } else if(bp && bp->kind() == Builtin::KindLong) |