summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Hodge <tpg@ucc.asn.au>2017-04-16 11:40:20 +0800
committerJohn Hodge <tpg@ucc.asn.au>2017-04-16 11:40:20 +0800
commit2acb5a8ac29ddc6f6894802d255d686fdb18329d (patch)
treea52c35427e7416f3c43be83c21e7caa85aba96ae
parent8a12037b9f8c3a18c860e6f392ba9cebb3ed5aa6 (diff)
downloadmrust-2acb5a8ac29ddc6f6894802d255d686fdb18329d.tar.gz
common - Expand FmtEscaped
-rw-r--r--src/common.hpp15
-rw-r--r--src/main.cpp51
2 files changed, 53 insertions, 13 deletions
diff --git a/src/common.hpp b/src/common.hpp
index 63125b0e..d3aca257 100644
--- a/src/common.hpp
+++ b/src/common.hpp
@@ -328,19 +328,8 @@ struct FmtEscaped {
FmtEscaped(const ::std::string& s):
s(s.c_str())
{}
- friend ::std::ostream& operator<<(::std::ostream& os, const FmtEscaped& x) {
- for(auto s = x.s; *s != '\0'; s ++)
- {
- switch(*s)
- {
- case '\n': os << "\\n"; break;
- case '\\': os << "\\\\"; break;
- case '"': os << "\\\""; break;
- default: os << *s; break;
- }
- }
- return os;
- }
+ // See main.cpp
+ friend ::std::ostream& operator<<(::std::ostream& os, const FmtEscaped& x);
};
// -------------------------------------------------------------------
diff --git a/src/main.cpp b/src/main.cpp
index 2f392bb7..a9e4868b 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -717,3 +717,54 @@ ProgramParams::ProgramParams(int argc, char *argv[])
}
}
+
+::std::ostream& operator<<(::std::ostream& os, const FmtEscaped& x)
+{
+ os << ::std::hex;
+ for(auto s = x.s; *s != '\0'; s ++)
+ {
+ switch(*s)
+ {
+ case '\0': os << "\\0"; break;
+ case '\n': os << "\\n"; break;
+ case '\\': os << "\\\\"; break;
+ case '"': os << "\\\""; break;
+ default:
+ uint8_t v = *s;
+ if( v < 0x80 )
+ {
+ if( v < ' ' || v > 0x7F )
+ os << "\\u{" << ::std::hex << (unsigned int)v << "}";
+ else
+ os << v;
+ }
+ else if( v < 0xC0 )
+ ;
+ else if( v < 0xE0 )
+ {
+ uint32_t val = (uint32_t)(v & 0x1F) << 6;
+ v = (uint8_t)*++s; if( (v & 0xC0) != 0x80 ) { s--; continue ; } val |= (uint32_t)v << 6;
+ os << "\\u{" << ::std::hex << val << "}";
+ }
+ else if( v < 0xF0 )
+ {
+ uint32_t val = (uint32_t)(v & 0x0F) << 12;
+ v = (uint8_t)*++s; if( (v & 0xC0) != 0x80 ) { s--; continue ; } val |= (uint32_t)v << 12;
+ v = (uint8_t)*++s; if( (v & 0xC0) != 0x80 ) { s--; continue ; } val |= (uint32_t)v << 6;
+ os << "\\u{" << ::std::hex << val << "}";
+ }
+ else if( v < 0xF8 )
+ {
+ uint32_t val = (uint32_t)(v & 0x07) << 18;
+ v = (uint8_t)*++s; if( (v & 0xC0) != 0x80 ) { s--; continue ; } val |= (uint32_t)v << 18;
+ v = (uint8_t)*++s; if( (v & 0xC0) != 0x80 ) { s--; continue ; } val |= (uint32_t)v << 12;
+ v = (uint8_t)*++s; if( (v & 0xC0) != 0x80 ) { s--; continue ; } val |= (uint32_t)v << 6;
+ os << "\\u{" << ::std::hex << val << "}";
+ }
+ break;
+ }
+ }
+ os << ::std::dec;
+ return os;
+}
+