From f40b73c81236ef2305d9979b7ad6ecf589b03e0a Mon Sep 17 00:00:00 2001 From: Bryan Gardiner Date: Sun, 18 Aug 2024 16:59:27 -0700 Subject: [PATCH] For version highlighting, don't rewind past number-letter boundaries. --- src/nvd | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/nvd b/src/nvd index ed9c724..94f3a59 100755 --- a/src/nvd +++ b/src/nvd @@ -106,6 +106,8 @@ PROFILE_LINK_REGEX = re.compile(r"^(?P.+)-(?P[0-9]+)-link$") ALPHANUM_RE = re.compile(r"\w") # Alternatively [0-9a-zA-Z] would work. +DIGIT_RE = re.compile("[0-9]") + def raise_arg(e): raise e @@ -688,12 +690,24 @@ def find_common_version_prefix_lists(*lsts: list[str]) -> int: # looks weird to show e.g. "1.15 -> 1.17" with only the 5 and 7 highlighted. # It's nicer to highlight 15 and 17. # - # It might also make sense to rewind from the middle of a number, or from - # the middle of a word, but *not* cross over from a word to a number or vice - # versa. + # Also, we want to rewind from the middle of a number but stop if we reach + # the end of a preceeding word, or vice versa. For example, with + # "1.9.15p2 -> 1.9.15p5", we should just highlight the final digit, not the + # common "15p" part. This is consistent with Nix treating numbers and + # letters as separate chunks. if not first and i < len(target) and i > 0 and ALPHANUM_RE.fullmatch(target[i]): - while i > 0 and ALPHANUM_RE.fullmatch(target[i - 1]): - i -= 1 + if DIGIT_RE.fullmatch(target[i]): + # Rewind only through digits. + while i > 0 and DIGIT_RE.fullmatch(target[i - 1]): + i -= 1 + else: + # Rewind only through letters. + while ( + i > 0 + and ALPHANUM_RE.fullmatch(target[i - 1]) + and not DIGIT_RE.fullmatch(target[i - 1]) + ): + i -= 1 return i