singleRowDiff handles the base case where seq1 has exactly one element. It matches difflib.Diff's behavior which prefers LeftOnly and matches at the latest possible position in seq2.
(elem string, seq2 []string)
| 96 | // It matches difflib.Diff's behavior which prefers LeftOnly and matches |
| 97 | // at the latest possible position in seq2. |
| 98 | func singleRowDiff(elem string, seq2 []string) []difflib.DiffRecord { |
| 99 | result := make([]difflib.DiffRecord, 0, len(seq2)+1) |
| 100 | |
| 101 | found := -1 |
| 102 | for j := len(seq2) - 1; j >= 0; j-- { |
| 103 | if elem == seq2[j] { |
| 104 | found = j |
| 105 | break |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if found >= 0 { |
| 110 | for j := 0; j < found; j++ { |
| 111 | result = append(result, difflib.DiffRecord{Payload: seq2[j], Delta: difflib.RightOnly}) |
| 112 | } |
| 113 | result = append(result, difflib.DiffRecord{Payload: elem, Delta: difflib.Common}) |
| 114 | for j := found + 1; j < len(seq2); j++ { |
| 115 | result = append(result, difflib.DiffRecord{Payload: seq2[j], Delta: difflib.RightOnly}) |
| 116 | } |
| 117 | } else { |
| 118 | result = append(result, difflib.DiffRecord{Payload: elem, Delta: difflib.LeftOnly}) |
| 119 | for _, s := range seq2 { |
| 120 | result = append(result, difflib.DiffRecord{Payload: s, Delta: difflib.RightOnly}) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return result |
| 125 | } |
| 126 | |
| 127 | // lcsLift computes the last row of the standard LCS DP matrix. |
| 128 | // result[j] = LCS(a, b[:j]) for j = 0..len(b). Uses O(len(b)) space. |