Changeset 39833 in project


Ignore:
Timestamp:
04/05/21 00:39:03 (6 weeks ago)
Author:
Kon Lovett
Message:

rename split/combine parse/string

Location:
release/5/semantic-version/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • release/5/semantic-version/trunk/semantic-version.scm

    r39832 r39833  
    77  version-punctuation
    88  make-version
     9  version-valid?
    910  version-compare
    10   version-split
    11   version-combine
    12   version-valid?
     11  version-parse
     12  version-string
    1313  version-inc!
    1414  version-dec!
     
    2020  (chicken type)
    2121  (chicken condition)
    22   (only (chicken string) string-compare3)
     22  (only (chicken string) ->string string-compare3)
    2323  (only (srfi 1) make-list list-copy drop-while map! reverse! every)
    2424  string-utils)
     
    2828;FIXME convert # <-> S on split & combine, so not in inc & comp?
    2929
    30 (define-type semver-part  (or number string))
    31 (define-type semver-parts (list-of semver-part))
    32 (define-type semver-puncs (list-of string))
    33 (define-type semver       (pair semver-parts semver-puncs))
     30(define-type ver-part  (or number string))
     31(define-type ver-punc  char)
     32(define-type ver-parts (list-of ver-part))
     33(define-type ver-puncs (list-of ver-punc))
     34(define-type ver       (pair ver-parts ver-puncs))
    3435
    35 (: version-punctuation (#!optional string -> string))
    36 
    37 (: make-version (#!rest semver-part -> semver))
    38 
    39 (: version-compare (semver semver #!optional --> integer))
    40 
    41 (: version-valid? (semver --> boolean))
    42 
    43 (: version-split (string -> semver))
    44 (: version-combine (semver -> string))
    45 
    46 (: version-inc! (semver #!optional integer integer -> semver))
    47 (: version-dec! (semver #!optional integer integer -> semver))
    48 
    49 (: version-inc (semver #!optional integer integer -> semver))
    50 (: version-dec (semver #!optional integer integer -> semver))
     36(: version-punctuation  (#!optional string -> string))
     37(: make-version         (#!rest ver-part -> ver))
     38(: version-compare      (ver ver #!rest --> integer))
     39(: version-valid?       (ver --> boolean))
     40(: version-parse        (string -> ver))
     41(: version-string      (ver -> string))
     42(: version-inc!         (ver #!optional integer number -> ver))
     43(: version-dec!         (ver #!optional integer number -> ver))
     44(: version-inc          (ver #!optional integer number -> ver))
     45(: version-dec          (ver #!optional integer number -> ver))
    5146
    5247;;
     48
     49;semantic-version type
     50
     51(define (ver-part? x) (or (string? x) (number? x)))
     52(define (ver-punc? x) (char? x))
     53
     54(define (make-ver cs ps)  (cons cs ps))
     55(define (ver-parts v)     (car v))
     56(define (ver-puncs v)     (cdr v))
     57(define (ver? x)          (and (pair? x) (list? (ver-parts x)) (list? (ver-puncs x))))
     58(define (copy-ver v)      (make-ver (list-copy (ver-parts v)) (ver-puncs v)))
     59
     60;
    5361
    5462;"!?@#$%^&*-_+=|/\\;:,. "
     
    5967(define version-punctuation (make-parameter VERSION-PUNCT))
    6068
    61 (define (make-ver cs ps) (cons cs ps))
    62 (define (ver-parts v) (car v))
    63 (define (ver-puncs v) (cdr v))
    64 (define (ver? x) (and (pair? x) (list? (ver-parts x)) (list? (ver-puncs x))))
    65 
    66 (define (ver-part? x) (or (string? x) (number? x)))
     69(define (drop-tailing-zeros ls)
     70  (reverse! (drop-while (lambda (x) (and (number? x) (zero? x))) (reverse ls))) )
    6771
    6872(define (default-puncs parts)
    69   (make-list (sub1 (length parts)) (string (string-ref (version-punctuation) 0))) )
     73  (make-list (sub1 (length parts)) (string-ref (version-punctuation) 0)) )
    7074
    7175(define (make-version . parts)
     
    7377    (make-ver parts (default-puncs parts))) )
    7478
    75 ;semver1 semver2 => <0 | 0 | >0
     79;ver => ?
     80(define (version-valid? ver)
     81  (and
     82    (ver? ver)
     83    (let ((parts (ver-parts ver)) (puncs (ver-puncs ver)))
     84      (and
     85        (or
     86          (and (null? puncs) (null? parts))
     87          (and
     88            (= (length puncs) (sub1 (length parts)))
     89            (every ver-part? parts)
     90            (every char? puncs) ) ) ) ) ) )
     91
     92;ver1 ver2 => <0 | 0 | >0
    7693(define (version-compare ver1 ver2 #!key (tail-zero? #f))
    77   (define (tail-zeros p)
    78     (if tail-zero? p
    79       (reverse! (drop-while (lambda (x) (and (number? x) (zero? x))) (reverse p)))) )
     94  (define (tail-zeros ls) (if tail-zero? ls (drop-tailing-zeros ls)))
    8095  (let loop ((p1 (tail-zeros (ver-parts ver1))) (p2 (tail-zeros (ver-parts ver2))))
    8196    (cond
     
    94109            cmp ) ) ) ) ) )
    95110
    96 ;semver => ?
    97 (define (version-valid? ver)
    98   (and
    99     (ver? ver)
    100     (let ((parts (ver-parts ver)) (puncs (ver-puncs ver)))
    101       (and
    102         (or
    103           (and (null? puncs) (null? parts))
    104           (and
    105             (= (length puncs) (sub1 (length parts)))
    106             (every ver-part? parts)
    107             (every string? puncs) ) ) ) ) ) )
    108 
    109 ;"..." => semver
    110 (define (version-split str)
     111;"..." => ver
     112(define (version-parse str)
    111113  (define (str/num x) (or (string->number x) x))
    112114  (let-values (((parts puncs) (string-unzip str (version-punctuation))))
    113     (make-ver (map! str/num parts) puncs) ) )
     115    (make-ver (map! str/num parts) (map! (cut string-ref <> 0) puncs)) ) )
    114116
    115 ;semver => "..."
    116 (define (version-combine ver)
    117   (define (->string x) (if (number? x) (number->string x) x))
    118   (let ((parts (ver-parts ver)) (puncs (ver-puncs ver)))
    119     (string-zip (map ->string parts) puncs)) )
     117;ver => "..."
     118(define (version-string ver)
     119  (string-zip (map ->string (ver-parts ver)) (map string (ver-puncs ver))) )
    120120
    121 ;inc/dec nth ver - "IVX"/"ivx" & "abc"/"ABC"
     121;iNOTE nc/dec nth ver - "IVX"/"ivx" & "a"/"A"
    122122
    123123(define (version-inc! ver #!optional (idx (sub1 (length (ver-parts ver)))) (amt 1))
     
    132132
    133133(define (version-inc ver #!optional (idx (sub1 (length (ver-parts ver)))) (amt 1))
    134   (version-inc! (make-ver (list-copy (ver-parts ver)) (ver-puncs ver)) idx amt) )
     134  (version-inc! (copy-ver ver) idx amt) )
    135135
    136136(define (version-dec ver #!optional (idx (sub1 (length (ver-parts ver)))) (amt 1))
    137   (version-dec! (make-ver (list-copy (ver-parts ver)) (ver-puncs ver)) idx amt) )
     137  (version-dec! (copy-ver ver) idx amt) )
    138138
    139139) ;semantic-version
  • release/5/semantic-version/trunk/tests/semantic-version-test.scm

    r39832 r39833  
    1313
    1414;assumes 1st is always "."
    15 (test (make-version "a" 2 "c") (version-split "a.2.c"))
     15(test (make-version "a" 2 "c") (version-parse "a.2.c"))
    1616
    17 (test-assert (version-valid? (version-split "a.2,c")))
    18 (test "a.2,c" (version-combine (version-split "a.2,c")))
     17(test-assert (version-valid? (version-parse "a.2,c")))
     18(test "a.2,c" (version-string (version-parse "a.2,c")))
    1919
    2020(test (make-version "a" 1 "c") (version-inc (make-version "a" 2 "c") 1 -1))
Note: See TracChangeset for help on using the changeset viewer.