From 6c7b6a51a4ea8fd05b85a5227c2333d4d9026f38 Mon Sep 17 00:00:00 2001 From: Mark H Weaver Date: Tue, 12 Feb 2013 12:02:15 -0500 Subject: Add version-compare and version>? to utils.scm. * guix/utils.scm (version-compare, version>?): New exported procedures, based on version-string>?, which was formerly in gnu-maintenance.scm. * guix/gnu-maintenance.scm (version-string>?): Removed procedure. (latest-release): Use 'version>?' instead of 'version-string>?'. --- guix/utils.scm | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'guix/utils.scm') diff --git a/guix/utils.scm b/guix/utils.scm index 7ab835e7f1..d7c37e37d1 100644 --- a/guix/utils.scm +++ b/guix/utils.scm @@ -57,6 +57,8 @@ gnu-triplet->nix-system %current-system + version-compare + version>? package-name->name+version)) @@ -422,6 +424,24 @@ returned by `config.guess'." ;; By default, this is equal to (gnu-triplet->nix-system %host-type). (make-parameter %system)) +(define version-compare + (let ((strverscmp + (let ((sym (or (dynamic-func "strverscmp" (dynamic-link)) + (error "could not find `strverscmp' (from GNU libc)")))) + (pointer->procedure int sym (list '* '*))))) + (lambda (a b) + "Return '> when A denotes a newer version than B, +'< when A denotes a older version than B, +or '= when they denote equal versions." + (let ((result (strverscmp (string->pointer a) (string->pointer b)))) + (cond ((positive? result) '>) + ((negative? result) '<) + (else '=)))))) + +(define (version>? a b) + "Return #t when A denotes a newer version than B." + (eq? '> (version-compare a b))) + (define (package-name->name+version name) "Given NAME, a package name like \"foo-0.9.1b\", return two values: \"foo\" and \"0.9.1b\". When the version part is unavailable, NAME and -- cgit v1.2.3 From 2bcfb9e065ac6abb6abf7ac9a263ba3c4d70124f Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Sun, 28 Apr 2013 21:38:40 +0200 Subject: utils: Add `string-tokenize*'. * guix/utils.scm (string-tokenize*): New procedure. * tests/utils.scm ("string-tokenize*"): New test. --- guix/utils.scm | 28 ++++++++++++++++++++++++++++ tests/utils.scm | 10 ++++++++++ 2 files changed, 38 insertions(+) (limited to 'guix/utils.scm') diff --git a/guix/utils.scm b/guix/utils.scm index 3cbed2fd0f..0b09affffd 100644 --- a/guix/utils.scm +++ b/guix/utils.scm @@ -60,6 +60,7 @@ version-compare version>? package-name->name+version + string-tokenize* file-extension call-with-temporary-output-file fold2)) @@ -471,6 +472,33 @@ introduce the version part." (let ((dot (string-rindex file #\.))) (and dot (substring file (+ 1 dot) (string-length file))))) +(define (string-tokenize* string separator) + "Return the list of substrings of STRING separated by SEPARATOR. This is +like `string-tokenize', but SEPARATOR is a string." + (define (index string what) + (let loop ((string string) + (offset 0)) + (cond ((string-null? string) + #f) + ((string-prefix? what string) + offset) + (else + (loop (string-drop string 1) (+ 1 offset)))))) + + (define len + (string-length separator)) + + (let loop ((string string) + (result '())) + (cond ((index string separator) + => + (lambda (offset) + (loop (string-drop string (+ offset len)) + (cons (substring string 0 offset) + result)))) + (else + (reverse (cons string result)))))) + (define (call-with-temporary-output-file proc) "Call PROC with a name of a temporary file and open output port to that file; close the file and delete it when leaving the dynamic extent of this diff --git a/tests/utils.scm b/tests/utils.scm index fa7d7b03fd..97547a6d62 100644 --- a/tests/utils.scm +++ b/tests/utils.scm @@ -64,6 +64,16 @@ ("nixpkgs" "1.0pre22125_a28fe19") ("gtk2" "2.38.0")))) +(test-equal "string-tokenize*" + '(("foo") + ("foo" "bar" "baz") + ("foo" "bar" "") + ("foo" "bar" "baz")) + (list (string-tokenize* "foo" ":") + (string-tokenize* "foo;bar;baz" ";") + (string-tokenize* "foo!bar!" "!") + (string-tokenize* "foo+-+bar+-+baz" "+-+"))) + (test-equal "fold2, 1 list" (list (reverse (iota 5)) (map - (reverse (iota 5)))) -- cgit v1.2.3