Yields from/to lines of text with a change indication. This function is an iterator. It itself pulls lines from a differencing iterator, processes them and yields them. When it can it yields both a "from" and a "to" line, otherwise it will yield one or the other.
()
| 1457 | return (num_lines[side],text) |
| 1458 | |
| 1459 | def _line_iterator(): |
| 1460 | """Yields from/to lines of text with a change indication. |
| 1461 | |
| 1462 | This function is an iterator. It itself pulls lines from a |
| 1463 | differencing iterator, processes them and yields them. When it can |
| 1464 | it yields both a "from" and a "to" line, otherwise it will yield one |
| 1465 | or the other. In addition to yielding the lines of from/to text, a |
| 1466 | boolean flag is yielded to indicate if the text line(s) have |
| 1467 | differences in them. |
| 1468 | |
| 1469 | Note, this function is purposefully not defined at the module scope so |
| 1470 | that data it needs from its parent function (within whose context it |
| 1471 | is defined) does not need to be of module scope. |
| 1472 | """ |
| 1473 | lines = [] |
| 1474 | num_blanks_pending, num_blanks_to_yield = 0, 0 |
| 1475 | while True: |
| 1476 | # Load up next 4 lines so we can look ahead, create strings which |
| 1477 | # are a concatenation of the first character of each of the 4 lines |
| 1478 | # so we can do some very readable comparisons. |
| 1479 | while len(lines) < 4: |
| 1480 | lines.append(next(diff_lines_iterator, 'X')) |
| 1481 | s = ''.join([line[0] for line in lines]) |
| 1482 | if s.startswith('X'): |
| 1483 | # When no more lines, pump out any remaining blank lines so the |
| 1484 | # corresponding add/delete lines get a matching blank line so |
| 1485 | # all line pairs get yielded at the next level. |
| 1486 | num_blanks_to_yield = num_blanks_pending |
| 1487 | elif s.startswith('-?+?'): |
| 1488 | # simple intraline change |
| 1489 | yield _make_line(lines,'?',0), _make_line(lines,'?',1), True |
| 1490 | continue |
| 1491 | elif s.startswith('--++'): |
| 1492 | # in delete block, add block coming: we do NOT want to get |
| 1493 | # caught up on blank lines yet, just process the delete line |
| 1494 | num_blanks_pending -= 1 |
| 1495 | yield _make_line(lines,'-',0), None, True |
| 1496 | continue |
| 1497 | elif s.startswith(('--?+', '--+', '- ')): |
| 1498 | # in delete block and see an intraline change or unchanged line |
| 1499 | # coming: yield the delete line and then blanks |
| 1500 | from_line,to_line = _make_line(lines,'-',0), None |
| 1501 | num_blanks_to_yield,num_blanks_pending = num_blanks_pending-1,0 |
| 1502 | elif s.startswith('-+?'): |
| 1503 | # intraline change |
| 1504 | yield _make_line(lines,None,0), _make_line(lines,'?',1), True |
| 1505 | continue |
| 1506 | elif s.startswith('-?+'): |
| 1507 | # intraline change |
| 1508 | yield _make_line(lines,'?',0), _make_line(lines,None,1), True |
| 1509 | continue |
| 1510 | elif s.startswith('-'): |
| 1511 | # delete FROM line |
| 1512 | num_blanks_pending -= 1 |
| 1513 | yield _make_line(lines,'-',0), None, True |
| 1514 | continue |
| 1515 | elif s.startswith('+--'): |
| 1516 | # in add block, delete block coming: we do NOT want to get |
no test coverage detected
searching dependent graphs…