Skip to content

Commit dfa476d

Browse files
committed
cpp string: update comment style
1 parent 675fb48 commit dfa476d

1 file changed

Lines changed: 65 additions & 83 deletions

File tree

‎cpp/string.cpp‎

Lines changed: 65 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@
22

33
#include "common.hpp"
44

5-
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
6-
std::stringstream ss(s);
5+
std::vector<std::string> split(const std::string &s, char delim) {
76
std::string item;
7+
std::stringstream ss(s);
8+
std::vector<std::string> output;
89
while (std::getline(ss, item, delim)) {
9-
elems.push_back(item);
10+
output.push_back(item);
1011
}
11-
return elems;
12-
}
13-
14-
std::vector<std::string> split(const std::string &s, char delim) {
15-
std::vector<std::string> elems;
16-
split(s, delim, elems);
17-
return elems;
12+
return output;
1813
}
1914

2015
int main() {
@@ -31,17 +26,16 @@ int main() {
3126
assert(oss.str() == "abc");
3227
}
3328

34-
/* # + for strings
35-
*
36-
* # cat
37-
*
38-
* # concatenate.
39-
*
40-
* Creates a new string.
41-
*
42-
* The only way to do inline this without creating a new string seems to be by using stringstream.
43-
* http://stackoverflow.com/questions/662918/how-do-i-concatenate-multiple-c-strings-on-one-line
44-
*/
29+
// # + for strings
30+
//
31+
// # cat
32+
//
33+
// # concatenate.
34+
//
35+
// Creates a new string.
36+
//
37+
// The only way to do inline this without creating a new string seems to be by using stringstream.
38+
// http://stackoverflow.com/questions/662918/how-do-i-concatenate-multiple-c-strings-on-one-line
4539
{
4640
std::string s = "ab";
4741
std::string s1 = "cd";
@@ -65,23 +59,21 @@ int main() {
6559
//s[3] = 'd';
6660
}
6761

68-
/* # lowercase
69-
*
70-
* http://stackoverflow.com/questions/313970/stl-string-to-lower-case
71-
*
72-
* Boost has a single function: boost::algorithm::to_lower(str);
73-
*/
62+
// # lowercase
63+
//
64+
// http://stackoverflow.com/questions/313970/stl-string-to-lower-case
65+
//
66+
// Boost has a single function: boost::algorithm::to_lower(str);
7467
{
7568
// Best stdlib way with transform:
7669
std::string s = "AbCd1_";
7770
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
7871
assert(s == "abcd1_");
7972
}
8073

81-
/* # c_str
82-
*
83-
* Convert std::string to C null terminated char* string.
84-
*/
74+
// # c_str
75+
//
76+
// Convert std::string to C null terminated char* string.
8577
{
8678
std::string s = "abc";
8779
assert((std::strcmp(s.c_str(), "abc")) == 0);
@@ -95,30 +87,26 @@ int main() {
9587

9688
// # Split at a character into array of strings.
9789
{
98-
// Best stdlib solution for any character: http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c
90+
// Best stdlib solution for any character:
91+
// http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c
9992
// There are shorters sstream solutions that split at whitespace.
10093
// For Boost it's a one liner.
10194
{
10295
assert((split("01:23::45", ':') == std::vector<std::string>{"01", "23", "", "45"}));
103-
104-
std::vector<std::string> v;
105-
split("01:23::45", ':', v);
106-
assert((v == std::vector<std::string>{"01", "23", "", "45"}));
10796
}
10897
}
10998

110-
/* # strip
111-
*
112-
* # chomp
113-
*
114-
* # trim
115-
*
116-
* Exact same techniques as removing elements from vectors but for characters.
117-
*
118-
* It's just that those operations are so common on strings...
119-
*
120-
* http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
121-
*/
99+
// # strip
100+
//
101+
// # chomp
102+
//
103+
// # trim
104+
//
105+
// Exact same techniques as removing elements from vectors but for characters.
106+
//
107+
// It's just that those operations are so common on strings...
108+
//
109+
// http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
122110
{
123111
// A single character: remove and erase idiom.
124112
// Single remove_all call in Boost.
@@ -142,19 +130,18 @@ int main() {
142130
}
143131
}
144132

145-
/* # getline
146-
*
147-
* Read istream until a any given character, by default newline, and store chars read into a string.
148-
*
149-
* The other major method of getting data from streams is `operator<<`,
150-
* which generaly speaking reads until whitespace. getline is generaly saner.
151-
*
152-
* Returns the stream itself, which allows to:
153-
*
154-
* - chain calls
155-
* - do while(getline) combos, as streams can be converted to bool via the `void*()`
156-
* operator which returns a pointer type which is then converted to a boolean.
157-
*/
133+
// # getline
134+
//
135+
// Read istream until a any given character, by default newline, and store chars read into a string.
136+
//
137+
// The other major method of getting data from streams is `operator<<`,
138+
// which generaly speaking reads until whitespace. getline is generaly saner.
139+
//
140+
// Returns the stream itself, which allows to:
141+
//
142+
// - chain calls
143+
// - do while(getline) combos, as streams can be converted to bool via the `void*()`
144+
// operator which returns a pointer type which is then converted to a boolean.
158145
{
159146
// Up to newline.
160147
{
@@ -219,35 +206,31 @@ int main() {
219206
assert(oss.str() == "abcd");
220207
}
221208

222-
/* # int to string
223-
*
224-
* There are a few standard alternatives.
225-
*
226-
* http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c
227-
*/
209+
// # int to string
210+
//
211+
// There are a few standard alternatives.
212+
//
213+
// http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c
228214
{
229-
/* C++11 solves the question once and for all with a robust one-liner for base types.
230-
*
231-
* It is not intended however for class input.
232-
*/
215+
// C++11 solves the question once and for all with a robust one-liner for base types.
216+
//
217+
// It is not intended however for class input.
233218
#if __cplusplus >= 201103L
234219
assert(std::to_string(123) == "123");
235220
#endif
236221

237-
/* std::stringstream seems to be the best pre C++11 solution.
238-
*
239-
* It also has the advantage of working for any class that implements `operator<<`.
240-
*/
222+
// std::stringstream seems to be the best pre C++11 solution.
223+
//
224+
// It also has the advantage of working for any class that implements `operator<<`.
241225
{
242226
std::stringstream oss;
243227
oss << 123;
244228
assert(oss.str() == "123");
245229
}
246230

247-
/* C sprintf.
248-
*
249-
* Works, but uses too many conversion operations.
250-
*/
231+
// C sprintf.
232+
//
233+
// Works, but uses too many conversion operations.
251234
{
252235
char cs[16];
253236
std::sprintf(cs, "%d", 123);
@@ -256,10 +239,9 @@ int main() {
256239
}
257240
}
258241

259-
/* # string to int
260-
*
261-
* http://stackoverflow.com/questions/7663709/convert-string-to-int-c
262-
*/
242+
// # string to int
243+
//
244+
// http://stackoverflow.com/questions/7663709/convert-string-to-int-c
263245
{
264246
// Best C++11 error checking option: stoi
265247
#if __cplusplus >= 201103L

0 commit comments

Comments
 (0)