summaryrefslogtreecommitdiff
path: root/ldp/etag.scm
diff options
context:
space:
mode:
Diffstat (limited to 'ldp/etag.scm')
-rw-r--r--ldp/etag.scm43
1 files changed, 43 insertions, 0 deletions
diff --git a/ldp/etag.scm b/ldp/etag.scm
new file mode 100644
index 0000000..756766e
--- /dev/null
+++ b/ldp/etag.scm
@@ -0,0 +1,43 @@
+(define-module (ldp etag))
+
+(define alphabet
+ (string-join
+ '("abcdefghijklmnopqrstuvwxyz"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "0123456789"
+ "-_")
+ ""))
+
+(define (generate-etag-letter)
+ (string-ref alphabet (random (string-length alphabet))))
+
+(define (generate-etag-letters n)
+ (if (<= n 0)
+ '()
+ (cons (generate-etag-letter)
+ (generate-etag-letters (- n 1)))))
+
+(define-public (generate-etag)
+ (list->string (generate-etag-letters 16)))
+
+(define-public (etag? x)
+ (define (aux i)
+ (or (>= i (string-length x))
+ (and (let ((c (string-ref x i)))
+ (or (and (char>=? c #\a) (char<=? c #\z))
+ (and (char>=? c #\A) (char<=? c #\Z))
+ (and (char>=? c #\0) (char<=? c #\9))
+ (char=? c #\-)
+ (char=? c #\_)))
+ (aux (+ i 1)))))
+ (and (string? x)
+ (aux 0)))
+
+(define-public (the-etag x)
+ (unless (etag? x)
+ (scm-error 'wrong-type-arg
+ "the-etag"
+ "Expected a string satisfying etag? from (ldp etag)."
+ '()
+ (list x)))
+ x)