(module version-misc mzscheme
(require (lib "string.ss")
(lib "list.ss")
(lib "contract.ss")
(prefix 67: (lib "67.ss" "srfi"))
(prefix 1: (lib "1.ss" "srfi")))
(provide/contract [version<= (string? string? . -> . boolean?)]
[version< (string? string? . -> . boolean?)]
[version= (string? string? . -> . boolean?)]
[version>= (string? string? . -> . boolean?)]
[version> (string? string? . -> . boolean?)])
(define-struct mz-version (numbers) #f)
(define (string->version str)
(cond
[(regexp-match #rx"^([0-9][0-9][0-9])([.0-9]*)$" str)
=>
(lambda (ver)
(let* ([major (string->number (list-ref ver 1))]
[after-major
(map string->number
(rest (regexp-split "\\." (list-ref ver 2))))]
[minor (if (>= (length after-major) 1)
(first after-major)
0)]
[maintenances (drop after-major 1)])
(make-mz-version (list*
(remainder (quotient major 100) 10)
(remainder (quotient major 10) 10)
(remainder major 10)
minor
maintenances))))]
[(regexp-match #rx"^([.0-9]*)$" str)
=>
(lambda (ver)
(let* ([numbers (regexp-split "\\." (list-ref ver 1))])
(make-mz-version (map string->number numbers))))]
[else #f]))
(define (drop a-list n)
(1:drop a-list (min n (length a-list))))
(define (version-cmp v1 v2)
(67:list-compare 67:integer-compare
(mz-version-numbers v1)
(mz-version-numbers v2)))
(define (version<= a b)
(let ([a (string->version a)]
[b (string->version b)])
(not (= (version-cmp a b)
1))))
(define (version>= a b)
(let ([a (string->version a)]
[b (string->version b)])
(not (= (version-cmp a b)
-1))))
(define (version= a b)
(let ([a (string->version a)]
[b (string->version b)])
(= (version-cmp a b)
0)))
(define (version< a b)
(let ([a (string->version a)]
[b (string->version b)])
(= (version-cmp a b)
-1)))
(define (version> a b)
(let ([a (string->version a)]
[b (string->version b)])
(= (version-cmp a b)
1))))