File indexing completed on 2025-01-19 05:11:32
0001 /* 0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 0007 #include "text.h" 0008 0009 #include <QDebug> 0010 0011 namespace Diff 0012 { 0013 Text readLines(const QString &text) 0014 { 0015 if (text.isEmpty()) 0016 return {}; 0017 0018 LineEnding le{LineEnding::None}; 0019 QString seprator; 0020 for (const auto &ch : text) { 0021 if (le == LineEnding::Cr) { 0022 if (ch == QLatin1Char('\n')) { 0023 le = LineEnding::CrLf; 0024 seprator = QStringLiteral("\r\n"); 0025 } 0026 break; 0027 } 0028 if (ch == QLatin1Char('\r')) { 0029 le = LineEnding::Cr; 0030 seprator = QStringLiteral("\r"); 0031 continue; 0032 } 0033 if (ch == QLatin1Char('\n')) { 0034 seprator = QStringLiteral("\n"); 0035 le = LineEnding::Lf; 0036 break; 0037 } 0038 } 0039 0040 if (le == LineEnding::None) { 0041 qWarning() << "Unable to detect line ending"; 0042 return {}; 0043 } 0044 0045 int i{0}; 0046 Text t; 0047 t.lineEnding = le; 0048 while (i != -1) { 0049 auto n = text.indexOf(seprator, i); 0050 if (n == -1) 0051 break; 0052 t.lines.append(text.mid(i, n - i)); 0053 i = n + seprator.size(); 0054 } 0055 0056 return t; 0057 } 0058 0059 Text::Text() 0060 : lineEnding(LineEnding::None) 0061 { 0062 } 0063 }