semantic-versions 1.0.0

Reference implementation of Semantic Versions (semver.xyz): parse, compare and range-match versions, with builds that participate in precedence, pre-release and build channels, and contexts.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::str::FromStr;

use crate::identifier::Identifiers;
use crate::{Error, MAX_LENGTH, MAX_SAFE_INTEGER};

/// A parsed Semantic Version, optionally scoped to a context
/// (`npm:react@18.2.0`).
///
/// Ordering follows the specification's precedence rules: pre-releases rank
/// below their release and builds rank above it (`1.0.0-rc < 1.0.0 <
/// 1.0.0+build`). The context never participates in precedence or equality.
///
/// A release is three integers; the pre-release, build and context of the
/// versions that have them live behind one pointer, so a `Version` is 32
/// bytes and plain releases parse without allocating.
#[derive(Clone)]
pub struct Version {
    /// Major version.
    pub major: u64,
    /// Minor version.
    pub minor: u64,
    /// Patch version.
    pub patch: u64,
    meta: Option<Box<Meta>>,
}

#[derive(Clone, Default)]
struct Meta {
    pre: Identifiers,
    build: Identifiers,
    context: Option<Box<str>>,
}

static EMPTY: Identifiers = Identifiers::empty();

impl fmt::Debug for Version {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Version")
            .field("major", &self.major)
            .field("minor", &self.minor)
            .field("patch", &self.patch)
            .field("pre", self.pre())
            .field("build", self.build())
            .field("context", &self.context())
            .finish()
    }
}

/// Split an optional context off a reference: `npm:react@1.2.3` →
/// `(Some("npm:react"), "1.2.3")`. The context ends at the last `@` of the first
/// whitespace-delimited token; a leading `@` (a scoped npm name) is part of the
/// context, and an `@` at position 0 means there is no context.
pub fn split_reference(input: &str) -> (Option<&str>, std::borrow::Cow<'_, str>) {
    if !input.as_bytes().contains(&b'@') {
        return (None, std::borrow::Cow::Borrowed(input));
    }
    let first_space = input.find(|c: char| c.is_whitespace());
    let (head, tail) = match first_space {
        Some(i) => (&input[..i], &input[i..]),
        None => (input, ""),
    };
    match head.rfind('@') {
        Some(at) if at > 0 => {
            let rest = &head[at + 1..];
            let rest = if tail.is_empty() {
                std::borrow::Cow::Borrowed(rest)
            } else {
                std::borrow::Cow::Owned(format!("{rest}{tail}"))
            };
            (Some(&head[..at]), rest)
        }
        _ => (None, std::borrow::Cow::Borrowed(input)),
    }
}

impl Version {
    /// Create a normal version `major.minor.patch`.
    pub fn new(major: u64, minor: u64, patch: u64) -> Self {
        Version {
            major,
            minor,
            patch,
            meta: None,
        }
    }

    pub(crate) fn from_parts(
        major: u64,
        minor: u64,
        patch: u64,
        pre: Identifiers,
        build: Identifiers,
    ) -> Self {
        let meta = if pre.is_empty() && build.is_empty() {
            None
        } else {
            Some(Box::new(Meta {
                pre,
                build,
                context: None,
            }))
        };
        Version {
            major,
            minor,
            patch,
            meta,
        }
    }

    /// Pre-release identifiers (empty for a normal version).
    #[inline]
    pub fn pre(&self) -> &Identifiers {
        self.meta.as_ref().map_or(&EMPTY, |m| &m.pre)
    }

    /// Build identifiers, as written (empty when there is no build).
    #[inline]
    pub fn build(&self) -> &Identifiers {
        self.meta.as_ref().map_or(&EMPTY, |m| &m.build)
    }

    /// The identity this version belongs to, e.g. `npm:react`.
    #[inline]
    pub fn context(&self) -> Option<&str> {
        self.meta.as_ref().and_then(|m| m.context.as_deref())
    }

    fn meta_mut(&mut self) -> &mut Meta {
        self.meta.get_or_insert_with(Default::default)
    }

    /// This version with `pre` as its pre-release.
    pub fn with_pre(mut self, pre: Identifiers) -> Self {
        if !(pre.is_empty() && self.meta.is_none()) {
            self.meta_mut().pre = pre;
        }
        self
    }

    /// This version with `build` as its build.
    pub fn with_build(mut self, build: Identifiers) -> Self {
        if !(build.is_empty() && self.meta.is_none()) {
            self.meta_mut().build = build;
        }
        self
    }

