semver.c

repository·master·Indexed 41 days ago

https://github.com/h2non/semver.c

A lightweight, zero-dependency, standard-compliant Semantic Versioning (v2.0) parser and renderer written in ANSI C. It provides tools for parsing strings into semver_t structures, comparing versions via semver_compare, and checking range satisfaction using operators like caret (^) and tilde (~). Includes utility functions for bumping versions, rendering to strings, and memory management via semver_free.

Tokens
1.6K
Snippets
3
Records
11
Agent score
2%

What's inside semver.c

  1. Compare two semantic versions

    master

    To compare two versions, parse them into semver_t structures using semver_parse and then use semver_compare.

    semver_compare returns:

    • -1 if the first version is lower than the second.
    • 0 if the versions are equal.
    • 1 if the first version is higher than the second.

    Note: Always call semver_free on your semver_t objects to release allocated memory.

    #include <stdio.h>
    #include <semver.h>
    
    char current[] = "1.5.10";
    char compare[] = "2.3.0";
    
    int
    main(int argc, char *argv[]) {
        semver_t current_version = {};
        semver_t compare_version = {};
    
        if (semver_parse(current, &current_version)
          || semver_parse(compare, &compare_version)) {
          fprintf(stderr,"Invalid semver string\n");
          return -1;
        }
    
        int resolution = semver_compare(compare_version, current_version);
    
        if (resolution == 0) {
          printf("Versions %s is equal to: %s\n", compare, current);
        }
        else if (resolution == -1) {
          printf("Version %s is lower than: %s\n", compare, current);
        }
        else {
          printf("Version %s is higher than: %s\n", compare, current);
        }
    
        semver_free(&current_version);
        semver_free(&compare_version);
        return 0;
    }
  2. Check if a version satisfies a range

    master

    Use semver_satisfies to check if a version matches a specific range defined by an operator.

    Supported operators:

    • = (Equality)
    • >= (Higher or equal to)
    • <= (Lower or equal to)
    • < (Lower than)
    • > (Higher than)
    • ^ (Caret operator)
    • ~ (Tilde operator)

    semver_satisfies returns 1 if the condition is met and 0 otherwise.

    #include <stdio.h>
    #include <semver.h>
    
    semver_t current = {};
    semver_t compare = {};
    
    int
    main(int argc, char *argv[]) {
        semver_parse("1.3.10", &current);
        semver_parse("1.5.2", &compare);
    
        // Use caret operator for the comparison
        char operator[] = "^";
    
        if (semver_satisfies(current, compare, operator)) {
          printf("Version %s can be satisfied by %s", "1.3.10", "1.5.2");
        }
    
        semver_free(&current);
        semver_free(&compare);
        return 0;
    }
  3. Parse a semver string

    master

    The semver_parse function converts a string into a semver_t structure.

    Signature: int semver_parse(const char *str, semver_t *ver)

    Returns:

    • 0 on success.
    • -1 if the string is an invalid semver expression or a parsing error occurs.
  4. Boolean comparison helpers

    master

    The library provides several shorthand functions for direct boolean comparisons between two semver_t versions:

    • semver_eq(semver_t a, semver_t b): Equality
    • semver_ne(semver_t a, semver_t b): Non-equal
    • semver_gt(semver_t a, semver_t b): Greater than
    • semver_lt(semver_t a, semver_t b): Lower than
    • semver_gte(semver_t a, semver_t b): Greater than or equal
    • semver_lte(semver_t a, semver_t b): Lower than or equal
  5. Specialized satisfaction checks: Caret and Tilde

    master

    The library provides optimized functions for common range checks:

    • semver_satisfies_caret(semver_t a, semver_t b): Checks if version a is satisfied by b using the caret (^) operator logic.
    • semver_satisfies_patch(semver_t a, semver_t b): Checks if version a is satisfied by b using the tilde (~) operator logic.
  6. Render and manipulate versions

    master

    Functions for converting versions to strings, numeric values, or bumping version numbers:

    • semver_render(semver_t *v, char *dest): Renders the version into the provided dest string.
    • semver_numeric(semver_t *v): Returns a numeric representation of the version, useful for sorting/filtering.
    • semver_bump(semver_t *a): Increments the major version.
    • semver_bump_minor(semver_t *a): Increments the minor version.
    • semver_bump_patch(semver_t *a): Increments the patch version.
  7. Check version satisfaction with semver_satisfies

    master

    Checks if version a satisfies the range defined by version b using a specific operator.

    Signature: int semver_satisfies(semver_t a, semver_t b, char *operator)

    Allowed operators:

    • = (Equality)
    • >= (Higher or equal to)
    • <= (Lower or equal to)
    • < (Lower than)
    • > (Higher than)
    • ^ (Caret operator)
    • ~ (Tilde operator)

    Returns:

    • 1 if satisfied.
    • 0 if not satisfied.
  8. Memory management and validation

    master

    Utility functions for cleaning up and validating semver data:

    • semver_free(semver_t *a): Frees heap-allocated memory (like prerelease or metadata) within the semver_t struct. Must be called to avoid leaks.
    • semver_is_valid(char *str): Returns non-zero if the string is a valid semver expression.
    • semver_clean(char *str): Removes invalid semver characters from a string in-place.