From f09155e486c69937e021eba58a0993d7fc023dd8 Mon Sep 17 00:00:00 2001 From: Daniel Wolf Date: Mon, 1 Feb 2016 20:47:27 +0100 Subject: [PATCH] Using raw pointers instead of iterators for string manipulation This avoids an assertion error when I temporarily move 1 past end --- src/stringTools.cpp | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/stringTools.cpp b/src/stringTools.cpp index 657d85d..f3eb098 100644 --- a/src/stringTools.cpp +++ b/src/stringTools.cpp @@ -6,20 +6,20 @@ using std::vector; vector splitIntoLines(const string& s) { vector lines; - auto it = s.cbegin(); - auto lineBegin = it; - auto end = s.cend(); + auto p = &s[0]; + auto lineBegin = p; + auto end = p + s.size(); // 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 - if (it == end || *it == '\n') { - string line(lineBegin, it); + if (p == end || *p == '\n') { + string line(lineBegin, p); // Trim \r characters boost::algorithm::trim_if(line, [](char c) { return c == '\r'; }); lines.push_back(line); - lineBegin = it + 1; + lineBegin = p + 1; } - ++it; + ++p; } return lines; @@ -31,22 +31,22 @@ vector wrapSingleLineString(const string& s, int lineLength) { if (s.find('\n') != std::string::npos) throw std::invalid_argument("s must not contain line breaks."); vector lines; - auto it = s.cbegin(); - auto lineBegin = it; - auto lineEnd = it; - auto end = s.cend(); + auto p = &s[0]; + auto lineBegin = p; + auto lineEnd = p; + auto end = p + s.size(); // Iterate over input string - while (it <= end) { + while (p <= end) { // If we're at a word boundary: update safeLineEnd - if (it == end || *it == ' ') { - lineEnd = it; + if (p == end || *p == ' ') { + lineEnd = p; } // 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) { // The line contains a single word, which is too long. Split mid-word. - lineEnd = it; + lineEnd = p; } // Add trimmed line to list @@ -55,12 +55,12 @@ vector wrapSingleLineString(const string& s, int lineLength) { lines.push_back(line); // Resume after the last line, skipping spaces - it = lineEnd; - while (it != end && *it == ' ') ++it; - lineBegin = lineEnd = it; + p = lineEnd; + while (p != end && *p == ' ') ++p; + lineBegin = lineEnd = p; } - ++it; + ++p; } return lines;