Skip to content

Commit 60435de

Browse files
committed
deploy: 34ea8ac
1 parent d28160b commit 60435de

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ <h2 id="_1">前言</h2>
292292
<blockquote>
293293
<p><img src="./img/bulb.png" height="30px" width="auto" style="margin: 0; border: none"/> 本书还在持续更新中……要追番的话,可以在 <a href="https://github.com/parallel101/cppguidebook">GitHub</a> 点一下右上角的 “Watch” 按钮,每当小彭老师提交新 commit,GitHub 会向你发送一封电子邮件,提醒你小彭老师更新了。</p>
294294
</blockquote>
295-
<p>更新时间:2025年02月26日 10:08:22 (UTC+08:00)</p>
295+
<p>更新时间:2025年02月26日 10:10:17 (UTC+08:00)</p>
296296
<p><a href="https://parallel101.github.io/cppguidebook">在 GitHub Pages 浏览本书</a> | <a href="https://142857.red/book">在小彭老师自己维护的镜像上浏览本书</a></p>
297297
<h2 id="_2">格式约定</h2>
298298
<blockquote>

print_page/index.html

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ <h2 id="index-_1">前言</h2>
421421
<blockquote>
422422
<p><img src="../img/bulb.png" height="30px" width="auto" style="margin: 0; border: none"/> 本书还在持续更新中……要追番的话,可以在 <a href="https://github.com/parallel101/cppguidebook">GitHub</a> 点一下右上角的 “Watch” 按钮,每当小彭老师提交新 commit,GitHub 会向你发送一封电子邮件,提醒你小彭老师更新了。</p>
423423
</blockquote>
424-
<p>更新时间:2025年02月26日 10:08:22 (UTC+08:00)</p>
424+
<p>更新时间:2025年02月26日 10:10:17 (UTC+08:00)</p>
425425
<p><a href="https://parallel101.github.io/cppguidebook">在 GitHub Pages 浏览本书</a> | <a href="https://142857.red/book">在小彭老师自己维护的镜像上浏览本书</a></p>
426426
<h2 id="index-_2">格式约定</h2>
427427
<blockquote>
@@ -16914,7 +16914,7 @@ <h3 id="unicode-utf-8_2">UTF-8 确实几乎完美支持字符串所有操作</h3
1691416914
// 会打印 “小�”
1691516915
</code></pre>
1691616916
<pre><code class="language-cpp">std::u32string s = U&quot;小彭老师公开课万岁&quot;;
16917-
fmt::println(&quot;UTF-32 下,前四个字符:{}&quot;, s.substr(0, 4));
16917+
fmt::println(&quot;UTF-32 下,前四个字符:{}&quot;, utf8::utf32to8(s.substr(0, 4)));
1691816918
// 会打印 “小彭老师”
1691916919
</code></pre>
1692016920
<p>只有当索引是来自 <code>find</code> 的结果时,UTF-8 字符串的切片才能正常工作:</p>
@@ -16925,7 +16925,7 @@ <h3 id="unicode-utf-8_2">UTF-8 确实几乎完美支持字符串所有操作</h3
1692516925
</code></pre>
1692616926
<pre><code class="language-cpp">std::u32string s = U&quot;小彭老师公开课万岁&quot;;
1692716927
size_t pos = s.find(U'公'); // pos = 4
16928-
fmt::println(&quot;UTF-32 下,“公”前的所有字符:{}&quot;, s.substr(0, pos));
16928+
fmt::println(&quot;UTF-32 下,“公”前的所有字符:{}&quot;, utf8::utf32to8(s.substr(0, pos)));
1692916929
// 会打印 “小彭老师”
1693016930
</code></pre>
1693116931
<blockquote>
@@ -16938,7 +16938,7 @@ <h3 id="unicode-utf-8_2">UTF-8 确实几乎完美支持字符串所有操作</h3
1693816938
// 也可能是乱码“�”,取决于终端理解的编码格式
1693916939
</code></pre>
1694016940
<pre><code class="language-cpp">std::u32string s = U&quot;小彭老师公开课万岁&quot;;
16941-
fmt::print(&quot;UTF-32 下第一个字符:{}&quot;, s[0]);
16941+
fmt::print(&quot;UTF-32 下第一个字符:{}&quot;, utf8::utf32to8(s.substr(0, 1)));
1694216942
// 会打印 ‘小’
1694316943
</code></pre>
1694416944
<p>UTF-8 字符串的反转也会出问题:</p>
@@ -17325,8 +17325,8 @@ <h3 id="unicode-utf-8_3">UTF-8 阵营</h3>
1732517325
<p>方法1:使用 <code>utf8to32</code> 一次性完成转换,用完后再转回去。</p>
1732617326
<pre><code class="language-cpp">std::string s = &quot;你好&quot;;
1732717327
std::u32string u32 = utf8::utf8to32(s);
17328-
fmt::println(&quot;U+{:04X}&quot;, u32[0]);
17329-
fmt::println(&quot;U+{:04X}&quot;, u32[1]);
17328+
fmt::println(&quot;U+{:04X}&quot;, static_cast&lt;std::uint32_t&gt;(u32[0]));
17329+
fmt::println(&quot;U+{:04X}&quot;, static_cast&lt;std::uint32_t&gt;(u32[1]));
1733017330
u32[1] = U'坏';
1733117331
s = utf8::utf32to8(u32);
1733217332
fmt::println(&quot;{}&quot;, s); // 你坏
@@ -17337,7 +17337,7 @@ <h3 id="unicode-utf-8_3">UTF-8 阵营</h3>
1733717337
utf8::unchecked::iterator&lt;char *&gt; eit(s + strlen(s));
1733817338
for (auto it = bit; it != eit; ++it) {
1733917339
// *it: char32_t
17340-
fmt::println(&quot;U+{:04X}&quot;, *it);
17340+
fmt::println(&quot;U+{:04X}&quot;, static_cast&lt;std::uint32_t&gt;(*it));
1734117341
}
1734217342

1734317343
// 安全(带边界检测)的版本
@@ -17346,7 +17346,7 @@ <h3 id="unicode-utf-8_3">UTF-8 阵营</h3>
1734617346
utf8::iterator&lt;char *&gt; eit(s + strlen(s), s, s + strlen(s));
1734717347
for (auto it = bit; it != eit; ++it) {
1734817348
// *it: char32_t
17349-
fmt::println(&quot;U+{:04X}&quot;, *it);
17349+
fmt::println(&quot;U+{:04X}&quot;, static_cast&lt;std::uint32_t&gt;(*it));
1735017350
}
1735117351

1735217352
// 基于 std::string 的版本
@@ -17355,7 +17355,7 @@ <h3 id="unicode-utf-8_3">UTF-8 阵营</h3>
1735517355
utf8::iterator&lt;std::string::iterator&gt; eit(s.end(), s.begin(), s.end());
1735617356
for (auto it = bit; it != eit; ++it) {
1735717357
// *it: char32_t
17358-
fmt::println(&quot;U+{:04X}&quot;, *it);
17358+
fmt::println(&quot;U+{:04X}&quot;, static_cast&lt;std::uint32_t&gt;(*it));
1735917359
}
1736017360
</code></pre>
1736117361
<p>由于迭代器接口复杂难懂,建议先封装成带有 <code>begin()</code> 和 <code>end()</code> 的 range 对象,方便使用 C++17 range-based loop 语法直观遍历:</p>
@@ -17378,7 +17378,7 @@ <h3 id="unicode-utf-8_3">UTF-8 阵营</h3>
1737817378
// 以下是新类的使用方法
1737917379
std::string s = &quot;你好&quot;;
1738017380
for (char32_t c : Utf8Range(s)) {
17381-
fmt::println(&quot;U+{:04X}&quot;, c);
17381+
fmt::println(&quot;U+{:04X}&quot;, static_cast&lt;std::uint32_t&gt;(c));
1738217382
}
1738317383
</code></pre>
1738417384
<h3 id="unicode-utf-16_2">UTF-16 阵营</h3>
@@ -17441,8 +17441,8 @@ <h3 id="unicode-utf-16_2">UTF-16 阵营</h3>
1744117441
<p>在刚刚介绍的 C++ 库 <code>utfcpp</code> 中,也有针对 UTF-16 的转换函数,如 <code>utf16to32</code>:</p>
1744217442
<pre><code class="language-cpp">std::u16string s = u&quot;你好&quot;;
1744317443
std::u32string u32 = utf16::utf16to32(s);
17444-
fmt::println(&quot;U+{:04X}&quot;, u32[0]);
17445-
fmt::println(&quot;U+{:04X}&quot;, u32[1]);
17444+
fmt::println(&quot;U+{:04X}&quot;, static_cast&lt;std::uint32_t&gt;(u32[0]));
17445+
fmt::println(&quot;U+{:04X}&quot;, static_cast&lt;std::uint32_t&gt;(u32[1]));
1744617446
u32[1] = U'𰻞';
1744717447
s = utf16::utf32to16(u32);
1744817448
fmt::println(&quot;{}&quot;, s); // 你𰻞

search/search_index.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

unicode/index.html

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ <h3 id="utf-8_2">UTF-8 确实几乎完美支持字符串所有操作</h3>
11921192
// 会打印 “小�”
11931193
</code></pre>
11941194
<pre><code class="language-cpp">std::u32string s = U&quot;小彭老师公开课万岁&quot;;
1195-
fmt::println(&quot;UTF-32 下,前四个字符:{}&quot;, s.substr(0, 4));
1195+
fmt::println(&quot;UTF-32 下,前四个字符:{}&quot;, utf8::utf32to8(s.substr(0, 4)));
11961196
// 会打印 “小彭老师”
11971197
</code></pre>
11981198
<p>只有当索引是来自 <code>find</code> 的结果时,UTF-8 字符串的切片才能正常工作:</p>
@@ -1203,7 +1203,7 @@ <h3 id="utf-8_2">UTF-8 确实几乎完美支持字符串所有操作</h3>
12031203
</code></pre>
12041204
<pre><code class="language-cpp">std::u32string s = U&quot;小彭老师公开课万岁&quot;;
12051205
size_t pos = s.find(U'公'); // pos = 4
1206-
fmt::println(&quot;UTF-32 下,“公”前的所有字符:{}&quot;, s.substr(0, pos));
1206+
fmt::println(&quot;UTF-32 下,“公”前的所有字符:{}&quot;, utf8::utf32to8(s.substr(0, pos)));
12071207
// 会打印 “小彭老师”
12081208
</code></pre>
12091209
<blockquote>
@@ -1216,7 +1216,7 @@ <h3 id="utf-8_2">UTF-8 确实几乎完美支持字符串所有操作</h3>
12161216
// 也可能是乱码“�”,取决于终端理解的编码格式
12171217
</code></pre>
12181218
<pre><code class="language-cpp">std::u32string s = U&quot;小彭老师公开课万岁&quot;;
1219-
fmt::print(&quot;UTF-32 下第一个字符:{}&quot;, s[0]);
1219+
fmt::print(&quot;UTF-32 下第一个字符:{}&quot;, utf8::utf32to8(s.substr(0, 1)));
12201220
// 会打印 ‘小’
12211221
</code></pre>
12221222
<p>UTF-8 字符串的反转也会出问题:</p>
@@ -1603,8 +1603,8 @@ <h3 id="utf-8_3">UTF-8 阵营</h3>
16031603
<p>方法1:使用 <code>utf8to32</code> 一次性完成转换,用完后再转回去。</p>
16041604
<pre><code class="language-cpp">std::string s = &quot;你好&quot;;
16051605
std::u32string u32 = utf8::utf8to32(s);
1606-
fmt::println(&quot;U+{:04X}&quot;, u32[0]);
1607-
fmt::println(&quot;U+{:04X}&quot;, u32[1]);
1606+
fmt::println(&quot;U+{:04X}&quot;, static_cast&lt;std::uint32_t&gt;(u32[0]));
1607+
fmt::println(&quot;U+{:04X}&quot;, static_cast&lt;std::uint32_t&gt;(u32[1]));
16081608
u32[1] = U'坏';
16091609
s = utf8::utf32to8(u32);
16101610
fmt::println(&quot;{}&quot;, s); // 你坏
@@ -1615,7 +1615,7 @@ <h3 id="utf-8_3">UTF-8 阵营</h3>
16151615
utf8::unchecked::iterator&lt;char *&gt; eit(s + strlen(s));
16161616
for (auto it = bit; it != eit; ++it) {
16171617
// *it: char32_t
1618-
fmt::println(&quot;U+{:04X}&quot;, *it);
1618+
fmt::println(&quot;U+{:04X}&quot;, static_cast&lt;std::uint32_t&gt;(*it));
16191619
}
16201620

16211621
// 安全(带边界检测)的版本
@@ -1624,7 +1624,7 @@ <h3 id="utf-8_3">UTF-8 阵营</h3>
16241624
utf8::iterator&lt;char *&gt; eit(s + strlen(s), s, s + strlen(s));
16251625
for (auto it = bit; it != eit; ++it) {
16261626
// *it: char32_t
1627-
fmt::println(&quot;U+{:04X}&quot;, *it);
1627+
fmt::println(&quot;U+{:04X}&quot;, static_cast&lt;std::uint32_t&gt;(*it));
16281628
}
16291629

16301630
// 基于 std::string 的版本
@@ -1633,7 +1633,7 @@ <h3 id="utf-8_3">UTF-8 阵营</h3>
16331633
utf8::iterator&lt;std::string::iterator&gt; eit(s.end(), s.begin(), s.end());
16341634
for (auto it = bit; it != eit; ++it) {
16351635
// *it: char32_t
1636-
fmt::println(&quot;U+{:04X}&quot;, *it);
1636+
fmt::println(&quot;U+{:04X}&quot;, static_cast&lt;std::uint32_t&gt;(*it));
16371637
}
16381638
</code></pre>
16391639
<p>由于迭代器接口复杂难懂,建议先封装成带有 <code>begin()</code><code>end()</code> 的 range 对象,方便使用 C++17 range-based loop 语法直观遍历:</p>
@@ -1656,7 +1656,7 @@ <h3 id="utf-8_3">UTF-8 阵营</h3>
16561656
// 以下是新类的使用方法
16571657
std::string s = &quot;你好&quot;;
16581658
for (char32_t c : Utf8Range(s)) {
1659-
fmt::println(&quot;U+{:04X}&quot;, c);
1659+
fmt::println(&quot;U+{:04X}&quot;, static_cast&lt;std::uint32_t&gt;(c));
16601660
}
16611661
</code></pre>
16621662
<h3 id="utf-16_2">UTF-16 阵营</h3>
@@ -1719,8 +1719,8 @@ <h3 id="utf-16_2">UTF-16 阵营</h3>
17191719
<p>在刚刚介绍的 C++ 库 <code>utfcpp</code> 中,也有针对 UTF-16 的转换函数,如 <code>utf16to32</code></p>
17201720
<pre><code class="language-cpp">std::u16string s = u&quot;你好&quot;;
17211721
std::u32string u32 = utf16::utf16to32(s);
1722-
fmt::println(&quot;U+{:04X}&quot;, u32[0]);
1723-
fmt::println(&quot;U+{:04X}&quot;, u32[1]);
1722+
fmt::println(&quot;U+{:04X}&quot;, static_cast&lt;std::uint32_t&gt;(u32[0]));
1723+
fmt::println(&quot;U+{:04X}&quot;, static_cast&lt;std::uint32_t&gt;(u32[1]));
17241724
u32[1] = U'𰻞';
17251725
s = utf16::utf32to16(u32);
17261726
fmt::println(&quot;{}&quot;, s); // 你𰻞

0 commit comments

Comments
 (0)