Skip to content

Commit 8b39444

Browse files
committed
cpp.unicode
1 parent 0d9563f commit 8b39444

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
cmake_minimum_required (VERSION 3.30)
2+
3+
project (main)
4+
5+
set (CMAKE_CXX_STANDARD 23)
6+
set (CMAKE_CXX_COMPILER g++-14)
7+
set (CMAKE_EXPORT_COMPILE_COMMANDS ON)
8+
9+
set (SOURCES
10+
main.cpp
11+
)
12+
13+
include_directories (
14+
/usr/local/include
15+
/opt/homebrew/include
16+
${CMAKE_CURRENT_SOURCE_DIR}
17+
)
18+
19+
link_directories (
20+
/usr/local/lib
21+
/opt/homebrew/lib
22+
)
23+
24+
add_executable (main
25+
${SOURCES}
26+
)

cpp.unicode/from.to.chars/main.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <iostream>
2+
#include <charconv>
3+
#include <cassert>
4+
5+
using namespace std;
6+
7+
int main(int argc, char *argv[])
8+
{
9+
u8string s2{u8"abc가나다"};
10+
cout << s2.length() << endl;
11+
12+
u16string s3{u"abc가나다"};
13+
cout << s3.length() << endl;
14+
15+
// 11을 10진수 문자열로 변환
16+
// 구조화된 바인딩. std::to_chars_result result{std::to_chars()} 와 동일
17+
char buf[10];
18+
auto [ptr0, ec0] = to_chars(buf, buf + sizeof(buf), 11, 10);
19+
if (ec0 == errc{})
20+
assert(string(buf, ptr0 - buf) == "11");
21+
22+
// 11을 16진수 문자열로 변환
23+
auto [ptr1, _] = to_chars(buf, buf + sizeof(buf), 11, 16);
24+
assert(string(buf, ptr1 - buf) == "b");
25+
26+
char str[]{"11year"}; // 숫자와 일반 문자로 구성됩니다.
27+
int result{0};
28+
29+
auto [ptr2, ec2] = from_chars(str, str + sizeof(str), result);
30+
if (ec2 == errc{})
31+
{
32+
// 숫자 부분만 잘 변환합니다.
33+
assert(result == 11);
34+
}
35+
36+
// ptr은 숫자 다음 위치입니다. 즉, 'y' 위치입니다.
37+
assert(ptr2 == &str[2]);
38+
39+
return 0;
40+
}

0 commit comments

Comments
 (0)