|
| 1 | +const MAX_UINT64 = '18446744073709551615' |
| 2 | + |
| 3 | +/** |
| 4 | + * The language-neutral SemVer representation used by the FFE evaluator. |
| 5 | + * Build metadata is validated while parsing but is intentionally not retained, |
| 6 | + * because it does not affect SemVer precedence. |
| 7 | + */ |
| 8 | +export interface ParsedSemver { |
| 9 | + major: string |
| 10 | + minor: string |
| 11 | + patch: string |
| 12 | + prerelease: string |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Parse the SemVer subset. |
| 17 | + * Core identifiers are limited to uint64; numeric prerelease identifiers may |
| 18 | + * be arbitrarily large. |
| 19 | + */ |
| 20 | +export function parseSemver(version: unknown): ParsedSemver | null { |
| 21 | + if (typeof version !== 'string') { |
| 22 | + return null |
| 23 | + } |
| 24 | + |
| 25 | + const major = parseCoreIdentifier(version, 0) |
| 26 | + if (!major || major.next >= version.length || version[major.next] !== '.') { |
| 27 | + return null |
| 28 | + } |
| 29 | + |
| 30 | + const minor = parseCoreIdentifier(version, major.next + 1) |
| 31 | + if (!minor || minor.next >= version.length || version[minor.next] !== '.') { |
| 32 | + return null |
| 33 | + } |
| 34 | + |
| 35 | + const patch = parseCoreIdentifier(version, minor.next + 1) |
| 36 | + if (!patch) { |
| 37 | + return null |
| 38 | + } |
| 39 | + |
| 40 | + const parsed: ParsedSemver = { |
| 41 | + major: major.value, |
| 42 | + minor: minor.value, |
| 43 | + patch: patch.value, |
| 44 | + prerelease: '', |
| 45 | + } |
| 46 | + |
| 47 | + if (patch.next === version.length) { |
| 48 | + return parsed |
| 49 | + } |
| 50 | + |
| 51 | + let remainder = version.slice(patch.next) |
| 52 | + if (remainder.startsWith('-')) { |
| 53 | + remainder = remainder.slice(1) |
| 54 | + const buildStart = remainder.indexOf('+') |
| 55 | + if (buildStart === -1) { |
| 56 | + return isValidSemverIdentifiers(remainder, false) ? { ...parsed, prerelease: remainder } : null |
| 57 | + } |
| 58 | + |
| 59 | + const prerelease = remainder.slice(0, buildStart) |
| 60 | + if (!isValidSemverIdentifiers(prerelease, false)) { |
| 61 | + return null |
| 62 | + } |
| 63 | + parsed.prerelease = prerelease |
| 64 | + remainder = remainder.slice(buildStart + 1) |
| 65 | + } else if (remainder.startsWith('+')) { |
| 66 | + remainder = remainder.slice(1) |
| 67 | + } else { |
| 68 | + return null |
| 69 | + } |
| 70 | + |
| 71 | + return isValidSemverIdentifiers(remainder, true) ? parsed : null |
| 72 | +} |
| 73 | + |
| 74 | +/** Compare SemVer precedence. Build metadata is intentionally ignored. */ |
| 75 | +export function compareSemver(left: ParsedSemver, right: ParsedSemver): number { |
| 76 | + for (const [leftValue, rightValue] of [ |
| 77 | + [left.major, right.major], |
| 78 | + [left.minor, right.minor], |
| 79 | + [left.patch, right.patch], |
| 80 | + ]) { |
| 81 | + const ordering = compareNumericStrings(leftValue, rightValue) |
| 82 | + if (ordering !== 0) { |
| 83 | + return ordering |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return compareSemverPrerelease(left.prerelease, right.prerelease) |
| 88 | +} |
| 89 | + |
| 90 | +function parseCoreIdentifier(version: string, start: number): { value: string; next: number } | null { |
| 91 | + if (start >= version.length || !isAsciiDigit(version.charCodeAt(start))) { |
| 92 | + return null |
| 93 | + } |
| 94 | + |
| 95 | + if (version[start] === '0') { |
| 96 | + return { value: '0', next: start + 1 } |
| 97 | + } |
| 98 | + |
| 99 | + let end = start |
| 100 | + while (end < version.length && isAsciiDigit(version.charCodeAt(end))) { |
| 101 | + end++ |
| 102 | + } |
| 103 | + |
| 104 | + const value = version.slice(start, end) |
| 105 | + if (value.length > MAX_UINT64.length || (value.length === MAX_UINT64.length && value > MAX_UINT64)) { |
| 106 | + return null |
| 107 | + } |
| 108 | + return { value, next: end } |
| 109 | +} |
| 110 | + |
| 111 | +function isValidSemverIdentifiers(value: string, allowLeadingZeros: boolean): boolean { |
| 112 | + let identifierStart = 0 |
| 113 | + let identifierNumeric = true |
| 114 | + |
| 115 | + for (let i = 0; i <= value.length; i++) { |
| 116 | + if (i === value.length || value[i] === '.') { |
| 117 | + if (i === identifierStart) { |
| 118 | + return false |
| 119 | + } |
| 120 | + if (!allowLeadingZeros && identifierNumeric && i - identifierStart > 1 && value[identifierStart] === '0') { |
| 121 | + return false |
| 122 | + } |
| 123 | + identifierStart = i + 1 |
| 124 | + identifierNumeric = true |
| 125 | + continue |
| 126 | + } |
| 127 | + |
| 128 | + const code = value.charCodeAt(i) |
| 129 | + if (!isAsciiAlphanumeric(code) && value[i] !== '-') { |
| 130 | + return false |
| 131 | + } |
| 132 | + if (!isAsciiDigit(code)) { |
| 133 | + identifierNumeric = false |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return true |
| 138 | +} |
| 139 | + |
| 140 | +function compareSemverPrerelease(left: string, right: string): number { |
| 141 | + if (left === right) { |
| 142 | + return 0 |
| 143 | + } |
| 144 | + if (left === '') { |
| 145 | + return 1 |
| 146 | + } |
| 147 | + if (right === '') { |
| 148 | + return -1 |
| 149 | + } |
| 150 | + |
| 151 | + let leftRemaining = left |
| 152 | + let rightRemaining = right |
| 153 | + while (true) { |
| 154 | + const [leftIdentifier, nextLeft] = nextSemverIdentifier(leftRemaining) |
| 155 | + const [rightIdentifier, nextRight] = nextSemverIdentifier(rightRemaining) |
| 156 | + const ordering = compareSemverIdentifier(leftIdentifier, rightIdentifier) |
| 157 | + if (ordering !== 0) { |
| 158 | + return ordering |
| 159 | + } |
| 160 | + |
| 161 | + if (nextLeft === '' || nextRight === '') { |
| 162 | + if (nextLeft === '' && nextRight === '') { |
| 163 | + return 0 |
| 164 | + } |
| 165 | + return nextLeft === '' ? -1 : 1 |
| 166 | + } |
| 167 | + |
| 168 | + leftRemaining = nextLeft.slice(1) |
| 169 | + rightRemaining = nextRight.slice(1) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +function nextSemverIdentifier(value: string): [string, string] { |
| 174 | + const dot = value.indexOf('.') |
| 175 | + return dot === -1 ? [value, ''] : [value.slice(0, dot), value.slice(dot)] |
| 176 | +} |
| 177 | + |
| 178 | +function compareSemverIdentifier(left: string, right: string): number { |
| 179 | + const leftNumeric = isSemverNumericIdentifier(left) |
| 180 | + const rightNumeric = isSemverNumericIdentifier(right) |
| 181 | + |
| 182 | + if (leftNumeric && rightNumeric) { |
| 183 | + return compareNumericStrings(left, right) |
| 184 | + } |
| 185 | + if (leftNumeric) { |
| 186 | + return -1 |
| 187 | + } |
| 188 | + if (rightNumeric) { |
| 189 | + return 1 |
| 190 | + } |
| 191 | + return compareAsciiStrings(left, right) |
| 192 | +} |
| 193 | + |
| 194 | +function isSemverNumericIdentifier(value: string): boolean { |
| 195 | + for (let i = 0; i < value.length; i++) { |
| 196 | + if (!isAsciiDigit(value.charCodeAt(i))) { |
| 197 | + return false |
| 198 | + } |
| 199 | + } |
| 200 | + return true |
| 201 | +} |
| 202 | + |
| 203 | +function compareNumericStrings(left: string, right: string): number { |
| 204 | + if (left.length !== right.length) { |
| 205 | + return left.length < right.length ? -1 : 1 |
| 206 | + } |
| 207 | + return compareAsciiStrings(left, right) |
| 208 | +} |
| 209 | + |
| 210 | +function compareAsciiStrings(left: string, right: string): number { |
| 211 | + const length = Math.min(left.length, right.length) |
| 212 | + for (let i = 0; i < length; i++) { |
| 213 | + const leftCode = left.charCodeAt(i) |
| 214 | + const rightCode = right.charCodeAt(i) |
| 215 | + if (leftCode !== rightCode) { |
| 216 | + return leftCode < rightCode ? -1 : 1 |
| 217 | + } |
| 218 | + } |
| 219 | + if (left.length === right.length) { |
| 220 | + return 0 |
| 221 | + } |
| 222 | + return left.length < right.length ? -1 : 1 |
| 223 | +} |
| 224 | + |
| 225 | +function isAsciiDigit(code: number): boolean { |
| 226 | + return code >= 48 && code <= 57 |
| 227 | +} |
| 228 | + |
| 229 | +function isAsciiAlphanumeric(code: number): boolean { |
| 230 | + return isAsciiDigit(code) || (code >= 65 && code <= 90) || (code >= 97 && code <= 122) |
| 231 | +} |
0 commit comments