Last active
March 31, 2025 14:13
-
-
Save rlapz/88ff027e7bb7878b5d57d503f8fb637c to your computer and use it in GitHub Desktop.
strverscmp-like in zig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn less_than_vers(s1: []const u8, s2: []const u8) bool { | |
var ii1: usize = 0; | |
var ii2: usize = 0; | |
const n1 = s1.len; | |
const n2 = s2.len; | |
while (ii1 < n1 and ii2 < n2) { | |
const c1 = s1[ii1]; | |
const c2 = s2[ii2]; | |
if (ascii.isDigit(c1) and ascii.isDigit(c2)) { | |
const start1 = ii1; | |
const start2 = ii2; | |
while (ii1 < n1 and s1[ii1] == '0') : (ii1 += 1) {} | |
while (ii2 < n2 and s2[ii2] == '0') : (ii2 += 1) {} | |
var end1 = ii1; | |
var end2 = ii2; | |
while (end1 < n1 and ascii.isDigit(s1[end1])) : (end1 += 1) {} | |
while (end2 < n2 and ascii.isDigit(s2[end2])) : (end2 += 1) {} | |
const len1 = end1 - ii1; | |
const len2 = end2 - ii2; | |
if (len1 != len2) | |
return (len1 < len2); | |
while (ii1 < end1 and ii2 < end2) { | |
if (s1[ii1] != s2[ii2]) | |
return (s1[ii1] < s2[ii2]); | |
ii1 += 1; | |
ii2 += 1; | |
} | |
const zlen1 = ii1 - start1; | |
const zlen2 = ii2 - start2; | |
if (zlen1 != zlen2) | |
return (zlen1 < zlen2); | |
} else { | |
if (c1 != c2) | |
return (c1 < c2); | |
ii1 += 1; | |
ii2 += 1; | |
} | |
} | |
if (ii1 < n1) | |
return true; | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment