Skip to content

Commit e50e3c6

Browse files
book: formatting style updates (#231)
1 parent 41b88c8 commit e50e3c6

10 files changed

+106
-76
lines changed

book/en-us/02-usability.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ int main() {
214214
}
215215

216216
// should output: 1, 4, 3, 4. can be simplified using `auto`
217-
for (std::vector<int>::iterator element = vec.begin(); element != vec.end(); ++element)
217+
for (std::vector<int>::iterator element = vec.begin(); element != vec.end();
218+
++element)
218219
std::cout << *element << std::endl;
219220
}
220221
```
@@ -301,7 +302,8 @@ int main() {
301302
MagicFoo magicFoo = {1, 2, 3, 4, 5};
302303

303304
std::cout << "magicFoo: ";
304-
for (std::vector<int>::iterator it = magicFoo.vec.begin(); it != magicFoo.vec.end(); ++it)
305+
for (std::vector<int>::iterator it = magicFoo.vec.begin();
306+
it != magicFoo.vec.end(); ++it)
305307
std::cout << *it << std::endl;
306308
}
307309
```

book/en-us/03-runtime.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,8 @@ int foo(int a, int b, int c) {
221221
;
222222
}
223223
int main() {
224-
// bind parameter 1, 2 on function foo, and use std::placeholders::_1 as placeholder
225-
// for the first parameter.
224+
// bind parameter 1, 2 on function foo,
225+
// and use std::placeholders::_1 as placeholder for the first parameter.
226226
auto bindFoo = std::bind(foo, std::placeholders::_1, 1,2);
227227
// when call bindFoo, we only need one param left
228228
bindFoo(1);
@@ -355,7 +355,8 @@ int main()
355355
std::string&& rv1 = std::move(lv1); // legal, std::move can convert lvalue to rvalue
356356
std::cout << rv1 << std::endl; // string,
357357

358-
const std::string& lv2 = lv1 + lv1; // legal, const lvalue reference can extend temp variable's lifecycle
358+
const std::string& lv2 = lv1 + lv1; // legal, const lvalue reference can
359+
// extend temp variable's lifecycle
359360
// lv2 += "Test"; // illegal, const ref can't be modified
360361
std::cout << lv2 << std::endl; // string,string,
361362

@@ -482,8 +483,9 @@ int main() {
482483
// "str: Hello world."
483484
std::cout << "str: " << str << std::endl;
484485

485-
// use push_back(const T&&), no copy
486-
// the string will be moved to vector, and therefore std::move can reduce copy cost
486+
// use push_back(const T&&),
487+
// no copy the string will be moved to vector,
488+
// and therefore std::move can reduce copy cost
487489
v.push_back(std::move(str));
488490
// str is empty now
489491
std::cout << "str: " << str << std::endl;

book/en-us/05-pointers.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,35 +57,35 @@ And see the reference count of an object by `use_count()`. E.g:
5757
auto pointer = std::make_shared<int>(10);
5858
auto pointer2 = pointer; // reference count+1
5959
auto pointer3 = pointer; // reference count+1
60-
int *p = pointer.get(); // no increase of reference count
60+
int *p = pointer.get(); // no increase of reference count
6161
62-
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; // 3
62+
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; // 3
6363
std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl; // 3
6464
std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl; // 3
6565
6666
pointer2.reset();
6767
std::cout << "reset pointer2:" << std::endl;
6868
69-
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; // 2
69+
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; // 2
7070
std::cout << "pointer2.use_count() = "
71-
<< pointer2.use_count() << std::endl; // 0, pointer2 has reset
71+
<< pointer2.use_count() << std::endl; // pointer2 has reset, 0
7272
std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl; // 2
7373
7474
pointer3.reset();
7575
std::cout << "reset pointer3:" << std::endl;
7676
77-
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; // 1
77+
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; // 1
7878
std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl; // 0
7979
std::cout << "pointer3.use_count() = "
80-
<< pointer3.use_count() << std::endl; // 0, pointer3 has reset
80+
<< pointer3.use_count() << std::endl; // pointer3 has reset, 0
8181
```
8282

8383
## 5.3 `std::unique_ptr`
8484

8585
`std::unique_ptr` is an exclusive smart pointer that prohibits other smart pointers from sharing the same object, thus keeping the code safe:
8686

8787
```cpp
88-
std::unique_ptr<int> pointer = std::make_unique<int>(10); // make_unique was introduced in C++14
88+
std::unique_ptr<int> pointer = std::make_unique<int>(10); // make_unique, from C++14
8989
std::unique_ptr<int> pointer2 = pointer; // illegal
9090
```
9191

book/en-us/06-regex.md

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ We use a simple example to briefly introduce the use of this library. Consider t
8484

8585
int main() {
8686
std::string fnames[] = {"foo.txt", "bar.txt", "test", "a0.txt", "AAA.txt"};
87-
// In C++, `\` will be used as an escape character in the string. In order for `\.`
88-
// to be passed as a regular expression, it is necessary to perform second escaping of `\`, thus we have `\\.`
87+
// In C++, `\` will be used as an escape character in the string.
88+
// In order for `\.` to be passed as a regular expression,
89+
// it is necessary to perform second escaping of `\`, thus we have `\\.`
8990
std::regex txt_regex("[a-z]+\\.txt");
9091
for (const auto &fname: fnames)
9192
std::cout << fname << ": " << std::regex_match(fname, txt_regex) << std::endl;
@@ -104,7 +105,8 @@ std::smatch base_match;
104105
for(const auto &fname: fnames) {
105106
if (std::regex_match(fname, base_match, base_regex)) {
106107
// the first element of std::smatch matches the entire string
107-
// the second element of std::smatch matches the first expression with brackets
108+
// the second element of std::smatch matches the first expression
109+
// with brackets
108110
if (base_match.size() == 2) {
109111
std::string base = base_match[1].str();
110112
std::cout << "sub-match[0]: " << base_match[0].str() << std::endl;
@@ -187,32 +189,36 @@ Please implement the member functions `start()` and `parse_request`. Enable serv
187189
template<typename SERVER_TYPE>
188190
void start_server(SERVER_TYPE &server) {
189191
190-
// process GET request for /match/[digit+numbers], e.g.
191-
// GET request is /match/abc123, will return abc123
192-
server.resource["fill_your_reg_ex"]["GET"] = [](ostream& response, Request& request) {
192+
// process GET request for /match/[digit+numbers],
193+
// e.g. GET request is /match/abc123, will return abc123
194+
server.resource["fill_your_reg_ex"]["GET"] =
195+
[](ostream& response, Request& request)
196+
{
193197
string number=request.path_match[1];
194198
response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length()
195199
<< "\r\n\r\n" << number;
196200
};
197201
198-
// peocess default GET request; anonymous function will be called if no other matches
199-
// response files in folder web/
202+
// peocess default GET request;
203+
// anonymous function will be called
204+
// if no other matches response files in folder web/
200205
// default: index.html
201206
server.default_resource["fill_your_reg_ex"]["GET"] =
202-
[](ostream& response, Request& request) {
203-
string filename = "www/";
204-
205-
string path = request.path_match[1];
206-
207-
// forbidden use `..` access content outside folder web/
208-
size_t last_pos = path.rfind(".");
209-
size_t current_pos = 0;
210-
size_t pos;
211-
while((pos=path.find('.', current_pos)) != string::npos && pos != last_pos) {
212-
current_pos = pos;
213-
path.erase(pos, 1);
214-
last_pos--;
215-
}
207+
[](ostream& response, Request& request)
208+
{
209+
string filename = "www/";
210+
211+
string path = request.path_match[1];
212+
213+
// forbidden use `..` access content outside folder web/
214+
size_t last_pos = path.rfind(".");
215+
size_t current_pos = 0;
216+
size_t pos;
217+
while((pos=path.find('.', current_pos)) != string::npos && pos != last_pos) {
218+
current_pos = pos;
219+
path.erase(pos, 1);
220+
last_pos--;
221+
}
216222
217223
// (...)
218224
};

book/en-us/07-thread.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ int main() {
145145
std::cout << "waiting...";
146146
result.wait(); // block until future has arrived
147147
// output result
148-
std::cout << "done!" << std:: endl << "future result is " << result.get() << std::endl;
148+
std::cout << "done!" << std:: endl << "future result is "
149+
<< result.get() << std::endl;
149150
return 0;
150151
}
151152
```
@@ -196,7 +197,8 @@ int main() {
196197
// temporal unlock to allow producer produces more rather than
197198
// let consumer hold the lock until its consumed.
198199
lock.unlock();
199-
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // consumer is slower
200+
// consumer is slower
201+
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
200202
lock.lock();
201203
if (!produced_nums.empty()) {
202204
std::cout << "consuming " << produced_nums.front() << std::endl;
@@ -272,7 +274,7 @@ This is a very strong set of synchronization conditions, in other words when it
272274
This seems too harsh for a variable that requires only atomic operations (no intermediate state).
273275

274276
The research on synchronization conditions has a very long history, and we will not go into details here. Readers should understand that under the modern CPU architecture, atomic operations at the CPU instruction level are provided.
275-
Therefore, in the C++11 multi-threaded shared variable reading and writing, the introduction of the `std::atomic` template, so that we instantiate an atomic type, will be a
277+
Therefore, in the C++11 multi-threaded shared variable reading and writing, the introduction of the `std::atomic` template, so that we instantiate an atomic type, will be an
276278
Atomic type read and write operations are minimized from a set of instructions to a single CPU instruction. E.g:
277279

278280
```cpp
@@ -417,8 +419,11 @@ Weakening the synchronization conditions between processes, usually we will cons
417419
```
418420
3 4 4 4 // The write operation of x was quickly observed
419421
0 3 3 4 // There is a delay in the observed time of the x write operation
420-
0 0 0 4 // The last read read the final value of x, but the previous changes were not observed.
421-
0 0 0 0 // The write operation of x is not observed in the current time period, but the situation that x is 4 can be observed at some point in the future.
422+
0 0 0 4 // The last read read the final value of x,
423+
// but the previous changes were not observed.
424+
0 0 0 0 // The write operation of x is not observed in the current time period,
425+
// but the situation that x is 4 can be observed
426+
// at some point in the future.
422427
```
423428
424429
### Memory Orders
@@ -480,9 +485,8 @@ To achieve the ultimate performance and achieve consistency of various strength
480485
});
481486
std::thread acqrel([&]() {
482487
int expected = 1; // must before compare_exchange_strong
483-
while(!flag.compare_exchange_strong(expected, 2, std::memory_order_acq_rel)) {
488+
while(!flag.compare_exchange_strong(expected, 2, std::memory_order_acq_rel))
484489
expected = 1; // must after compare_exchange_strong
485-
}
486490
// flag has changed to 2
487491
});
488492
std::thread acquire([&]() {

book/zh-cn/02-usability.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ int main() {
176176
}
177177

178178
// 将输出 1, 4, 3, 4
179-
for (std::vector<int>::iterator element = vec.begin(); element != vec.end(); ++element)
179+
for (std::vector<int>::iterator element = vec.begin(); element != vec.end();
180+
++element)
180181
std::cout << *element << std::endl;
181182
}
182183
```
@@ -228,7 +229,7 @@ int main() {
228229
}
229230
```
230231

231-
为了解决这个问题,C++11 首先把初始化列表的概念绑定到了类型上,并将其称之为 `std::initializer_list`,允许构造函数或其他函数像参数一样使用初始化列表,这就为类对象的初始化与普通数组和 POD 的初始化方法提供了统一的桥梁,例如:
232+
为解决这个问题,C++11 首先把初始化列表的概念绑定到类型上,称其为 `std::initializer_list`,允许构造函数或其他函数像参数一样使用初始化列表,这就为类对象的初始化与普通数组和 POD 的初始化方法提供了统一的桥梁,例如:
232233

233234
```cpp
234235
#include <initializer_list>
@@ -249,7 +250,9 @@ int main() {
249250
MagicFoo magicFoo = {1, 2, 3, 4, 5};
250251

251252
std::cout << "magicFoo: ";
252-
for (std::vector<int>::iterator it = magicFoo.vec.begin(); it != magicFoo.vec.end(); ++it) std::cout << *it << std::endl;
253+
for (std::vector<int>::iterator it = magicFoo.vec.begin();
254+
it != magicFoo.vec.end(); ++it)
255+
std::cout << *it << std::endl;
253256
}
254257
```
255258

book/zh-cn/03-runtime.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Lambda 表达式的本质是一个和函数对象类型相似的类类型(称
136136
#include <iostream>
137137
138138
using foo = void(int); // 定义函数类型, using 的使用见上一节中的别名语法
139-
void functional(foo f) { // 定义在参数列表中的函数类型 foo 被视为退化后的函数指针类型 foo*
139+
void functional(foo f) { // 参数列表中定义的函数类型 foo 被视为退化后的函数指针类型 foo*
140140
f(1); // 通过函数指针调用函数
141141
}
142142
@@ -193,7 +193,8 @@ int foo(int a, int b, int c) {
193193
;
194194
}
195195
int main() {
196-
// 将参数1,2绑定到函数 foo 上,但是使用 std::placeholders::_1 来对第一个参数进行占位
196+
// 将参数1,2绑定到函数 foo 上,
197+
// 但使用 std::placeholders::_1 来对第一个参数进行占位
197198
auto bindFoo = std::bind(foo, std::placeholders::_1, 1,2);
198199
// 这时调用 bindFoo 时,只需要提供第一个参数即可
199200
bindFoo(1);
@@ -551,7 +552,7 @@ constexpr _Tp&& forward(typename std::remove_reference<_Tp>::type&& __t) noexcep
551552
```
552553
553554
在这份实现中,`std::remove_reference` 的功能是消除类型中的引用,
554-
`std::is_lvalue_reference` 用于检查类型推导是否正确,在 `std::forward` 的第二个实现中
555+
`std::is_lvalue_reference` 则用于检查类型推导是否正确,在 `std::forward` 的第二个实现中
555556
检查了接收到的值确实是一个左值,进而体现了坍缩规则。
556557
557558
当 `std::forward` 接受左值时,`_Tp` 被推导为左值,所以返回值为左值;而当其接受右值时,

book/zh-cn/05-pointers.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ order: 5
1919
也就是我们常说的 RAII 资源获取即初始化技术。
2020

2121
凡事都有例外,我们总会有需要将对象在自由存储上分配的需求,在传统 C++ 里我们只好使用 `new``delete`
22-
『记得』对资源进行释放。而 C++11 引入智能指针的概念,使用引用计数的想法,让程序员不再需要关心手动释放内存。
23-
这些智能指针就包括 `std::shared_ptr`/`std::unique_ptr`/`std::weak_ptr`,使用它们需要包含头文件 `<memory>`
22+
『记得』对资源进行释放。而 C++11 引入了智能指针的概念,使用了引用计数的想法,让程序员不再需要关心手动释放内存。
23+
这些智能指针包括 `std::shared_ptr`/`std::unique_ptr`/`std::weak_ptr`,使用它们需要包含头文件 `<memory>`
2424

2525
> 注意:引用计数不是垃圾回收,引用计数能够尽快收回不再被使用的对象,同时在回收的过程中也不会造成长时间的等待,
2626
> 更能够清晰明确的表明资源的生命周期。
2727
2828
## 5.2 `std::shared_ptr`
2929

30-
`std::shared_ptr` 是一种智能指针,它能够记录多少个 `shared_ptr` 共同指向一个对象,从而消除显式的调用
30+
`std::shared_ptr` 是一种智能指针,它能够记录多少个 `shared_ptr` 共同指向一个对象,从而消除显式的调用
3131
`delete`,当引用计数变为零的时候就会将对象自动删除。
3232

3333
但还不够,因为使用 `std::shared_ptr` 仍然需要使用 `new` 来调用,这使得代码出现了某种程度上的不对称。
@@ -59,21 +59,23 @@ int main() {
5959
auto pointer = std::make_shared<int>(10);
6060
auto pointer2 = pointer; // 引用计数+1
6161
auto pointer3 = pointer; // 引用计数+1
62-
int *p = pointer.get(); // 这样不会增加引用计数
63-
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; // 3
62+
int *p = pointer.get(); // 这样不会增加引用计数
63+
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; // 3
6464
std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl; // 3
6565
std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl; // 3
6666
6767
pointer2.reset();
6868
std::cout << "reset pointer2:" << std::endl;
69-
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; // 2
70-
std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl; // 0, pointer2 已 reset
69+
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; // 2
70+
std::cout << "pointer2.use_count() = "
71+
<< pointer2.use_count() << std::endl; // pointer2 已 reset; 0
7172
std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl; // 2
7273
pointer3.reset();
7374
std::cout << "reset pointer3:" << std::endl;
74-
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; // 1
75+
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; // 1
7576
std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl; // 0
76-
std::cout << "pointer3.use_count() = " << pointer3.use_count() << std::endl; // 0, pointer3 已 reset
77+
std::cout << "pointer3.use_count() = "
78+
<< pointer3.use_count() << std::endl; // pointer3 已 reset; 0
7779
```
7880

7981
## 5.3 `std::unique_ptr`

book/zh-cn/06-regex.md

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ C++11 提供的正则表达式库操作 `std::string` 对象,
9494

9595
int main() {
9696
std::string fnames[] = {"foo.txt", "bar.txt", "test", "a0.txt", "AAA.txt"};
97-
// 在 C++ 中 \ 会被作为字符串内的转义符,为使 \. 作为正则表达式传递进去生效,需要对 \ 进行二次转义,从而有 \\.
97+
// 在 C++ 中 \ 会被作为字符串内的转义符,
98+
// 为使 \. 作为正则表达式传递进去生效,需要对 \ 进行二次转义,从而有 \\.
9899
std::regex txt_regex("[a-z]+\\.txt");
99100
for (const auto &fname: fnames)
100101
std::cout << fname << ": " << std::regex_match(fname, txt_regex) << std::endl;
@@ -103,7 +104,7 @@ int main() {
103104

104105
另一种常用的形式就是依次传入 `std::string`/`std::smatch`/`std::regex` 三个参数,
105106
其中 `std::smatch` 的本质其实是 `std::match_results`
106-
在标准库中`std::smatch` 被定义为了 `std::match_results<std::string::const_iterator>`
107+
故而在标准库的实现中`std::smatch` 被定义为了 `std::match_results<std::string::const_iterator>`
107108
也就是一个子串迭代器类型的 `match_results`
108109
使用 `std::smatch` 可以方便的对匹配的结果进行获取,例如:
109110

@@ -193,16 +194,23 @@ protected:
193194
template<typename SERVER_TYPE>
194195
void start_server(SERVER_TYPE &server) {
195196
196-
// process GET request for /match/[digit+numbers], e.g. GET request is /match/abc123, will return abc123
197-
server.resource["fill_your_reg_ex"]["GET"] = [](ostream& response, Request& request) {
197+
// process GET request for /match/[digit+numbers],
198+
// e.g. GET request is /match/abc123, will return abc123
199+
server.resource["fill_your_reg_ex"]["GET"] =
200+
[](ostream& response, Request& request)
201+
{
198202
string number=request.path_match[1];
199-
response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number;
203+
response << "HTTP/1.1 200 OK\r\nContent-Length: "
204+
<< number.length() << "\r\n\r\n" << number;
200205
};
201206
202-
// peocess default GET request; anonymous function will be called if no other matches
203-
// response files in folder web/
207+
// peocess default GET request;
208+
// anonymous function will be called
209+
// if no other matches response files in folder web/
204210
// default: index.html
205-
server.default_resource["fill_your_reg_ex"]["GET"] = [](ostream& response, Request& request) {
211+
server.default_resource["fill_your_reg_ex"]["GET"] =
212+
[](ostream& response, Request& request)
213+
{
206214
string filename = "www/";
207215
208216
string path = request.path_match[1];

0 commit comments

Comments
 (0)