-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaux.rkt
32 lines (26 loc) · 1.04 KB
/
aux.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#lang racket
;; Aux - helper routines that do not fit anywhere else
(provide box-swap! string-trim-length)
;; no contract for box-swap! since this is a performance primtitive
(define (box-swap! box f . args)
(let loop ()
(let* ([old (unbox box)]
[new (apply f old args)])
(if (box-cas! box old new)
new
(loop)))))
(define/contract (string-trim-length str len)
(-> string? exact-positive-integer? any)
(define s (string-trim str))
(cond
((< len 6) (substring s 0 len))
((> (string-length s) (- len 6))
(string-append (substring s 0 (- len 6)) " [...]"))
(else s)))
(module+ test
(require rackunit)
(check-equal? (string-trim-length "This is a somewhat long test string." 20) "This is a some [...]")
(check-equal? (string-trim-length "This is a somewhat long test string." 200) "This is a somewhat long test string.")
(check-equal? (string-trim-length "This is a somewhat long test string." 1) "T")
(check-exn exn:fail:contract? (lambda () (string-trim-length "hello world" 0)))
)