loc is a location in text1, compute and return the equivalent location in text2. e.g. "The cat" vs "The big cat", 1->1, 5->8 @param diffs LinkedList of Diff objects. @param loc Location within text1. @return Location within text2.
(LinkedList<Diff> diffs, int loc)
| 1319 | * @return Location within text2. |
| 1320 | */ |
| 1321 | public int diff_xIndex(LinkedList<Diff> diffs, int loc) { |
| 1322 | int chars1 = 0; |
| 1323 | int chars2 = 0; |
| 1324 | int last_chars1 = 0; |
| 1325 | int last_chars2 = 0; |
| 1326 | Diff lastDiff = null; |
| 1327 | for (Diff aDiff : diffs) { |
| 1328 | if (aDiff.operation != Operation.INSERT) { |
| 1329 | // Equality or deletion. |
| 1330 | chars1 += aDiff.text.length(); |
| 1331 | } |
| 1332 | if (aDiff.operation != Operation.DELETE) { |
| 1333 | // Equality or insertion. |
| 1334 | chars2 += aDiff.text.length(); |
| 1335 | } |
| 1336 | if (chars1 > loc) { |
| 1337 | // Overshot the location. |
| 1338 | lastDiff = aDiff; |
| 1339 | break; |
| 1340 | } |
| 1341 | last_chars1 = chars1; |
| 1342 | last_chars2 = chars2; |
| 1343 | } |
| 1344 | if (lastDiff != null && lastDiff.operation == Operation.DELETE) { |
| 1345 | // The location was deleted. |
| 1346 | return last_chars2; |
| 1347 | } |
| 1348 | // Add the remaining character length. |
| 1349 | return last_chars2 + (loc - last_chars1); |
| 1350 | } |
| 1351 | |
| 1352 | /** |
| 1353 | * Convert a Diff list into a pretty HTML report. |