    /// This version scoped to `context` (or to none).
    pub fn with_context(mut self, context: Option<&str>) -> Self {
        if !(context.is_none() && self.meta.is_none()) {
            self.meta_mut().context = context.map(Into::into);
        }
        self
    }

    /// Parse a version, optionally context-qualified (`npm:react@1.2.3`).
    /// There is no coercion: `v1.2.3` and `1.2` are errors.
    pub fn parse(input: &str) -> Result<Self, Error> {
        if input.len() > MAX_LENGTH {
            return Err(Error::TooLong);
        }
        // Fast path: a bare version (no context, no surrounding whitespace)
        // scans directly. Only when that fails do we look for a context or
        // whitespace to strip, so the common case pays for neither.
        if let Ok(v) = parse_core(input, input) {
            return Ok(v);
        }
        let (context, rest) = split_reference(input);
        Ok(parse_core(rest.trim(), input)?.with_context(context))
    }

    /// True if this is a pre-release.
    pub fn is_prerelease(&self) -> bool {
        !self.pre().is_empty()
    }

    /// The version with its build removed (context kept).
    pub fn without_build(&self) -> Version {
        self.clone().with_build(Identifiers::empty())
    }

    /// The context-qualified form, `context@version`, or the bare version.
    pub fn reference(&self) -> String {
        match self.context() {
            Some(c) => format!("{c}@{self}"),
            None => self.to_string(),
        }
    }

    #[inline]
    pub(crate) fn same_tuple(&self, other: &Version) -> bool {
        self.major == other.major && self.minor == other.minor && self.patch == other.patch
    }

    /// Precedence comparison that ignores builds (used by range comparators,
    /// where a build is a channel filter rather than part of the bound).
    #[inline]
    pub(crate) fn cmp_ignoring_build(&self, other: &Version) -> Ordering {
        match self.cmp_tuple(other) {
            Ordering::Equal => cmp_pre(self.pre(), other.pre()),
            o => o,
        }
    }

    #[inline]
    fn cmp_tuple(&self, other: &Version) -> Ordering {
        self.major
            .cmp(&other.major)
            .then(self.minor.cmp(&other.minor))
            .then(self.patch.cmp(&other.patch))
    }
}

#[inline]
fn cmp_pre(a: &Identifiers, b: &Identifiers) -> Ordering {
    match (a.is_empty(), b.is_empty()) {
        (true, true) => Ordering::Equal,
        (false, true) => Ordering::Less, // a pre-release ranks below its release
        (true, false) => Ordering::Greater,
        (false, false) => a.cmp_ids(b),
    }
}

#[inline]
fn cmp_build(a: &Identifiers, b: &Identifiers) -> Ordering {
    match (a.is_empty(), b.is_empty()) {
        (true, true) => Ordering::Equal,
        (false, true) => Ordering::Greater, // a build ranks above its release
        (true, false) => Ordering::Less,
        (false, false) => a.cmp_ids(b),
    }
}

impl Ord for Version {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        match self.cmp_tuple(other) {
            Ordering::Equal => {}
            o => return o,
        }
        // The common case: two plain releases of the same tuple.
        if self.meta.is_none() && other.meta.is_none() {
            return Ordering::Equal;
        }
        cmp_pre(self.pre(), other.pre()).then_with(|| cmp_build(self.build(), other.build()))
    }
}

impl PartialOrd for Version {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl PartialEq for Version {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.cmp(other) == Ordering::Equal
    }
}

impl Eq for Version {}

impl Hash for Version {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.major.hash(state);
        self.minor.hash(state);
        self.patch.hash(state);
        self.pre().hash(state);
        for id in self.build().iter() {
            match id {
                crate::Identifier::Numeric(n) => n.trim_start_matches('0').hash(state),
                crate::Identifier::AlphaNumeric(a) => a.hash(state),
            }
        }
    }
}

impl fmt::Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{}.{}", self.major, self.minor, self.patch)?;
        if let Some(m) = &self.meta {
            if !m.pre.is_empty() {
                write!(f, "-{}", m.pre)?;
            }
            if !m.build.is_empty() {
                write!(f, "+{}", m.build)?;
            }
        }
        Ok(())
    }
}

impl FromStr for Version {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Version::parse(s)
    }
}

// ---------------------------------------------------------------- parsing

pub(crate) struct Cursor<'a> {
    pub s: &'a [u8],
    pub i: usize,
}

impl<'a> Cursor<'a> {
    pub fn new(s: &'a str) -> Self {
        Cursor {
            s: s.as_bytes(),
            i: 0,
        }
    }
    #[inline]
    pub fn peek(&self) -> Option<u8> {
        self.s.get(self.i).copied()
    }
    #[inline]
    pub fn eat(&mut self, c: u8) -> bool {
        if self.peek() == Some(c) {
            self.i += 1;
            true
        } else {
            false
        }
    }
    #[inline]
    pub fn done(&self) -> bool {
        self.i >= self.s.len()
    }
}

/// Byte classes for identifier scanning: 0 = not an identifier character,
/// 1 = letter or hyphen, 2 = digit.
const CLASS: [u8; 256] = {
    let mut t = [0u8; 256];
    let mut c = 0;
    while c < 256 {
        let b = c as u8;
        t[c] = if b.is_ascii_digit() {
            2
        } else if b.is_ascii_alphabetic() || b == b'-' {
            1
        } else {
            0
        };
        c += 1;
    }
    t
};

#[cold]
#[inline(never)]
pub(crate) fn invalid(input: &str) -> Error {
    Error::InvalidVersion(input.to_string())
}

#[cold]
#[inline(never)]
fn too_large(input: &str) -> Error {
    Error::TooLarge(input.to_string())
}

/// `0` or a non-zero digit followed by digits, at most `MAX_SAFE_INTEGER`.
#[inline(always)]
pub(crate) fn numeric(cur: &mut Cursor<'_>, input: &str) -> Result<u64, Error> {
    let s = cur.s;
    let start = cur.i;
    let mut i = start;
    let mut n: u64 = 0;
    while i < s.len() {
        let d = s[i].wrapping_sub(b'0');
        if d > 9 {
            break;
        }
        // At most 20 digits fit a u64; anything past 16 is rejected below.
        if i - start < 20 {
            n = n.wrapping_mul(10).wrapping_add(u64::from(d));
        }
        i += 1;
    }
    let len = i - start;
    if len == 0 || (len > 1 && s[start] == b'0') {
        return Err(invalid(input));
    }
    if len > 16 || n > MAX_SAFE_INTEGER {
        return Err(too_large(input));
    }
    cur.i = i;
    Ok(n)
}

/// Dot-separated identifiers starting at the cursor. With `pre`, all-digit
/// identifiers must have no leading zero and be below `MAX_SAFE_INTEGER`.
fn identifiers(cur: &mut Cursor<'_>, input: &str, pre: bool) -> Result<Identifiers, Error> {
    let s = cur.s;
    let start = cur.i;
    let mut i = start;
    loop {
        let id_start = i;
        let mut digits = true;
        while i < s.len() {
            let class = CLASS[s[i] as usize];
            if class == 0 {
                break;
            }
            digits &= class == 2;
            i += 1;
        }
        let len = i - id_start;
        if len == 0 {
            return Err(invalid(input));
        }
        if pre && digits {
            if len > 1 && s[id_start] == b'0' {
                return Err(invalid(input));
            }
            // The JavaScript reference requires num < MAX_SAFE_INTEGER.
            if len >= 16 {
                let n = s[id_start..i].iter().fold(0u64, |n, &c| {
                    n.saturating_mul(10).saturating_add(u64::from(c - b'0'))
                });
                if n >= MAX_SAFE_INTEGER {
                    return Err(too_large(input));
                }
            }
        }
        if i < s.len() && s[i] == b'.' {
            i += 1;
            continue;
        }
        break;
    }
    cur.i = i;
    // Only ASCII identifier characters were consumed.
    Ok(Identifiers::from_ascii(&s[start..i]))
}

pub(crate) fn pre_release(cur: &mut Cursor<'_>, input: &str) -> Result<Identifiers, Error> {
    identifiers(cur, input, true)
}

pub(crate) fn build(cur: &mut Cursor<'_>, input: &str) -> Result<Identifiers, Error> {
    identifiers(cur, input, false)
}

fn parse_core(s: &str, input: &str) -> Result<Version, Error> {
    let bad = || invalid(input);
    let mut cur = Cursor::new(s);
    let major = numeric(&mut cur, input)?;
    if !cur.eat(b'.') {
        return Err(bad());
    }
    let minor = numeric(&mut cur, input)?;
    if !cur.eat(b'.') {
        return Err(bad());
    }
    let patch = numeric(&mut cur, input)?;
    let pre = if cur.eat(b'-') {
        pre_release(&mut cur, input)?
    } else {
        Identifiers::empty()
    };
    let build = if cur.eat(b'+') {
        build(&mut cur, input)?
    } else {
        Identifiers::empty()
    };
    if !cur.done() {
        return Err(bad());
    }
    Ok(Version::from_parts(major, minor, patch, pre, build))
}