A Python port of the Invisible Internet Project (I2P)
at main 30 lines 759 B view raw
1"""Semantic version comparison. 2 3Ported from net.i2p.util.VersionComparator. 4""" 5 6from __future__ import annotations 7 8import re 9 10 11def compare_versions(v1: str, v2: str) -> int: 12 """Compare two version strings numerically. 13 14 Returns -1 if v1 < v2, 0 if equal, 1 if v1 > v2. 15 Handles versions like "0.9.50", "0.9.50-1", "2.3.0". 16 """ 17 parts1 = [int(x) for x in re.split(r"[.\-]", v1)] 18 parts2 = [int(x) for x in re.split(r"[.\-]", v2)] 19 20 # Pad shorter list with zeros 21 max_len = max(len(parts1), len(parts2)) 22 parts1.extend([0] * (max_len - len(parts1))) 23 parts2.extend([0] * (max_len - len(parts2))) 24 25 for a, b in zip(parts1, parts2): 26 if a < b: 27 return -1 28 if a > b: 29 return 1 30 return 0