Changeset 39851 in project


Ignore:
Timestamp:
04/05/21 22:20:19 (2 weeks ago)
Author:
Kon Lovett
Message:

version? isa type-predicate, add version-copy, version-extend(!), version-depth+/-(!), add tests, follow naming conventions, extend doc

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

Legend:

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

    r39848 r39851  
    3232
    3333=== make-version
     34=== check-version
     35=== error-version
    3436
    3537<procedure>(make-version ELM...) --> version</procedure>
     38<procedure>(check-version LOC OBJ [NAM]) -> version</procedure>
     39<procedure>(error-version LOC OBJ [NAM])</procedure>
    3640
    3741Returns a new version with the specified elements, ELM..., and the default
    3842punctuation (see {{version-punctuation}}).
    3943
    40 ; ELM : {{(or number string symbol)}} ; version element. but {{symbol}} printname used.
     44; ELM : {{(or number string symbol)}} ; version element.
     45
     46'''Note''' that the {{symbol}} printname is used internally.
     47Returns whether trailing 0's are ignored during comparision.
     48
     49=== version-copy
     50
     51<procedure>(version-copy VER) --> version</procedure>
     52
     53Return a copy of the version, sharing only atoms.
     54
     55; VER : {{version}} ; version to copy.
     56
     57=== version?
     58
     59<procedure>(version? OBJ) --> boolean</procedure>
     60
     61; OBJ : {{*}} ; object to test.
     62
     63=== version-depth
     64
     65<procedure>(version-depth VER) --> integer</procedure>
     66
     67Return the number of elements in the version.
     68
     69; VER : {{version}} ; version to query.
     70
     71=== version-parts
     72
     73<procedure>(version-parts VER) --> list</procedure>
     74
     75Returns list of version elements.
     76
     77; VER : {{version}} ; version to query.
     78
     79=== version-puncs
     80
     81<procedure>(version-puncs VER) --> list</procedure>
     82
     83Returns list of separator characters.
     84
     85; VER : {{version}} ; version to query.
     86
     87=== version->list
     88
     89<procedure>(version->list VER) --> (list-of (or number string char))</procedure>
     90
     91Returns an "exploded" form, with version elements interleaved with punctuation
     92characeters.
     93
     94; VER : {{version}} ; version to list.
     95
     96=== list->version
     97
     98<procedure>(list->version LIS) --> version</procedure>
     99
     100Returns a version from an "exploded" form, see {{version->list}}.
     101
     102; LIS : {{(list-of (or number string symbol char))}} ; list of version components.
    41103
    42104=== version-compare
     
    85147Returns a SRFI 128 {{comparator}} for the {{version}} type.
    86148
    87 === version-valid?
    88 
    89 <procedure>(version? OBJ) --> boolean</procedure>
    90 
    91 ; OBJ : {{*}} ; object to test.
    92 
    93 === version-parse
    94 
    95 <procedure>(version-parse STR) --> version</procedure>
     149=== string->version
     150
     151<procedure>(string->version STR) --> version</procedure>
    96152
    97153Return the parsed form of the STR. The source separator (punctuation) is
    98 preserved, and restored by {{version-string}}.
     154preserved, and restored by {{version->string}}.
    99155
    100156; STR : {{string}} ; version string.
    101157
    102 === version-string
    103 
    104 <procedure>(version-string VER) --> string</procedure>
     158=== version->string
     159
     160<procedure>(version->string VER) --> string</procedure>
    105161
    106162Return the string form of the VER.
     
    108164; VER : {{version}} ; version to hash.
    109165
    110 === version-depth
    111 
    112 <procedure>(version-depth VER) --> integer</procedure>
    113 
    114 Return the number of elements in the version.
    115 
    116 ; VER : {{version}} ; version to query.
    117 
    118 === version-parts
    119 
    120 <procedure>(version-parts VER) --> list</procedure>
    121 
    122 Returns list of version elements.
    123 
    124 ; VER : {{version}} ; version to query.
    125 
    126 === version-puncs
    127 
    128 <procedure>(version-puncs VER) --> list</procedure>
    129 
    130 Returns list of separator characters.
    131 
    132 ; VER : {{version}} ; version to query.
     166=== version-depth+!
     167
     168<procedure>(version-depth+! VER CNT PART [PUNC]) -> version</procedure>
     169
     170Returns VER with more "depth", as PART, interspersed with PUNC.
     171
     172; VER : {{version}} ; version to extend.
     173; CNT : {{integer}} ; element count.
     174; PART : {{(or number string symbol)}} ; version element.
     175; PUNC : {{char}} ; version punctuation, default is as above.
     176
     177=== version-depth-!
     178
     179<procedure>(version-depth-! VER CNT) -> version</procedure>
     180
     181Returns VER with less "depth", as CNT elements are dropped, along with the
     182corresponding punctuation.
     183
     184; VER : {{version}} ; version to extend.
     185; CNT : {{integer}} ; element count.
     186
     187=== version-depth+
     188
     189<procedure>(version-depth+ VER CNT PART [PUNC]) --> version</procedure>
     190
     191Returns a copy of VER with more "depth", as PART, interspersed with PUNC.
     192
     193; VER : {{version}} ; version to extend.
     194; CNT : {{integer}} ; element count.
     195; PART : {{(or number string symbol)}} ; version element.
     196; PUNC : {{char}} ; version punctuation, default is as above.
     197
     198=== version-depth-
     199
     200<procedure>(version-depth- VER CNT) --> version</procedure>
     201
     202Returns a copy of VER with less "depth", as CNT elements are dropped, along
     203with the corresponding punctuation.
     204
     205; VER : {{version}} ; version to extend.
     206; CNT : {{integer}} ; element count.
     207
     208=== version-extend!
     209
     210<procedure>(version-extend! VER COMP...) -> version</procedure>
     211
     212Returns VER with added "depth", in the form of a new "tail", in "exploded"
     213form.
     214
     215; VER : {{version}} ; version to extend.
     216; COMP : {{(list-of (or number string symbol char))}} ; version components.
     217
     218=== version-extend
     219
     220<procedure>(version-extend VER  COMP...) --> version</procedure>
     221
     222Returns copy of VER with added "depth", in the form of a new "tail", in
     223"exploded" form.
     224
     225; VER : {{version}} ; version to extend.
     226; COMP : {{(list-of (or number string symbol char))}} ; version components.
    133227
    134228=== version-inc!
     
    188282
    189283(define (egg-repo-versions repo egg)
    190   (map! (o version-parse pathname-strip-directory)
     284  (map! (o string->version pathname-strip-directory)
    191285        (glob (make-pathname `(,repo ,egg "tags") "*"))) )
    192286
    193 (for-each (o print version-string) (sort (egg-repo-versions REPO "apropos") version<?))
     287(for-each (o print version->string) (sort (egg-repo-versions REPO "apropos") version<?))
    194288;=> prints release versions in ascending order
    195289
    196 (string=? "a.1,b" ((o version-string version-parse) "a.1,b"))
     290(string=? "a.1,b" ((o version->string string->version) "a.1,b"))
    197291;=> #t
    198292; but, assuming baseline `version-punctuation'
    199 (string=? "a.1.b" (version-string (make-version 'a 1 'b)))
     293(string=? "a.1.b" (version->string (make-version 'a 1 'b)))
    200294;=> #t
    201295
    202296(define ver1 (make-version 'a 1 'b 2))
    203 ((o print version-string) ver1)
     297((o print version->string) ver1)
    204298;=> "a.1.b.2"
    205299(version-inc! ver1)
    206 ((o print version-string) ver1)
     300((o print version->string) ver1)
    207301;=> "a.1.b.3"
    208302(version-dec! ver1 1)
    209 ((o print version-string) ver1)
     303((o print version->string) ver1)
    210304;=> "a.0.b.3"
    211305</enscript>
  • release/5/semantic-version/trunk/semantic-version.egg

    r39840 r39851  
    77 (author "Kon Lovett")
    88 (license "BSD")
    9  (dependencies srfi-1 srfi-128 (string-utils "2.5.0"))
     9 (dependencies utf8 srfi-1 srfi-128)
    1010 (test-dependencies test)
    1111 (components
  • release/5/semantic-version/trunk/semantic-version.scm

    r39848 r39851  
    77;;
    88;; - Change representation to record w/ vector "arms".
    9 ;;
    10 ;; - Add cardinality operations?
    119
    1210(module semantic-version
     
    1614  version-tail-zero
    1715  make-version
    18   version? check-version
     16  version-copy
     17  version? check-version error-version
     18  version-depth
     19  version-parts
     20  version-puncs
     21  list->version
     22  version->list
    1923  version-compare
    2024  version<? version=? version>? version<=? version>=?
     
    2327  ;version*-hash
    2428  version-comparator
    25   version-parse
    26   version-string
    27   version-depth
    28   version-parts
    29   version-puncs
     29  string->version
     30  version->string
     31  version-depth+!
     32  version-depth-!
     33  version-depth+
     34  version-depth-
     35  version-extend!
     36  version-extend
    3037  version-inc! version-dec!
    3138  version-inc version-dec)
    3239
    3340(import scheme
     41  utf8
    3442  (chicken base)
    3543  (chicken type)
    3644  (chicken condition)
    3745  (only (chicken string) ->string string-compare3)
    38   (only (srfi 1) make-list list-copy drop-while map! reverse! every)
     46  (only (srfi 1) make-list list-copy drop-while reverse! append! drop-right! every map-in-order)
     47  (only utf8-srfi-13 string-filter string-index)
    3948  (only (srfi 69) equal?-hash)
    40   (only (srfi 128) make-comparator)
    41   string-utils)
     49  (only (srfi 128) make-comparator))
    4250
    4351;;
     
    5462
    5563(: make-version         (#!rest ver-part --> ver))
    56 (: version?             (ver --> boolean))
     64(: version?             (* -> boolean : ver))
     65(: check-version        (symbol * #!optional (or string symbol) -> ver))
     66(: error-version        (symbol * #!optional (or string symbol) -> void))
     67(: version-copy         (ver --> ver))
     68(: version-depth        (ver --> integer))
     69(: version-parts        (ver --> ver-parts))
     70(: version-puncs        (ver --> ver-puncs))
     71(: list->version        ((list-of (or ver-part ver-punc)) --> ver))
     72(: version->list        (ver --> (list-of (or ver-part ver-punc))))
    5773(: version-compare      (ver ver #!optional boolean --> integer))
    5874(: version<?            (ver ver #!optional boolean --> boolean))
     
    6379(: version-hash         (ver #!rest --> integer))
    6480(: version-comparator   (--> (struct comparator)))
    65 (: version-parse        (string --> ver))
    66 (: version-string       (ver --> string))
    67 (: version-depth        (ver --> integer))
    68 (: version-parts        (ver --> ver-parts))
    69 (: version-puncs        (ver --> ver-puncs))
     81(: string->version      (string --> ver))
     82(: version->string      (ver --> string))
     83(: version-extend!      (ver #!rest (or ver-part ver-punc) -> ver))
     84(: version-extend       (ver #!rest (or ver-part ver-punc) --> ver))
     85(: version-depth+!      (ver integer ver-part #!optional ver-punc -> ver))
     86(: version-depth-!      (ver integer -> ver))
     87(: version-depth+       (ver integer ver-part #!optional ver-punc --> ver))
     88(: version-depth-       (ver integer --> ver))
    7089(: version-inc!         (ver #!optional integer number -> ver))
    7190(: version-dec!         (ver #!optional integer number -> ver))
     
    81100(define (ver-punc? x) (char? x))
    82101
    83 (define (make-ver cs ps)  (cons cs ps))
    84 (define (ver-parts v)     (car v))
    85 (define (ver-puncs v)     (cdr v))
    86 (define (ver? x)          (and (pair? x) (list? (ver-parts x)) (list? (ver-puncs x))))
    87 (define (copy-ver v)      (make-ver (list-copy (ver-parts v)) (ver-puncs v)))
     102(define (make-ver cs ps)      (cons cs ps))
     103(define (ver-parts v)         (car v))
     104(define (ver-puncs v)         (cdr v))
     105(define (ver-parts-set! v x)  (set-car! v x))
     106(define (ver-puncs-set! v x)  (set-cdr! v x))
     107(define (ver? x)              (and (pair? x) (list? (ver-parts x)) (list? (ver-puncs x))))
     108(define (copy-ver v)          (make-ver (list-copy (ver-parts v)) (list-copy (ver-puncs v))))
    88109
    89110(define (ver-parts? x) (every ver-part? x))
    90111(define (ver-puncs? x) (every ver-punc? x))
    91112
    92 (define (check-parts loc x)
    93   (unless (ver-parts? x) (error loc "invalid semantic-version parts" x))
     113(define (vererrmsg msg nam)
     114  (string-append (or (and nam (->string nam)) "bad argument") " - " msg) )
     115
     116(define (vererr loc obj msg nam)
     117  (error loc (vererrmsg msg nam) obj) )
     118
     119(define (check-parts loc x #!optional nam)
     120  (unless (ver-parts? x) (vererr loc x "invalid semantic-version parts" nam))
    94121  x )
    95122
    96 (define (check-puncs loc x)
    97   (unless (ver-puncs? x) (error loc "invalid semantic-version puncs" x))
    98   x )
    99 
    100 (define (check-version loc x)
    101   (unless (ver? x) (error loc "invalid semantic-version" x))
    102   (check-parts loc (ver-parts x))
    103   (check-puncs loc (ver-puncs x))
     123(define (check-puncs loc x #!optional nam)
     124  (unless (ver-puncs? x) (vererr loc x "invalid semantic-version puncs" nam))
    104125  x )
    105126
     
    118139  (reverse! (drop-while (lambda (x) (and (number? x) (zero? x))) (reverse ls))) )
    119140
     141(define (default-punctuation)
     142  (string-ref (version-punctuation) 0) )
     143
    120144(define (default-puncs parts)
    121   (make-list (sub1 (length parts)) (string-ref (version-punctuation) 0)) )
     145  (make-list (sub1 (length parts)) (default-punctuation)) )
    122146
    123147(define (make-version . parts)
     
    136160          (ver-parts? parts)
    137161          (ver-puncs? puncs) ) ) ) ) )
     162
     163(define (error-version loc x #!optional nam)
     164  (vererr loc x "invalid semantic-version" nam) )
     165
     166(define (check-version loc x #!optional nam)
     167  (unless (ver? x) (error-version loc x nam))
     168  (check-parts loc (ver-parts x) nam)
     169  (check-puncs loc (ver-puncs x) nam)
     170  x )
     171
     172(define (version-copy ver)
     173  (copy-ver (check-version 'version-copy ver)) )
     174
     175(define (version-depth ver)
     176  (length (ver-parts (check-version 'version-depth ver))) )
     177
     178(define (version-parts ver)
     179  (list-copy (ver-parts (check-version 'version-parts ver))) )
     180
     181(define (version-puncs ver)
     182  (list-copy (ver-puncs (check-version 'version-puncs ver))) )
     183
     184(define (version->list ver)
     185  (check-version 'version->list ver)
     186  (let loop ((puncs (ver-puncs ver)) (parts (ver-parts ver)) (ls '()))
     187    (cond
     188      ((and (null? puncs) (null? parts))
     189        (reverse! ls) )
     190      ((= (length puncs) (length parts))
     191        (loop (cdr puncs) parts (cons (car puncs) ls)) )
     192      (else
     193        (loop puncs (cdr parts) (cons (car parts) ls)) ) ) ) )
     194
     195(define (list->version ls)
     196  (define (str/num x) (if (number? x) x (->string x)))
     197  (let loop ((parts '()) (puncs '()) (ls ls))
     198    (cond
     199      ((null? ls)             (make-ver (reverse! parts) (reverse! puncs)))
     200      ((ver-part? (car ls))   (loop (cons (str/num (car ls)) parts) puncs (cdr ls)))
     201      ((ver-punc? (car ls))   (loop parts (cons (car ls) puncs) (cdr ls)))
     202      (else
     203        (error 'list->version "invalid version component" (car ls))) ) ) )
    138204
    139205(define (version-compare ver1 ver2 #!optional (tail-zero? (version-tail-zero)))
     
    188254  (make-comparator version? version=? version<? version-hash) )
    189255
    190 (define (version-parse str)
     256;string-utils like
     257;"..." => string-parts char-puncs
     258;"a.3,c" => ("a" "3" "c") (#\. #\,)
     259(define (*string-unzip str punc-str)
     260  (let (
     261    (parts (string-split str punc-str #t))
     262    (punct (string->list (string-filter (cut string-index punc-str <>) str))) )
     263    (values parts punct) ) )
     264
     265(define (string->version str)
    191266  (define (str/num x) (or (string->number x) x))
    192   (let-values (((parts puncs) (string-unzip str (version-punctuation))))
    193     (make-ver (map! str/num parts) (map! (cut string-ref <> 0) puncs)) ) )
    194 
    195 (define (version-string ver)
    196   (check-version 'version-string ver)
    197   (string-zip (map ->string (ver-parts ver)) (map string (ver-puncs ver))) )
    198 
    199 (define (version-depth ver)
    200   (length (ver-parts (check-version 'version-depth ver))) )
    201 
    202 (define (version-parts ver)
    203   (list-copy (ver-parts (check-version 'version-parts ver))) )
    204 
    205 (define (version-puncs ver)
    206   (list-copy (ver-puncs (check-version 'version-puncs ver))) )
     267  (let-values (((parts puncs) (*string-unzip str (version-punctuation))))
     268    (make-ver (map-in-order str/num parts) puncs) ) )
     269
     270(define (version->string ver)
     271  (apply string-append (map-in-order ->string (version->list (check-version 'version->string ver)))) )
     272
     273(define (version-depth+! ver cnt part #!optional (punc (default-punctuation)))
     274  (check-version 'version-depth+! ver)
     275  (check-parts 'version-depth+! (list part))
     276  (check-puncs 'version-depth+! (list punc))
     277  (ver-parts-set! ver (append! (ver-parts ver) (make-list cnt part)))
     278  ;need to include leading punct!
     279  (ver-puncs-set! ver (append! (ver-puncs ver) (make-list cnt punc)))
     280  ver )
     281
     282(define (version-depth-! ver cnt)
     283  (check-version 'version-depth-! ver)
     284  (cond
     285    ((zero? cnt)
     286      ver )
     287    ((positive? cnt)
     288      (let ((puncs (ver-puncs ver)) (parts (ver-parts ver)))
     289        (unless (<= cnt (length parts))
     290          (error 'version-depth-! "semantice-version has too little depth to drop" ver cnt) )
     291        ;be direct when dropping all
     292        (ver-parts-set! ver (if (= cnt (length parts)) '() (drop-right! parts cnt)))
     293        ;need to drop leading punctuation
     294        (ver-puncs-set! ver (if (= cnt (length parts)) '() (drop-right! puncs cnt)))
     295        ver ) )
     296    (else
     297      (error 'version-depth-! "cannot drop negative depth" ver cnt)) ) )
     298
     299(define (version-depth+ ver cnt part #!optional (punc (default-punctuation)))
     300  (version-depth+! (copy-ver (check-version 'version-depth+ ver)) cnt part punc) )
     301
     302(define (version-depth- ver cnt)
     303  (version-depth-! (copy-ver (check-version 'version-depth- ver)) cnt) )
     304
     305(define (version-extend ver . comps)
     306  (list->version (append! (version->list (check-version 'version-extend ver)) comps)) )
     307
     308(define (version-extend! ver . comps)
     309  (let ((vern (apply version-extend (check-version 'version-extend! ver) comps)))
     310    (ver-parts-set! ver (ver-parts vern))
     311    (ver-puncs-set! ver (ver-puncs vern)) )
     312  ver )
    207313
    208314;default is the last component, better be a number!
  • release/5/semantic-version/trunk/tests/semantic-version-test.scm

    r39848 r39851  
    1616
    1717;assumes 1st is always "."
    18 (test (make-version 'a 2 "c") (version-parse "a.2.c"))
     18(test (make-version 'a 2 "c") (string->version "a.2.c"))
    1919
    2020(test 3 (version-depth (make-version 'a 2 "c")))
    2121
    22 (test-assert (version? (version-parse "a.2,c")))
    23 (test "a.2,c" (version-string (version-parse "a.2,c")))
     22(test-assert (version? (string->version "a.2,c")))
     23(test "a.2,c" (version->string (string->version "a.2,c")))
    2424(let ((ver "a.2,c"))
    25   (test '("a" 2 "c") (version-parts (version-parse ver)))
    26   (test '(#\. #\,) (version-puncs (version-parse ver))) )
     25  (test '("a" 2 "c") (version-parts (string->version ver)))
     26  (test '(#\. #\,) (version-puncs (string->version ver))) )
    2727
    2828(test "inc of -# is dec" (make-version "a" 1 "c") (version-inc (make-version "a" 2 "c") 1 -1))
     
    3535(test-assert (negative? (version-compare (make-version 1 2 3) (make-version 1 2 3 0) #t)))
    3636(test-assert (zero? (version-compare (make-version 1 2 3) (make-version 1 2 3 0))))
     37
     38(test '("a" #\. 2 #\, "c") (version->list (list->version '("a" #\. 2 #\, "c"))))
     39
     40(test (string->version "a.2.c,27-X") (version-extend (make-version 'a 2 "c") #\, 27 #\- 'X))
     41
     42(test (make-version 'a 2 "c" 0 0) (version-depth+ (make-version 'a 2 "c") 2 0))
     43(test (make-version 'a) (version-depth- (make-version 'a 2 "c") 2))
    3744
    3845;;
Note: See TracChangeset for help on using the changeset viewer.