MCPcopy Create free account
hub / github.com/Codeya-IDE/deepin-ide / patch_fromText

Method patch_fromText

3rdparty/diff-match-patch/diff_match_patch.cpp:2033–2105  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2031
2032
2033QList<Patch> diff_match_patch::patch_fromText(const QString &textline) {
2034 QList<Patch> patches;
2035 if (textline.isEmpty()) {
2036 return patches;
2037 }
2038 QStringList text = textline.split("\n", QString::SkipEmptyParts);
2039 Patch patch;
2040 QRegExp patchHeader("^@@ -(\\d+),?(\\d*) \\+(\\d+),?(\\d*) @@$");
2041 char sign;
2042 QString line;
2043 while (!text.isEmpty()) {
2044 if (!patchHeader.exactMatch(text.front())) {
2045 throw QString("Invalid patch string: %1").arg(text.front());
2046 }
2047
2048 patch = Patch();
2049 patch.start1 = patchHeader.cap(1).toInt();
2050 if (patchHeader.cap(2).isEmpty()) {
2051 patch.start1--;
2052 patch.length1 = 1;
2053 } else if (patchHeader.cap(2) == "0") {
2054 patch.length1 = 0;
2055 } else {
2056 patch.start1--;
2057 patch.length1 = patchHeader.cap(2).toInt();
2058 }
2059
2060 patch.start2 = patchHeader.cap(3).toInt();
2061 if (patchHeader.cap(4).isEmpty()) {
2062 patch.start2--;
2063 patch.length2 = 1;
2064 } else if (patchHeader.cap(4) == "0") {
2065 patch.length2 = 0;
2066 } else {
2067 patch.start2--;
2068 patch.length2 = patchHeader.cap(4).toInt();
2069 }
2070 text.removeFirst();
2071
2072 while (!text.isEmpty()) {
2073 if (text.front().isEmpty()) {
2074 text.removeFirst();
2075 continue;
2076 }
2077 sign = text.front()[0].toLatin1();
2078 line = safeMid(text.front(), 1);
2079 line = line.replace("+", "%2B"); // decode would change all "+" to " "
2080 line = QUrl::fromPercentEncoding(qPrintable(line));
2081 if (sign == '-') {
2082 // Deletion.
2083 patch.diffs.append(Diff(DELETE, line));
2084 } else if (sign == '+') {
2085 // Insertion.
2086 patch.diffs.append(Diff(INSERT, line));
2087 } else if (sign == ' ') {
2088 // Minor equality.
2089 patch.diffs.append(Diff(EQUAL, line));
2090 } else if (sign == '@') {

Callers

nothing calls this directly

Calls 7

PatchClass · 0.85
frontMethod · 0.80
DiffClass · 0.70
QStringClass · 0.50
isEmptyMethod · 0.45
replaceMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected