nydp 0.4.2 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +44 -0
  3. data/lib/lisp/core-010-precompile.nydp +13 -16
  4. data/lib/lisp/core-012-utils.nydp +3 -2
  5. data/lib/lisp/core-015-documentation.nydp +54 -23
  6. data/lib/lisp/core-017-builtin-dox.nydp +14 -12
  7. data/lib/lisp/core-020-utils.nydp +5 -5
  8. data/lib/lisp/core-030-syntax.nydp +166 -72
  9. data/lib/lisp/core-035-flow-control.nydp +38 -11
  10. data/lib/lisp/core-037-list-utils.nydp +12 -0
  11. data/lib/lisp/core-039-module.nydp +24 -0
  12. data/lib/lisp/core-040-utils.nydp +32 -12
  13. data/lib/lisp/core-041-string-utils.nydp +25 -1
  14. data/lib/lisp/core-042-date-utils.nydp +21 -1
  15. data/lib/lisp/core-043-list-utils.nydp +96 -64
  16. data/lib/lisp/core-070-prefix-list.nydp +1 -1
  17. data/lib/lisp/core-080-pretty-print.nydp +57 -17
  18. data/lib/lisp/core-090-hook.nydp +35 -1
  19. data/lib/lisp/core-100-utils.nydp +82 -2
  20. data/lib/lisp/core-110-hash-utils.nydp +56 -2
  21. data/lib/lisp/core-120-settings.nydp +16 -5
  22. data/lib/lisp/core-130-validations.nydp +51 -0
  23. data/lib/lisp/core-900-benchmarking.nydp +78 -20
  24. data/lib/lisp/tests/accum-examples.nydp +28 -1
  25. data/lib/lisp/tests/aif-examples.nydp +8 -3
  26. data/lib/lisp/tests/andify-examples.nydp +7 -0
  27. data/lib/lisp/tests/at-syntax-examples.nydp +17 -0
  28. data/lib/lisp/tests/best-examples.nydp +9 -0
  29. data/lib/lisp/tests/builtin-tests.nydp +19 -0
  30. data/lib/lisp/tests/case-examples.nydp +14 -0
  31. data/lib/lisp/tests/date-examples.nydp +54 -1
  32. data/lib/lisp/tests/destructuring-examples.nydp +46 -14
  33. data/lib/lisp/tests/detect-examples.nydp +12 -0
  34. data/lib/lisp/tests/dp-examples.nydp +24 -0
  35. data/lib/lisp/tests/each-tests.nydp +5 -0
  36. data/lib/lisp/tests/empty-examples.nydp +1 -1
  37. data/lib/lisp/tests/error-tests.nydp +4 -4
  38. data/lib/lisp/tests/explain-mac-examples.nydp +1 -1
  39. data/lib/lisp/tests/filter-forms-examples.nydp +15 -0
  40. data/lib/lisp/tests/hash-examples.nydp +25 -1
  41. data/lib/lisp/tests/list-grep-examples.nydp +40 -0
  42. data/lib/lisp/tests/list-tests.nydp +58 -1
  43. data/lib/lisp/tests/map-hash-examples.nydp +11 -0
  44. data/lib/lisp/tests/module-examples.nydp +10 -0
  45. data/lib/lisp/tests/multi-assign-examples.nydp +6 -0
  46. data/lib/lisp/tests/parser-tests.nydp +25 -0
  47. data/lib/lisp/tests/pretty-print-tests.nydp +17 -14
  48. data/lib/lisp/tests/set-difference-examples.nydp +8 -0
  49. data/lib/lisp/tests/settings-examples.nydp +17 -1
  50. data/lib/lisp/tests/string-tests.nydp +70 -1
  51. data/lib/lisp/tests/syntax-tests.nydp +5 -1
  52. data/lib/lisp/tests/to-integer-examples.nydp +16 -0
  53. data/lib/lisp/tests/validation-examples.nydp +15 -0
  54. data/lib/nydp.rb +10 -3
  55. data/lib/nydp/assignment.rb +10 -3
  56. data/lib/nydp/builtin.rb +1 -1
  57. data/lib/nydp/builtin/abs.rb +8 -0
  58. data/lib/nydp/builtin/date.rb +15 -1
  59. data/lib/nydp/builtin/error.rb +1 -1
  60. data/lib/nydp/builtin/hash.rb +24 -1
  61. data/lib/nydp/builtin/inspect.rb +1 -1
  62. data/lib/nydp/builtin/plus.rb +10 -2
  63. data/lib/nydp/builtin/random_string.rb +2 -2
  64. data/lib/nydp/builtin/{car.rb → regexp.rb} +2 -2
  65. data/lib/nydp/builtin/ruby_wrap.rb +72 -0
  66. data/lib/nydp/builtin/string_match.rb +2 -2
  67. data/lib/nydp/builtin/string_pad_left.rb +7 -0
  68. data/lib/nydp/builtin/string_pad_right.rb +7 -0
  69. data/lib/nydp/builtin/string_replace.rb +3 -3
  70. data/lib/nydp/builtin/string_split.rb +4 -3
  71. data/lib/nydp/builtin/to_integer.rb +23 -0
  72. data/lib/nydp/builtin/to_string.rb +2 -9
  73. data/lib/nydp/builtin/type_of.rb +9 -6
  74. data/lib/nydp/closure.rb +0 -3
  75. data/lib/nydp/cond.rb +23 -1
  76. data/lib/nydp/context_symbol.rb +14 -6
  77. data/lib/nydp/core.rb +36 -28
  78. data/lib/nydp/core_ext.rb +21 -5
  79. data/lib/nydp/date.rb +26 -18
  80. data/lib/nydp/function_invocation.rb +34 -26
  81. data/lib/nydp/helper.rb +35 -3
  82. data/lib/nydp/interpreted_function.rb +68 -40
  83. data/lib/nydp/literal.rb +1 -1
  84. data/lib/nydp/pair.rb +22 -5
  85. data/lib/nydp/parser.rb +11 -7
  86. data/lib/nydp/string_atom.rb +3 -4
  87. data/lib/nydp/symbol_lookup.rb +7 -7
  88. data/lib/nydp/tokeniser.rb +2 -2
  89. data/lib/nydp/truth.rb +10 -10
  90. data/lib/nydp/version.rb +1 -1
  91. data/lib/nydp/vm.rb +7 -0
  92. data/nydp.gemspec +2 -4
  93. data/spec/date_spec.rb +93 -0
  94. data/spec/embedded_spec.rb +12 -12
  95. data/spec/foreign_hash_spec.rb +14 -2
  96. data/spec/hash_non_hash_behaviour_spec.rb +7 -7
  97. data/spec/hash_spec.rb +24 -2
  98. data/spec/nydp_spec.rb +14 -2
  99. data/spec/pair_spec.rb +3 -1
  100. data/spec/parser_spec.rb +31 -20
  101. data/spec/rand_spec.rb +3 -3
  102. data/spec/spec_helper.rb +10 -1
  103. metadata +24 -37
  104. data/lib/nydp/builtin/cdr.rb +0 -7
  105. data/lib/nydp/builtin/cons.rb +0 -9
