Skip to content

Commit 74432a6

Browse files
committed
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!
1 parent dcaa5a0 commit 74432a6

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/working_files.cc

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <chrono>
1313
#include <climits>
1414
#include <numeric>
15-
#include <sstream>
1615
namespace chrono = std::chrono;
1716

1817
using namespace clang;
@@ -43,13 +42,17 @@ Position GetPositionForOffset(const std::string &content, int offset) {
4342
return {line, col};
4443
}
4544

46-
std::vector<std::string> ToLines(const std::string &content) {
47-
std::vector<std::string> result;
48-
std::istringstream lines(content);
49-
std::string line;
50-
while (getline(lines, line))
51-
result.push_back(line);
52-
return result;
45+
std::vector<std::string> ToLines(const std::string &c) {
46+
std::vector<std::string> ret;
47+
int last = 0, e = c.size();
48+
for (int i = 0; i < e; i++)
49+
if (c[i] == '\n') {
50+
ret.emplace_back(&c[last], i - last - (i && c[i - 1] == '\r'));
51+
last = i + 1;
52+
}
53+
if (last < e)
54+
ret.emplace_back(&c[last], e - last);
55+
return ret;
5356
}
5457

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

0 commit comments

Comments
 (0)