Skip to content

Commit 341a012

Browse files
committed
finslide
1 parent f1d5119 commit 341a012

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

15/08/a.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <string>
2+
#include <vector>
3+
#include <string_view>
4+
#include <iostream>
5+
6+
using namespace std;
7+
8+
struct Empty {
9+
};
10+
11+
struct NonEmpty {
12+
char c;
13+
};
14+
15+
struct DerivedEmpty : Empty {
16+
int i;
17+
};
18+
19+
struct DerivedNonEmpty : NonEmpty {
20+
int i;
21+
};
22+
23+
int main() {
24+
cout << sizeof(Empty) << endl; // 1 = 1(empty)
25+
cout << sizeof(NonEmpty) << endl; // 1 = 1(char c)
26+
cout << sizeof(DerivedEmpty) << endl; // 4 = 0(empty) + 4(int i)
27+
cout << sizeof(DerivedNonEmpty) << endl; // 8 = 1(char c) + 3(padding) + 4(int i)
28+
}

15/09/a.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <string>
2+
#include <iostream>
3+
#include <iomanip>
4+
#include <cstring>
5+
6+
using namespace std;
7+
8+
int main() {
9+
cout << "\x7a\u00df\u6c34\U0001f34c" << endl;
10+
{
11+
cout << "char: ";
12+
string s = "\x7a\u00df\u6c34\U0001f34c";
13+
for (size_t i = 0; i < s.size(); i++) {
14+
cout << setw(2) << setfill('0') << hex;
15+
cout << hex << (uint32_t)(uint8_t)s[i] << ' ';
16+
}
17+
cout << endl;
18+
}
19+
{
20+
cout << "wchar_t: ";
21+
wstring s = L"\x7a\u00df\u6c34\U0001f34c";
22+
for (size_t i = 0; i < s.size(); i++) {
23+
cout << setw(sizeof(wchar_t) * 2) << setfill('0') << hex;
24+
cout << (uint32_t)s[i] << ' ';
25+
}
26+
cout << endl;
27+
}
28+
//{
29+
//cout << "char8_t: ";
30+
//u8string s = u8"\x7a\u00df\u6c34\U0001f34c";
31+
//for (size_t i = 0; i < s.size(); i++) {
32+
//cout << setw(2) << setfill('0') << hex;
33+
//cout << (uint16_t)s[i] << ' ';
34+
//}
35+
//cout << endl;
36+
//}
37+
{
38+
cout << "char16_t: ";
39+
u16string s = u"\x7a\u00df\u6c34\U0001f34c";
40+
for (size_t i = 0; i < s.size(); i++) {
41+
cout << setw(4) << setfill('0') << hex;
42+
cout << (uint16_t)s[i] << ' ';
43+
}
44+
cout << endl;
45+
}
46+
{
47+
cout << "char32_t: ";
48+
u32string s = U"\x7a\u00df\u6c34\U0001f34c";
49+
for (size_t i = 0; i < s.size(); i++) {
50+
cout << setw(8) << setfill('0') << hex;
51+
cout << (uint32_t)s[i] << ' ';
52+
}
53+
cout << endl;
54+
}
55+
}

15/09/b.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <string>
2+
#include <string_view>
3+
#include <stdexcept>
4+
#include <iostream>
5+
#include <iomanip>
6+
#include <locale>
7+
8+
using namespace std;
9+
10+
template <class Internal = wchar_t>
11+
string convcodec(string_view src, locale const &srcLoc, locale const &dstLoc) {
12+
auto strCodecvtError = [] (codecvt_base::result res) -> string {
13+
if (res == codecvt_base::result::noconv)
14+
return "noconv";
15+
if (res == codecvt_base::result::error)
16+
return "error";
17+
if (res == codecvt_base::result::partial)
18+
return "partial";
19+
return "ok";
20+
};
21+
22+
string dst;
23+
basic_string<Internal> tmp;
24+
mbstate_t mbstate = {};
25+
26+
auto &srcFacet = use_facet<codecvt<Internal, char, mbstate_t>>(srcLoc);
27+
tmp.resize(src.size());
28+
29+
const char *src_begin = src.data();
30+
const char *src_end = src.data() + src.size();
31+
const char *src_next;
32+
Internal *tmp_begin = tmp.data();
33+
Internal *tmp_end = tmp.data() + tmp.size();
34+
Internal *tmp_next;
35+
auto resIn = srcFacet.in(mbstate, src_begin, src_end, src_next, tmp_begin, tmp_end, tmp_next);
36+
if (resIn != codecvt_base::result::ok)
37+
throw runtime_error("cannot decode input sequence: " + strCodecvtError(resIn));
38+
size_t tmp_size = tmp_next - tmp_begin;
39+
tmp_end = tmp_next;
40+
41+
auto &dstFacet = use_facet<codecvt<Internal, char, mbstate_t>>(dstLoc);
42+
dst.resize(tmp_size * dstFacet.max_length());
43+
44+
char *dst_begin = dst.data();
45+
char *dst_end = dst.data() + dst.size();
46+
char *dst_next;
47+
const Internal *tmp_next_next;
48+
auto resOut = dstFacet.out(mbstate, tmp_begin, tmp_end, tmp_next_next, dst_begin, dst_end, dst_next);
49+
if (resOut != codecvt_base::result::ok)
50+
throw runtime_error("cannot encode output sequence: " + strCodecvtError(resOut));
51+
size_t dst_size = dst_next - dst_begin;
52+
53+
dst.resize(dst_size);
54+
return dst;
55+
}
56+
57+
void showstr(string_view s) {
58+
for (char c: s) {
59+
cout << hex << setfill('0') << setw(2);
60+
cout << (uint32_t)(uint8_t)c << ' ';
61+
}
62+
cout << s << endl;
63+
}
64+
65+
int main() {
66+
string s = "你好";
67+
//string s = "你好\U0001f34c"; // error: cannot encode U+1F34C
68+
showstr(s);
69+
s = convcodec(s, locale("en_US.UTF-8"), locale("zh_CN.GBK"));
70+
showstr(s);
71+
}

15/slides.pptx

8.4 MB
Binary file not shown.

0 commit comments

Comments
 (0)