@@ -2,3 +2,8 @@
2
2
  ("iterates over each item in list"
3
3
  (let acc 0 (each x '(1 2 3 4) (assign acc (+ x acc))) acc)
4
4
  10))
5
+
6
+ (examples-for each-with-index
7
+ ("iterates over each item in list"
8
+ (returnlet acc nil (each-with-index i x '(a b c d) (push (list i x) acc)))
9
+ ( (3 d) (2 c) (1 b) (0 a) )))
@@ -18,7 +18,7 @@
18
18
  ("nonzero" (present? "foo-bar") t)
19
19
  ("zero" (present? "") nil)
20
20
  ("blank" (present? " ") t)
21
- ("nil is t" (present? nil) nil)
21
+ ("nil is nil" (present? nil) nil)
22
22
  ("sym is t" (present? 'abc) t)
23
23
  ("number is t" (present? 234) t)
24
24
 
@@ -20,10 +20,10 @@
20
20
  (on-err (error "bar")
21
21
  (on-err (error "toto")
22
22
  (error "primum errorum")))))
23
- "\"foo\"
24
- \"bar\"
25
- \"toto\"
26
- \"primum errorum\"")
23
+ "foo
24
+ bar
25
+ toto
26
+ primum errorum")
27
27
 
28
28
  ("handles errors but any ensuring clause gets called first"
29
29
  (with (x nil y nil)
@@ -13,7 +13,7 @@
13
13
 
14
14
  ("expands thrice for n = 3"
15
15
  (explain-mac 3 '(afn (a) (let b (+ 2 a) (* b b))))
16
- (with (self nil) (assign self (fn (a) (let b (+ 2 a) (* b b))))))
16
+ ((fun (self) (assign self (fn (a) (let b (+ 2 a) (* b b))))) nil))
17
17
 
18
18
  ("expands four times for n = 4"
19
19
  (explain-mac 4 '(afn (a) (let b (+ 2 a) (* b b))))
@@ -1,3 +1,18 @@
1
+ (examples-for filter-remove
2
+ ("removes (memoise) from a list of forms"
3
+ (filter-remove 'memoise '((trace)
4
+ ;; this is a comment
5
+ (memoise)
6
+ (let x a
7
+ (+ x b)
8
+ (bingo dingbat))))
9
+ (((memoise))
10
+ ((trace)
11
+ (comment "this is a comment")
12
+ (let x a
13
+ (+ x b)
14
+ (bingo dingbat))))))
15
+
1
16
  (examples-for filter-forms
2
17
  ("groups forms by their 'car if the 'car is a key in the given hash"
3
18
  (let ff (filter-forms { car nil comment nil mac nil }
@@ -1,3 +1,20 @@
1
+ (examples-for hash-replace-keys
2
+ ("changes strings to symbols"
3
+ (let h (hash-replace-keys λk(sym k) { "a" 1 "b" 2 "c" 3 })
4
+ (list (hash-keys h) (hash-values h)))
5
+ ((a b c) (1 2 3)))
6
+
7
+ ("appends a character to keys"
8
+ (let h (hash-replace-keys λk(string-pieces k "!") { "a" 1 "b" 2 "c" 3 })
9
+ (list (hash-keys h) (hash-values h)))
10
+ (("a!" "b!" "c!") (1 2 3))))
11
+
12
+ (examples-for hash
13
+ ("builds a hash"
14
+ (let h (hash "a" 1 'b 2 '(x y) 3)
15
+ (list (hash-keys h) (hash-values h)))
16
+ (("a" b (x y)) (1 2 3))))
17
+
1
18
  (examples-for hash-values
2
19
  ("returns the list of values of the given hash"
3
20
  (hash-values { a 1 b 2 c 'c d "hello" })
@@ -10,6 +27,12 @@
10
27
  (" t for a hash" (hash? {a 1 b 2}) t )
11
28
  ("nil for a symbol" (hash? 'foo) nil))
12
29
 
30
+ (examples-for hash-slice
31
+ ("returns a new hash containing only the specified keys"
32
+ (let h (hash-slice { a 1 b 2 c 3 d 4 } '(a b e f))
33
+ (list (hash-keys h) (hash-values h)))
34
+ ((a b) (1 2))))
35
+
13
36
  (examples-for hash-merge
14
37
  ("merge with symbol keys"
15
38
  (let h (hash-merge { a 1 b 2 c 3 } { a 99 c 98 d 97 })
@@ -29,7 +52,8 @@
29
52
 
30
53
  ("returns date operations"
31
54
  (sort:hash-keys (date 2015 11 18))
32
- (beginning_of_month
55
+ (age
56
+ beginning_of_month
33
57
  beginning_of_week
34
58
  beginning_of_year
35
59
  day
@@ -0,0 +1,40 @@
1
+ (examples-for list/grep
2
+ ("it finds strings"
3
+ (list/grep
4
+ string?
5
+ (list 'foo "bar" '(a b "c d" e) { ignores "hash elements" }))
6
+ ("bar" "c d"))
7
+
8
+ ("it finds items whose car is carryon"
9
+ (list/grep
10
+ λf(caris 'carryon f)
11
+ '(foo
12
+ (carryon 1 2 3 42)
13
+ (bar (40 41 42 (carryon 43 (carryon 42) 41)))
14
+ 42
15
+ 99))
16
+ ((carryon 1 2 3 42)
17
+ (carryon 43 (carryon 42) 41)
18
+ (carryon 42)))
19
+
20
+ ("it finds items of length 3"
21
+ (list/grep
22
+ λf(eq? 3 (len f))
23
+ '(foo
24
+ (2 3 42)
25
+ (bar (40 41 42) (43 (x y z) 41))
26
+ 42
27
+ 99))
28
+ ((2 3 42)
29
+ (bar (40 41 42) (43 (x y z) 41))
30
+ (40 41 42)
31
+ (43 (x y z) 41)
32
+ (x y z)))
33
+
34
+ ("it finds code within interpolated strings"
35
+ (list/seek-cars 'include (parse-in-string "
36
+ <div>~~(include \"empty-name.html\")</div>
37
+ <div>~~(map λn(let name n (include \"show-name.html\")) {names})'</div>
38
+ "))
39
+ ((include "empty-name.html")
40
+ (include "show-name.html"))))
@@ -1,3 +1,17 @@
1
+ (examples-for list-single-element
2
+ ("returns nothing from an empty list"
3
+ (list-single-element nil)
4
+ nil)
5
+
6
+ ("returns first item from a list with one item"
7
+ (list-single-element '(x))
8
+ x)
9
+
10
+ ("returns nothing from a list with more than one item"
11
+ (list-single-element '(x y z))
12
+ nil))
13
+
14
+
1
15
  (examples-for iso
2
16
  ("(nil) is not the same as nil"
3
17
  (iso '(nil) nil)
@@ -37,7 +51,11 @@
37
51
  (g f (c d e) b a)))
38
52
 
39
53
  (examples-for flatten
40
- ("'flatten returns a flat list of things"
54
+ ("removes empty lists and nil"
55
+ (flatten '(a b nil (c d () e nil) f ()))
56
+ (a b c d e f))
57
+
58
+ ("returns a flat list of things"
41
59
  (flatten '((poo (x) (* x x)) (1 2 3)))
42
60
  (poo x * x x 1 2 3)))
43
61
 
@@ -187,3 +205,42 @@
187
205
  ("finds value corresponding to given key"
188
206
  (alref 'c '((a b) (c d)))
189
207
  d))
208
+
209
+ (examples-for list/fill
210
+ ("returns an overfull list unchanged"
211
+ (list/fill '(a b c) λ(just 'x) 2)
212
+ (a b c))
213
+
214
+ ("returns a full list unchanged"
215
+ (list/fill '(a b c) λ(just 'x) 3)
216
+ (a b c))
217
+
218
+ ("returns an unfull list with extra items"
219
+ (list/fill '(a b c) λ(just 'x) 4)
220
+ (a b c x))
221
+
222
+ ("returns an unfull list with extra items"
223
+ (let idx 0
224
+ (list/fill '(a b c) λ(++ idx) 10))
225
+ (a b c 1 2 3 4 5 6 7)))
226
+
227
+ (examples-for list/last
228
+ ("returns the thing if it's nil"
229
+ (list/last nil)
230
+ nil)
231
+
232
+ ("returns the thing if it's not a pair"
233
+ (list/last 42)
234
+ 42)
235
+
236
+ ("returns the cdr if it's not a pair"
237
+ (list/last '(x . y))
238
+ y)
239
+
240
+ ("returns the last item in the list"
241
+ (list/last '(x y z))
242
+ z)
243
+
244
+ ("returns nil if it's the last item in the list"
245
+ (list/last '(x y z nil))
246
+ nil))
@@ -0,0 +1,11 @@
1
+ (examples-for map-hash
2
+ ("iterates over key-value pairs"
3
+ (map-hash (fn (k v i) "pos.~i : ~k is for ~v")
4
+ { a 42 b 729 c 1024 })
5
+ ("pos.0 : a is for 42" "pos.1 : b is for 729" "pos.2 : c is for 1024"))
6
+
7
+ ("sorts keys before iterating"
8
+ (map-hash (fn (k v i) "pos.~i : ~k is for ~v")
9
+ { "aa" 1 "bbb" 2 "c" 3 }
10
+ (fn (keys) (sort-by len keys)))
11
+ ("pos.0 : c is for 3" "pos.1 : aa is for 1" "pos.2 : bbb is for 2" )))
@@ -0,0 +1,10 @@
1
+ (examples-for module
2
+ ("it overrides external namespace internally"
3
+ (with (x (fn (n) "the old outside x ~n")
4
+ y (fn (n) "the old outside y ~n"))
5
+ (module foo
6
+ (def x (n) (* 2 n))
7
+ (def y (n) (+ 2 n))
8
+ (export bar (a b) (* (x a) (y b))))
9
+ (list (foo/bar 3 5) (x 43) (y 44)))
10
+ (42 "the old outside x 43" "the old outside y 44")))
@@ -0,0 +1,6 @@
1
+ (examples-for ampersand-syntax
2
+ ("assigns multiple values at once ; value of expr is last assigned value"
3
+ (with (a 1 b { foo 2 } c { d { e 3 } })
4
+ (let res (= a 10 b.foo 20 (&d.e c) 30)
5
+ (list a b.foo c.d.e res)))
6
+ (10 20 30 30)))
@@ -9,9 +9,14 @@
9
9
  ("parses an empty symbol" (parse "||") (||) )
10
10
  ("parses a colon-syntax symbol" (parse "this:that") ((colon-syntax this that)) )
11
11
  ("parses a percent-syntax symbol" (parse "this%that") ((percent-syntax this that)) )
12
+ ("parses combined percent-colon" (parse "%this:%that") ((colon-syntax (percent-syntax || this) (percent-syntax || that))) )
12
13
  ("parses a prefix dollar" (parse "$this") ((dollar-syntax || this)))
13
14
  ("parses a prefix dollar-dot" (parse "$.this") ((dot-syntax (dollar-syntax || ||) this)))
14
15
 
16
+ ("parses combined percent-colon-dot"
17
+ (parse "%this.foo:%that.bar")
18
+ ((colon-syntax (percent-syntax || (dot-syntax this foo)) (percent-syntax || (dot-syntax that bar)))) )
19
+
15
20
  ("parse custom prefix-lists with a special-syntax prefix"
16
21
  (parse "x.y(1 2 3)")
17
22
  ((prefix-list "x.y" (1 2 3))))
@@ -24,6 +29,22 @@
24
29
  (parse "%this.and.that")
25
30
  ((percent-syntax || (dot-syntax this and that))) )
26
31
 
32
+ ("parses a plain at-symbol"
33
+ (pre-compile (car (parse "@")))
34
+ @)
35
+
36
+ ("parses a plain at-prefix-syntax symbol"
37
+ (parse "@this")
38
+ ((at-syntax || this)))
39
+
40
+ ("parses an at-prefix-mixed-with-dot-syntax symbol"
41
+ (parse "@this.and.that")
42
+ ((dot-syntax (at-syntax || this) and that)))
43
+
44
+ ("at-prefix-mixed-with-dot-syntax expands to plain hash lookup: @this.and.that equivalent to @.this.and.that"
45
+ (pre-compile (car (parse "@this.and.that")))
46
+ (hash-get (hash-get (hash-get @ (quote this)) (quote and)) (quote that)))
47
+
27
48
  ("parses a dot-syntax symbol"
28
49
  (parse "this.that.zozo")
29
50
  ((dot-syntax this that zozo)) )
@@ -49,6 +70,10 @@
49
70
  (parse-in-string (joinstr "" (list "hello, " '~ "(world), take me to your " '~ "dealer please")))
50
71
  (string-pieces "hello, " (world) ", take me to your " dealer " please"))
51
72
 
73
+ ("parses a plain string whose entire content is a single interpolation"
74
+ (parse-in-string (joinstr "" (list '~ "(hello world)")))
75
+ (hello world))
76
+
52
77
  ("parses a plain string of html text with interpolations"
53
78
  (parse-in-string "<div id='content_item_~~{id}'><label>~~{data-name-1}</label> ~~{data-text-1}</div>")
54
79
  (string-pieces "<div id='content_item_" (brace-list id) "'><label>" (brace-list data-name-1) "</label> " (brace-list data-text-1) "</div>"))
@@ -206,13 +206,12 @@ toto")
206
206
  (pp '(def yoohoo (it) (wrangle "foobar" it)))
207
207
  "(def yoohoo (it) (wrangle \"foobar\" it))")
208
208
 
209
- ("a 'let form"
210
- (pp '(let acc nil
211
- (rfnwith flattenize (x things)
212
- (if (pair? x)
213
- (eachr flattenize x)
214
- (push x acc)))
215
- acc))
209
+ ("something with a nil and t literal"
210
+ (pp '(def yoohoo (it) (wrangle nil t it)))
211
+ "(def yoohoo (it) (wrangle nil t it))")
212
+
213
+ ("a 'let form squished into one line"
214
+ (pp '(let acc nil (rfnwith flattenize (x things) (if (pair? x) (eachr flattenize x) (push x acc))) acc))
216
215
  "(let acc nil
217
216
  (rfnwith flattenize (x things)
218
217
  (if (pair? x)
@@ -221,13 +220,11 @@ toto")
221
220
  acc)")
222
221
 
223
222
 
224
- ("a real-life example from utils"
223
+ ("a real-life example from utils with improper 'if formatting"
225
224
  (pp '(def flatten (things)
226
225
  (let acc nil
227
226
  (rfnwith flattenize (x things)
228
- (if (pair? x)
229
- (eachr flattenize x)
230
- (push x acc)))
227
+ (if (pair? x) (eachr flattenize x) (push x acc)))
231
228
  acc)))
232
229
  "(def flatten (things)
233
230
  (let acc nil
@@ -282,12 +279,18 @@ toto")
282
279
 
283
280
  ("special syntax"
284
281
  (pp '(string-pieces "hello " (bang-syntax || (dot-syntax x y (ampersand-syntax foo bar))) " and welcome to " (prefix-list "%%" (a b c d)) " and friends!"))
285
- "\"hello ~~!x.y.foo&bar and welcome to ~~%%(a b c d) and friends!\"")
282
+ "\"hello ~~|!x.y.foo&bar| and welcome to ~~%%(a b c d) and friends!\"")
286
283
 
287
284
  ("percent-syntax"
288
285
  (pp '(percent-syntax || (dot-syntax x y)))
289
286
  "%x.y")
290
287
 
291
288
  ("brace list"
292
- (pp '(&x {a 1 b "two" c 'three d ,four e (sub invocation) f {sub brace list} }))
293
- "(&x { a 1 b \"two\" c 'three d ,four e (sub invocation) f { sub brace list } })"))
289
+ (pp '(let hello {a 1 b "two" c 'three d ,four e (sub invocation) f {sub brace list here} }))
290
+ "(let hello { a 1
291
+ b \"two\"
292
+ c 'three
293
+ d ,four
294
+ e (sub invocation)
295
+ f { sub brace
296
+ list here } })"))
@@ -0,0 +1,8 @@
1
+ (examples-for -
2
+ ("returns elements of first that are not in second"
3
+ (- '(a b c e) '(a b d))
4
+ (c e))
5
+
6
+ ("returns all of first if second is nil"
7
+ (- '(a b c) nil)
8
+ (a b c)))
@@ -1,3 +1,6 @@
1
+ (def-setting "testing.reset.string" "hello world")
2
+ (def-setting "testing.reset.fn" to-string)
3
+
1
4
  (examples-for settings/fn
2
5
  ("returns arg if it is a sym"
3
6
  (settings/fn 'foobar)
@@ -17,8 +20,21 @@
17
20
 
18
21
  ("wraps arg in a fn expression if it is a hash"
19
22
  (settings/fn '{ a 1 b 2 })
20
- (fn (_) { a 1 b 2 }))
23
+ (k { a 1 b 2 }))
21
24
 
22
25
  ("wraps arg in a fn expression if it is a complex expression"
23
26
  (settings/fn '(let foo this that (rfnwith (complex stuff) (%td.tricky#syntax))))
24
27
  (fn (_) (let foo this that (rfnwith (complex stuff) ((percent-syntax || (dot-syntax td tricky#syntax))))))))
28
+
29
+ (examples-for reset-setting
30
+ ("restores a setting to its original value"
31
+ (do (set-setting "testing.reset.string" "goodbye, dear life")
32
+ (reset-setting "testing.reset.string")
33
+ (setting "testing.reset.string"))
34
+ "hello world")
35
+
36
+ ("restores a setting to its original function value"
37
+ (do (set-setting "testing.reset.fn" "goodbye, dear life")
38
+ (reset-setting "testing.reset.fn")
39
+ (setting "testing.reset.fn"))
40
+ "testing.reset.fn"))
@@ -1,3 +1,51 @@
1
+ (examples-for string-eval-fn
2
+ ("returns a function to eval a user-supplied string"
3
+ (let s "hello \~|u|, \~x + \~y is \~(+ x y), thank you!"
4
+ (let f (string-eval-fn s '(u x y))
5
+ (f "world" 37 5)))
6
+ "hello world, 37 + 5 is 42, thank you!"))
7
+
8
+ (examples-for string/eval-with-args
9
+ ("evals a user-supplied string"
10
+ (let s "hello \~|u|, \~x + \~y is \~(+ x y), thank heavens!"
11
+ (string/eval-with-args s '(u x y) "world" 36 6))
12
+ "hello world, 36 + 6 is 42, thank heavens!")
13
+
14
+ ("reports errors"
15
+ (on-err errors
16
+ (let s "hello \~|u|, \~x + \~y is \~(+ x y), thank heavens!"
17
+ (string/eval-with-args s '(a b c) "world" 36 6)))
18
+ ("error evaluating \"hello \~|u|, \~x + \~y is \~(+ x y), thank heavens!\"
19
+ with arg names (a b c)
20
+ and args (\"world\" 36 6)"
21
+ "unbound symbol: y")))
22
+
23
+ (examples-for string/pad-left
24
+ ("does not change a string whose length is greater than the given length"
25
+ (string/pad-left "Toronto" 3 "X")
26
+ "Toronto")
27
+
28
+ ("adds left padding to a string such that the result is the given length"
29
+ (string/pad-left "toto" 8 "X")
30
+ "XXXXtoto")
31
+
32
+ ("accepts multi-character padding"
33
+ (string/pad-left "toto" 12 "XYZ")
34
+ "XYZXYZXYtoto"))
35
+
36
+ (examples-for string/pad-right
37
+ ("does not change a string whose length is greater than the given length"
38
+ (string/pad-right "Toronto" 3 "X")
39
+ "Toronto")
40
+
41
+ ("adds right padding to a string such that the result is the given length"
42
+ (string/pad-right "toto" 8 "X")
43
+ "totoXXXX")
44
+
45
+ ("accepts multi-character padding"
46
+ (string/pad-right "toto" 12 "XYZ")
47
+ "totoXYZXYZXY"))
48
+
1
49
  (examples-for string-split
2
50
  ("splits a string using given expression"
3
51
  (string-split "a and b and c and d" " and ")
@@ -7,6 +55,10 @@
7
55
  (collect !empty? (string-split "word"))
8
56
  ("w" "o" "r" "d"))
9
57
 
58
+ ("splits on regexp"
59
+ (string-split "hello, darkness ; my old friend\nI've come : to talk - with you again..." (regexp "\[\\n;:\\-,\]"))
60
+ ("hello" " darkness " " my old friend" "I've come " " to talk " " with you again..."))
61
+
10
62
  ("returns empty leading, internal, and trailing segments"
11
63
  (string-split "and" "and")
12
64
  ("" ""))
@@ -20,8 +72,22 @@
20
72
  (string-replace "and" "or" "a and b and c and d")
21
73
  "a or b or c or d")
22
74
 
75
+ ("replaces with hash lookup"
76
+ (string-replace
77
+ "['<>&\"]"
78
+ { "'" "&#39;"
79
+ "<" "&lt;"
80
+ ">" "&gt;"
81
+ "\"" "&quot;" }
82
+ "it's >such< a \"lovely\" day")
83
+ "it&#39;s &gt;such&lt; a &quot;lovely&quot; day")
84
+
23
85
  ("replace with regexp"
24
86
  (string-replace "and|or" "x" "a and b or c and d")
87
+ "a x b x c x d")
88
+
89
+ ("replace with literal regexp"
90
+ (string-replace (regexp "and|or") "x" "a and b or c and d")
25
91
  "a x b x c x d"))
26
92
 
27
93
  (examples-for string-truncate
@@ -90,4 +156,7 @@
90
156
  (examples-for string-strip
91
157
  ("removes leading whitespace" (string-strip " hello!") "hello!" )
92
158
  ("removes trailing whitespace" (string-strip "(world) ") "(world)" )
93
- ("removes leading and trailing whitespace" (string-strip "\n\nme\n\n") "me" ))
159
+ ("removes leading and trailing whitespace" (string-strip "\n\nme\n\n") "me" )
160
+ ("ignores leading and trailing whitespace for intenal lines"
161
+ (string-strip "\n\n me \n\n you \n\n\t them \t\n\n")
162
+ "me \n\n you \n\n\t them" ))