Using raw pointers instead of iterators for string manipulation
This avoids an assertion error when I temporarily move 1 past end
This commit is contained in:
parent
75872fe45d
commit
f09155e486
|
@ -6,20 +6,20 @@ using std::vector;
|
||||||
|
|
||||||
vector<string> splitIntoLines(const string& s) {
|
vector<string> splitIntoLines(const string& s) {
|
||||||
vector<string> lines;
|
vector<string> lines;
|
||||||
auto it = s.cbegin();
|
auto p = &s[0];
|
||||||
auto lineBegin = it;
|
auto lineBegin = p;
|
||||||
auto end = s.cend();
|
auto end = p + s.size();
|
||||||
// Iterate over input string
|
// Iterate over input string
|
||||||
while (it <= end) {
|
while (p <= end) {
|
||||||
// Add a new result line when we hit a \n character or the end of the string
|
// Add a new result line when we hit a \n character or the end of the string
|
||||||
if (it == end || *it == '\n') {
|
if (p == end || *p == '\n') {
|
||||||
string line(lineBegin, it);
|
string line(lineBegin, p);
|
||||||
// Trim \r characters
|
// Trim \r characters
|
||||||
boost::algorithm::trim_if(line, [](char c) { return c == '\r'; });
|
boost::algorithm::trim_if(line, [](char c) { return c == '\r'; });
|
||||||
lines.push_back(line);
|
lines.push_back(line);
|
||||||
lineBegin = it + 1;
|
lineBegin = p + 1;
|
||||||
}
|
}
|
||||||
++it;
|
++p;
|
||||||
}
|
}
|
||||||
|
|
||||||
return lines;
|
return lines;
|
||||||
|
@ -31,22 +31,22 @@ vector<string> wrapSingleLineString(const string& s, int lineLength) {
|
||||||
if (s.find('\n') != std::string::npos) throw std::invalid_argument("s must not contain line breaks.");
|
if (s.find('\n') != std::string::npos) throw std::invalid_argument("s must not contain line breaks.");
|
||||||
|
|
||||||
vector<string> lines;
|
vector<string> lines;
|
||||||
auto it = s.cbegin();
|
auto p = &s[0];
|
||||||
auto lineBegin = it;
|
auto lineBegin = p;
|
||||||
auto lineEnd = it;
|
auto lineEnd = p;
|
||||||
auto end = s.cend();
|
auto end = p + s.size();
|
||||||
// Iterate over input string
|
// Iterate over input string
|
||||||
while (it <= end) {
|
while (p <= end) {
|
||||||
// If we're at a word boundary: update safeLineEnd
|
// If we're at a word boundary: update safeLineEnd
|
||||||
if (it == end || *it == ' ') {
|
if (p == end || *p == ' ') {
|
||||||
lineEnd = it;
|
lineEnd = p;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we've hit lineLength or the end of the string: add a new result line
|
// If we've hit lineLength or the end of the string: add a new result line
|
||||||
if (it == end || it - lineBegin == lineLength) {
|
if (p == end || p - lineBegin == lineLength) {
|
||||||
if (lineEnd == lineBegin) {
|
if (lineEnd == lineBegin) {
|
||||||
// The line contains a single word, which is too long. Split mid-word.
|
// The line contains a single word, which is too long. Split mid-word.
|
||||||
lineEnd = it;
|
lineEnd = p;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add trimmed line to list
|
// Add trimmed line to list
|
||||||
|
@ -55,12 +55,12 @@ vector<string> wrapSingleLineString(const string& s, int lineLength) {
|
||||||
lines.push_back(line);
|
lines.push_back(line);
|
||||||
|
|
||||||
// Resume after the last line, skipping spaces
|
// Resume after the last line, skipping spaces
|
||||||
it = lineEnd;
|
p = lineEnd;
|
||||||
while (it != end && *it == ' ') ++it;
|
while (p != end && *p == ' ') ++p;
|
||||||
lineBegin = lineEnd = it;
|
lineBegin = lineEnd = p;
|
||||||
}
|
}
|
||||||
|
|
||||||
++it;
|
++p;
|
||||||
}
|
}
|
||||||
|
|
||||||
return lines;
|
return lines;
|
||||||
|
|
Loading…
Reference in New Issue