Figure 1: First iteration of make_s
1: //
2: // First iteration: mostly pointers, a string just to
3: // hold the result.
4: //
5:
6: template<typename Container>
7: Container make_s(const char *intext,
8: bool stripLeadingSpaces = true)
9: {
10: using std::string;
11: int len;
12: char *incopy = new char[(len = strlen(intext)) + 1];
13: strcpy(incopy, intext);
14: Container c;
15: char *end = incopy + len;
16: char *nextComma;
17:
18: char *curPos = incopy;
19: while (true)
20: {
21: nextComma = std::find(curPos, end, ',');
22: *nextComma = '\0';
23:
24: if (stripLeadingSpaces)
25: while (isspace(*curPos)) // ignore leading space
26: ++curPos; // on each string
27:
28: string s(curPos); // from curPos to the NUL
29: c.push_back(s); // not for sets/multisets
30:
31: if (nextComma == end)
32: break;
33:
34: curPos = ++nextComma;
35: }
36: delete[] incopy;
37: return c;
38: }