MCPcopy Create free account
hub / github.com/Kitware/CMake / testRobustEncoding

Function testRobustEncoding

Source/kwsys/testEncoding.cxx:73–129  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

71}
72
73static int testRobustEncoding()
74{
75 // test that the conversion functions handle invalid
76 // unicode correctly/gracefully
77
78 // we manipulate the format flags of stdout, remember
79 // the original state here to restore before return
80 std::ios::fmtflags const& flags = std::cout.flags();
81
82 int ret = 0;
83 char cstr[] = { static_cast<char>(-1), 0 };
84 // this conversion could fail
85 std::wstring wstr = kwsys::Encoding::ToWide(cstr);
86
87 wstr = kwsys::Encoding::ToWide(nullptr);
88 if (!wstr.empty()) {
89 wchar_t const* wcstr = wstr.c_str();
90 std::cout << "ToWide(NULL) returned";
91 for (size_t i = 0; i < wstr.size(); i++) {
92 std::cout << " " << std::hex << static_cast<int>(wcstr[i]);
93 }
94 std::cout << std::endl;
95 ret++;
96 }
97 wstr = kwsys::Encoding::ToWide("");
98 if (!wstr.empty()) {
99 wchar_t const* wcstr = wstr.c_str();
100 std::cout << "ToWide(\"\") returned";
101 for (size_t i = 0; i < wstr.size(); i++) {
102 std::cout << " " << std::hex << static_cast<int>(wcstr[i]);
103 }
104 std::cout << std::endl;
105 ret++;
106 }
107
108#ifdef _WIN32
109 // 16 bit wchar_t - we make an invalid surrogate pair
110 wchar_t cwstr[] = { 0xD801, 0xDA00, 0 };
111 // this conversion could fail
112 std::string win_str = kwsys::Encoding::ToNarrow(cwstr);
113#endif
114
115 std::string str = kwsys::Encoding::ToNarrow(nullptr);
116 if (!str.empty()) {
117 std::cout << "ToNarrow(NULL) returned " << str << std::endl;
118 ret++;
119 }
120
121 str = kwsys::Encoding::ToNarrow(L"");
122 if (!wstr.empty()) {
123 std::cout << "ToNarrow(\"\") returned " << str << std::endl;
124 ret++;
125 }
126
127 std::cout.flags(flags);
128 return ret;
129}
130

Callers 1

testEncodingFunction · 0.85

Calls 4

flagsMethod · 0.80
c_strMethod · 0.80
emptyMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…