From ce3769f0664f8e44c0bdc40d95971aeb35109ea7 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 1 Mar 2019 17:30:53 -0800 Subject: [PATCH] working_files: normalize \r\n and \n to \n Clients may normalize end-of-line sequences, thus cause a mismatch between index_lines and buffer_lines. Thanks to CXuesong for reporting this issue! --- src/working_files.cc | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/working_files.cc b/src/working_files.cc index 771a208b..7348d35a 100644 --- a/src/working_files.cc +++ b/src/working_files.cc @@ -12,7 +12,6 @@ #include #include #include -#include namespace chrono = std::chrono; using namespace clang; @@ -43,13 +42,17 @@ Position GetPositionForOffset(const std::string &content, int offset) { return {line, col}; } -std::vector ToLines(const std::string &content) { - std::vector result; - std::istringstream lines(content); - std::string line; - while (getline(lines, line)) - result.push_back(line); - return result; +std::vector ToLines(const std::string &c) { + std::vector 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.