| 1918 | |
| 1919 | |
| 1920 | void diff_match_patch::patch_splitMax(QList<Patch> &patches) { |
| 1921 | short patch_size = Match_MaxBits; |
| 1922 | QString precontext, postcontext; |
| 1923 | Patch patch; |
| 1924 | int start1, start2; |
| 1925 | bool empty; |
| 1926 | Operation diff_type; |
| 1927 | QString diff_text; |
| 1928 | QMutableListIterator<Patch> pointer(patches); |
| 1929 | Patch bigpatch; |
| 1930 | |
| 1931 | if (pointer.hasNext()) { |
| 1932 | bigpatch = pointer.next(); |
| 1933 | } |
| 1934 | |
| 1935 | while (!bigpatch.isNull()) { |
| 1936 | if (bigpatch.length1 <= patch_size) { |
| 1937 | bigpatch = pointer.hasNext() ? pointer.next() : Patch(); |
| 1938 | continue; |
| 1939 | } |
| 1940 | // Remove the big old patch. |
| 1941 | pointer.remove(); |
| 1942 | start1 = bigpatch.start1; |
| 1943 | start2 = bigpatch.start2; |
| 1944 | precontext = ""; |
| 1945 | while (!bigpatch.diffs.isEmpty()) { |
| 1946 | // Create one of several smaller patches. |
| 1947 | patch = Patch(); |
| 1948 | empty = true; |
| 1949 | patch.start1 = start1 - precontext.length(); |
| 1950 | patch.start2 = start2 - precontext.length(); |
| 1951 | if (!precontext.isEmpty()) { |
| 1952 | patch.length1 = patch.length2 = precontext.length(); |
| 1953 | patch.diffs.append(Diff(EQUAL, precontext)); |
| 1954 | } |
| 1955 | while (!bigpatch.diffs.isEmpty() |
| 1956 | && patch.length1 < patch_size - Patch_Margin) { |
| 1957 | diff_type = bigpatch.diffs.front().operation; |
| 1958 | diff_text = bigpatch.diffs.front().text; |
| 1959 | if (diff_type == INSERT) { |
| 1960 | // Insertions are harmless. |
| 1961 | patch.length2 += diff_text.length(); |
| 1962 | start2 += diff_text.length(); |
| 1963 | patch.diffs.append(bigpatch.diffs.front()); |
| 1964 | bigpatch.diffs.removeFirst(); |
| 1965 | empty = false; |
| 1966 | } else if (diff_type == DELETE && patch.diffs.size() == 1 |
| 1967 | && patch.diffs.front().operation == EQUAL |
| 1968 | && diff_text.length() > 2 * patch_size) { |
| 1969 | // This is a large deletion. Let it pass in one chunk. |
| 1970 | patch.length1 += diff_text.length(); |
| 1971 | start1 += diff_text.length(); |
| 1972 | empty = false; |
| 1973 | patch.diffs.append(Diff(diff_type, diff_text)); |
| 1974 | bigpatch.diffs.removeFirst(); |
| 1975 | } else { |
| 1976 | // Deletion or equality. Only take as much as we can stomach. |
| 1977 | diff_text = diff_text.left(std::min(diff_text.length(), |