Skip to content

Commit

Permalink
working_files: normalize \r\n and \n to \n
Browse files Browse the repository at this point in the history
Clients may normalize end-of-line sequences, thus cause a mismatch
between index_lines and buffer_lines.

Thanks to CXuesong for reporting this issue!
  • Loading branch information
MaskRay committed Mar 2, 2019
1 parent 00158e2 commit 7e77561
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/working_files.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ limitations under the License.
#include <chrono>
#include <climits>
#include <numeric>
#include <sstream>
namespace chrono = std::chrono;

using namespace clang;
Expand Down Expand Up @@ -55,13 +54,17 @@ Position GetPositionForOffset(const std::string &content, int offset) {
return {line, col};
}

std::vector<std::string> ToLines(const std::string &content) {
std::vector<std::string> result;
std::istringstream lines(content);
std::string line;
while (getline(lines, line))
result.push_back(line);
return result;
std::vector<std::string> ToLines(const std::string &c) {
std::vector<std::string> ret;
int last = 0, e = c.size();
for (int i = 0; i < e; i++)
if (c[i] == '\n') {
ret.emplace_back(&c[last], i - last - (i && c[i - 1] == '\r'));
last = i + 1;
}
if (last < e)
ret.emplace_back(&c[last], e - last);
return ret;
}

// Computes the edit distance of strings [a,a+la) and [b,b+lb) with Eugene W.
Expand Down

0 comments on commit 7e77561

Please sign in to comment.