Skip to content

Commit 39f8ee9

Browse files
committed
feat(cpp14): add 06-digit-separators
Book chapter (zh + en): - section 一/I: Basic usage — decimal, hex, binary with separators - section 二/II: Real-world case — UTF-8 continuation byte mask 0b11'1111u from msvc-stl/stl/inc/format#L281-L282 - section 三/III: Notes — start/end/consecutive restrictions - section 四/IV: Exercise topics and d2x checker command - section 五/V: External resources Exercise: 0. Digit separators in decimal, hex, and binary. 3 D2X_YOUR_ANSWER. Build wiring: - register 06 target in dslings/cpp14, dslings/en/cpp14, solutions/cpp14 - add 06 entry to zh/en SUMMARY.md
1 parent 1c37ddd commit 39f8ee9

10 files changed

Lines changed: 255 additions & 0 deletions

File tree

book/en/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
# C++14 Core Language Features
3232

3333
- [Generic Lambdas](./cpp14/00-generic-lambdas.md)
34+
- [Digit Separators](./cpp14/06-digit-separators.md)
3435

3536
# Additional Resources
3637

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<div align=right>
2+
3+
🌎 [中文] | [English]
4+
</div>
5+
6+
[中文]: ../../cpp14/06-digit-separators.html
7+
[English]: ./06-digit-separators.html
8+
9+
# Digit Separators
10+
11+
C++14 allows single quotes `'` as separators in numeric literals, improving readability without affecting the value
12+
13+
| Book | Video | Code | X |
14+
| --- | --- | --- | --- |
15+
| [cppreference-integer_literal](https://en.cppreference.com/w/cpp/language/integer_literal) / [markdown](https://github.com/mcpp-community/d2mcpp/blob/main/book/en/src/cpp14/06-digit-separators.md) | [Video Explanation]() | [Exercise Code](https://github.com/mcpp-community/d2mcpp/blob/main/dslings/en/cpp14/06-digit-separators-0.cpp) | |
16+
17+
18+
**Why introduced?**
19+
20+
`1000000000` is hard to parse, `0xFFFF0000` requires counting Fs. `1'000'000'000` and `0xFFFF'0000` make the numeric structure clear at a glance
21+
22+
## I. Basic Usage and Scenarios
23+
24+
```cpp
25+
int million = 1'000'000; // decimal
26+
int hex_val = 0xFF'FF'00'00; // hexadecimal
27+
int bin_val = 0b1010'1100'1111; // binary + separator
28+
29+
static_assert(million == 1000000, "");
30+
```
31+
32+
## II. Real-World Case — Digit Separators in the STL
33+
34+
> The MSVC STL uses binary literals with digit separators in UTF-8 decoding. The example below cites the vendored [MSVC STL](https://github.com/mcpp-community/d2mcpp/tree/main/msvc-stl) (source: [`msvc-stl/stl/inc/format`](https://github.com/mcpp-community/d2mcpp/blob/main/msvc-stl/stl/inc/format#L281-L282))
35+
36+
```cpp
37+
// MSVC STL · msvc-stl/stl/inc/format (abridged)
38+
// UTF-8 decoding — extract the lower 6 bits of each continuation byte
39+
_Val = (_Val << 6) | (static_cast<unsigned char>(_First[_Idx]) & 0b11'1111u);
40+
```
41+
42+
`0b11'1111u` is 63, and the `'` separator makes the binary mask's semantics directly visible in the literal — no mental conversion from `0x3F` or `63`
43+
44+
## III. Notes
45+
46+
- Cannot appear at the start or end: `'0` / `1'` are invalid
47+
- Cannot be consecutive: `1''0` is invalid
48+
- Does not affect the value, only improves readability
49+
50+
## IV. Exercise Code
51+
52+
### Exercise Topics
53+
54+
- 0 - [Digit Separators — Large Numbers and Radix Combinations](https://github.com/mcpp-community/d2mcpp/blob/main/dslings/en/cpp14/06-digit-separators-0.cpp)
55+
56+
### Auto-Checker Command
57+
58+
```
59+
d2x checker digit-separators
60+
```
61+
62+
## V. Other
63+
64+
- [Discussion Forum](https://forum.d2learn.org/category/20)
65+
- [d2mcpp Tutorial Repository](https://github.com/mcpp-community/d2mcpp)
66+
- [Tutorial Video List](https://space.bilibili.com/65858958/lists/5208246)
67+
- [Tutorial Support Tool - xlings](https://github.com/openxlings/xlings)

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
# C++14核心语言特性
3232

3333
- [泛型 lambda - generic lambdas](./cpp14/00-generic-lambdas.md)
34+
- [数字分隔符 - digit separators](./cpp14/06-digit-separators.md)
3435

3536
# 其他
3637

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<div align=right>
2+
3+
🌎 [中文] | [English]
4+
</div>
5+
6+
[中文]: ./06-digit-separators.html
7+
[English]: ../en/cpp14/06-digit-separators.html
8+
9+
# 数字分隔符 - digit separators
10+
11+
C++14 允许在数字字面量中使用单引号 `'` 作为分隔符, 不影响数值, 显著提升大数可读性
12+
13+
| Book | Video | Code | X |
14+
| --- | --- | --- | --- |
15+
| [cppreference-integer_literal](https://en.cppreference.com/w/cpp/language/integer_literal) / [markdown](https://github.com/mcpp-community/d2mcpp/blob/main/book/src/cpp14/06-digit-separators.md) | [视频解读]() | [练习代码](https://github.com/mcpp-community/d2mcpp/blob/main/dslings/cpp14/06-digit-separators-0.cpp) | |
16+
17+
18+
**为什么引入?**
19+
20+
`1000000000` 很难分清位数, `0xFFFF0000` 需要仔细数几位 F。`1'000'000'000``0xFFFF'0000` 让数值结构一目了然
21+
22+
## 一、基础用法和场景
23+
24+
```cpp
25+
int million = 1'000'000; // 十进制
26+
int hex_val = 0xFF'FF'00'00; // 十六进制
27+
int bin_val = 0b1010'1100'1111; // 二进制 + 分隔符
28+
29+
static_assert(million == 1000000, "");
30+
```
31+
32+
分隔符可以出现在数字中任意位置, 不限制分组位数
33+
34+
## 二、真实案例 - STL 中的数字分隔符
35+
36+
> MSVC STL 在 UTF-8 解码中使用二进制字面量配合数字分隔符。下面以仓库内置的 [MSVC STL](https://github.com/mcpp-community/d2mcpp/tree/main/msvc-stl) 为例 (源码: [`msvc-stl/stl/inc/format`](https://github.com/mcpp-community/d2mcpp/blob/main/msvc-stl/stl/inc/format#L281-L282))
37+
38+
```cpp
39+
// MSVC STL · msvc-stl/stl/inc/format (有删节)
40+
// UTF-8 解码 — 提取连续字节的低 6 位
41+
_Val = (_Val << 6) | (static_cast<unsigned char>(_First[_Idx]) & 0b11'1111u);
42+
```
43+
44+
`0b11'1111u` 是 63, 用 `'` 将高位和低位分开后, 二进制掩码的语义直接从字面量可见, 无需心算 `0x3F``63`
45+
46+
## 三、注意事项
47+
48+
- 不能出现在数字开头或结尾: `'0` / `1'` 非法
49+
- 不能连续: `1''0` 非法
50+
- 不影响数值, 仅增强可读性
51+
52+
## 四、练习代码
53+
54+
### 练习代码主题
55+
56+
- 0 - [数字分隔符 — 大数与进制组合](https://github.com/mcpp-community/d2mcpp/blob/main/dslings/cpp14/06-digit-separators-0.cpp)
57+
58+
### 练习代码自动检测命令
59+
60+
```
61+
d2x checker digit-separators
62+
```
63+
64+
## 五、其他
65+
66+
- [交流讨论](https://forum.d2learn.org/category/20)
67+
- [d2mcpp教程仓库](https://github.com/mcpp-community/d2mcpp)
68+
- [教程视频列表](https://space.bilibili.com/65858958/lists/5208246)
69+
- [教程支持工具-xlings](https://github.com/openxlings/xlings)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// d2mcpp: https://github.com/mcpp-community/d2mcpp
2+
// license: Apache-2.0
3+
// file: dslings/cpp14/06-digit-separators-0.cpp
4+
//
5+
// Exercise/练习: cpp14 | 06 - digit separators | 数字分隔符
6+
//
7+
// Tips/提示:
8+
// - 单引号 ' 分隔数字, 不影响数值
9+
// - 可用于十进制、十六进制、二进制字面量
10+
//
11+
// Docs/文档:
12+
// - https://en.cppreference.com/w/cpp/language/integer_literal
13+
// - https://github.com/mcpp-community/d2mcpp/blob/main/book/src/cpp14/06-digit-separators.md
14+
//
15+
// 练习交流讨论: http://forum.d2learn.org/category/20
16+
//
17+
// Auto-Checker/自动检测命令:
18+
//
19+
// d2x checker digit-separators
20+
//
21+
22+
#include <d2x/cpp/common.hpp>
23+
24+
int main() {
25+
26+
int million = 1'D2X_YOUR_ANSWER'000;
27+
d2x_assert_eq(million, 1000000);
28+
29+
int hex_color = 0xFF'D2X_YOUR_ANSWER'BB;
30+
d2x_assert_eq(hex_color, 0xFFAABB);
31+
32+
int bin_val = 0b1010'D2X_YOUR_ANSWER;
33+
d2x_assert_eq(bin_val, 0b10101100);
34+
35+
D2X_WAIT
36+
37+
return 0;
38+
}

dslings/cpp14/xmake.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ target("cpp14-00-generic-lambdas-0")
99
target("cpp14-00-generic-lambdas-1")
1010
set_kind("binary")
1111
add_files("00-generic-lambdas-1.cpp")
12+
13+
-- target: cpp14-06-digit-separators
14+
15+
target("cpp14-06-digit-separators-0")
16+
set_kind("binary")
17+
add_files("06-digit-separators-0.cpp")
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// d2mcpp: https://github.com/mcpp-community/d2mcpp
2+
// license: Apache-2.0
3+
// file: dslings/en/cpp14/06-digit-separators-0.cpp
4+
//
5+
// Exercise: cpp14 | 06 - digit separators
6+
//
7+
// Tips:
8+
// - Single quotes ' separate digits without affecting the value
9+
// - Works with decimal, hex, and binary literals
10+
//
11+
// Docs:
12+
// - https://en.cppreference.com/w/cpp/language/integer_literal
13+
// - https://github.com/mcpp-community/d2mcpp/blob/main/book/en/src/cpp14/06-digit-separators.md
14+
//
15+
// Discussion Forum: http://forum.d2learn.org/category/20
16+
//
17+
// Auto-Checker:
18+
//
19+
// d2x checker digit-separators
20+
//
21+
22+
#include <d2x/cpp/common.hpp>
23+
24+
int main() {
25+
26+
int million = 1'D2X_YOUR_ANSWER'000;
27+
d2x_assert_eq(million, 1000000);
28+
29+
int hex_color = 0xFF'D2X_YOUR_ANSWER'BB;
30+
d2x_assert_eq(hex_color, 0xFFAABB);
31+
32+
int bin_val = 0b1010'D2X_YOUR_ANSWER;
33+
d2x_assert_eq(bin_val, 0b10101100);
34+
35+
D2X_WAIT
36+
37+
return 0;
38+
}

dslings/en/cpp14/xmake.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ target("cpp14-00-generic-lambdas-0")
99
target("cpp14-00-generic-lambdas-1")
1010
set_kind("binary")
1111
add_files("00-generic-lambdas-1.cpp")
12+
13+
-- target: cpp14-06-digit-separators
14+
15+
target("cpp14-06-digit-separators-0")
16+
set_kind("binary")
17+
add_files("06-digit-separators-0.cpp")
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// d2mcpp: https://github.com/mcpp-community/d2mcpp
2+
// license: Apache-2.0
3+
// reference solution for: dslings/cpp14/06-digit-separators-0.cpp
4+
//
5+
// 用途: 仅给 CI 与维护者参考使用,不是教程入口。
6+
// 教程练习入口: dslings/cpp14/06-digit-separators-0.cpp
7+
//
8+
9+
#include <d2x/cpp/common.hpp>
10+
11+
int main() {
12+
13+
int million = 1'000'000;
14+
d2x_assert_eq(million, 1000000);
15+
16+
int hex_color = 0xFF'AA'BB;
17+
d2x_assert_eq(hex_color, 0xFFAABB);
18+
19+
int bin_val = 0b1010'1100;
20+
d2x_assert_eq(bin_val, 0b10101100);
21+
22+
return 0;
23+
}

solutions/cpp14/xmake.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ target("cpp14-00-generic-lambdas-0-ref")
99
target("cpp14-00-generic-lambdas-1-ref")
1010
set_kind("binary")
1111
add_files("00-generic-lambdas-1.cpp")
12+
13+
-- target: cpp14-06-digit-separators
14+
15+
target("cpp14-06-digit-separators-0-ref")
16+
set_kind("binary")
17+
add_files("06-digit-separators-0.cpp")

0 commit comments

Comments
 (0)