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 oth
()
| 1436 | return (num_lines[side],text) |
| 1437 | |
| 1438 | def _line_iterator(): |
| 1439 | """Yields from/to lines of text with a change indication. |
| 1440 | |
| 1441 | This function is an iterator. It itself pulls lines from a |
| 1442 | differencing iterator, processes them and yields them. When it can |
| 1443 | it yields both a "from" and a "to" line, otherwise it will yield one |
| 1444 | or the other. In addition to yielding the lines of from/to text, a |
| 1445 | boolean flag is yielded to indicate if the text line(s) have |
| 1446 | differences in them. |
| 1447 | |
| 1448 | Note, this function is purposefully not defined at the module scope so |
| 1449 | that data it needs from its parent function (within whose context it |
| 1450 | is defined) does not need to be of module scope. |
| 1451 | """ |
| 1452 | lines = [] |
| 1453 | num_blanks_pending, num_blanks_to_yield = 0, 0 |
| 1454 | while True: |
| 1455 | # Load up next 4 lines so we can look ahead, create strings which |
| 1456 | # are a concatenation of the first character of each of the 4 lines |
| 1457 | # so we can do some very readable comparisons. |
| 1458 | while len(lines) < 4: |
| 1459 | lines.append(next(diff_lines_iterator, 'X')) |
| 1460 | s = ''.join([line[0] for line in lines]) |
| 1461 | if s.startswith('X'): |
| 1462 | # When no more lines, pump out any remaining blank lines so the |
| 1463 | # corresponding add/delete lines get a matching blank line so |
| 1464 | # all line pairs get yielded at the next level. |
| 1465 | num_blanks_to_yield = num_blanks_pending |
| 1466 | elif s.startswith('-?+?'): |
| 1467 | # simple intraline change |
| 1468 | yield _make_line(lines,'?',0), _make_line(lines,'?',1), True |
| 1469 | continue |
| 1470 | elif s.startswith('--++'): |
| 1471 | # in delete block, add block coming: we do NOT want to get |
| 1472 | # caught up on blank lines yet, just process the delete line |
| 1473 | num_blanks_pending -= 1 |
| 1474 | yield _make_line(lines,'-',0), None, True |
| 1475 | continue |
| 1476 | elif s.startswith(('--?+', '--+', '- ')): |
| 1477 | # in delete block and see an intraline change or unchanged line |
| 1478 | # coming: yield the delete line and then blanks |
| 1479 | from_line,to_line = _make_line(lines,'-',0), None |
| 1480 | num_blanks_to_yield,num_blanks_pending = num_blanks_pending-1,0 |
| 1481 | elif s.startswith('-+?'): |
| 1482 | # intraline change |
| 1483 | yield _make_line(lines,None,0), _make_line(lines,'?',1), True |
| 1484 | continue |
| 1485 | elif s.startswith('-?+'): |
| 1486 | # intraline change |
| 1487 | yield _make_line(lines,'?',0), _make_line(lines,None,1), True |
| 1488 | continue |
| 1489 | elif s.startswith('-'): |
| 1490 | # delete FROM line |
| 1491 | num_blanks_pending -= 1 |
| 1492 | yield _make_line(lines,'-',0), None, True |
| 1493 | continue |
| 1494 | elif s.startswith('+--'): |
| 1495 | # in add block, delete block coming: we do NOT want to get |
no test coverage detected