2015-04-18 14:50:51 +00:00
|
|
|
# Some useful helpers for dealing with strings.
|
2015-04-17 18:22:59 +00:00
|
|
|
|
2015-04-03 19:53:33 +00:00
|
|
|
recipe string-equal [
|
2015-04-19 07:21:24 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30:literal
|
2015-04-03 19:53:33 +00:00
|
|
|
a:address:array:character <- next-ingredient
|
|
|
|
a-len:integer <- length a:address:array:character/deref
|
|
|
|
b:address:array:character <- next-ingredient
|
|
|
|
b-len:integer <- length b:address:array:character/deref
|
|
|
|
# compare lengths
|
|
|
|
{
|
2015-04-12 05:24:33 +00:00
|
|
|
trace [string-equal], [comparing lengths]
|
2015-04-03 19:53:33 +00:00
|
|
|
length-equal?:boolean <- equal a-len:integer, b-len:integer
|
|
|
|
break-if length-equal?:boolean
|
|
|
|
reply 0:literal
|
|
|
|
}
|
|
|
|
# compare each corresponding character
|
2015-04-12 05:24:33 +00:00
|
|
|
trace [string-equal], [comparing characters]
|
2015-04-03 19:53:33 +00:00
|
|
|
i:integer <- copy 0:literal
|
|
|
|
{
|
|
|
|
done?:boolean <- greater-or-equal i:integer, a-len:integer
|
|
|
|
break-if done?:boolean
|
|
|
|
a2:character <- index a:address:array:character/deref, i:integer
|
|
|
|
b2:character <- index b:address:array:character/deref, i:integer
|
|
|
|
{
|
|
|
|
chars-match?:boolean <- equal a2:character, b2:character
|
|
|
|
break-if chars-match?:boolean
|
|
|
|
reply 0:literal
|
|
|
|
}
|
|
|
|
i:integer <- add i:integer, 1:literal
|
|
|
|
loop
|
|
|
|
}
|
|
|
|
reply 1:literal
|
|
|
|
]
|
2015-04-06 19:02:38 +00:00
|
|
|
|
|
|
|
scenario string-equal-reflexive [
|
|
|
|
run [
|
2015-04-19 07:21:24 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30:literal
|
2015-04-06 19:02:38 +00:00
|
|
|
x:address:array:character <- new [abc]
|
|
|
|
3:boolean/raw <- string-equal x:address:array:character, x:address:array:character
|
|
|
|
]
|
|
|
|
memory should contain [
|
|
|
|
3 <- 1 # x == x for all x
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-04-08 07:47:04 +00:00
|
|
|
scenario string-equal-identical [
|
2015-04-06 19:02:38 +00:00
|
|
|
run [
|
2015-04-19 07:21:24 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30:literal
|
2015-04-06 19:02:38 +00:00
|
|
|
x:address:array:character <- new [abc]
|
|
|
|
y:address:array:character <- new [abc]
|
|
|
|
3:boolean/raw <- string-equal x:address:array:character, y:address:array:character
|
|
|
|
]
|
|
|
|
memory should contain [
|
2015-04-08 07:47:04 +00:00
|
|
|
3 <- 1 # abc == abc
|
2015-04-06 19:02:38 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-04-08 07:47:04 +00:00
|
|
|
scenario string-equal-distinct-lengths [
|
2015-04-06 19:02:38 +00:00
|
|
|
run [
|
2015-04-19 07:21:24 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30:literal
|
2015-04-06 19:02:38 +00:00
|
|
|
x:address:array:character <- new [abc]
|
|
|
|
y:address:array:character <- new [abcd]
|
|
|
|
3:boolean/raw <- string-equal x:address:array:character, y:address:array:character
|
|
|
|
]
|
2015-04-08 07:47:04 +00:00
|
|
|
memory should contain [
|
|
|
|
3 <- 0 # abc != abcd
|
|
|
|
]
|
2015-04-13 17:09:46 +00:00
|
|
|
trace should contain [
|
|
|
|
string-equal: comparing lengths
|
|
|
|
]
|
|
|
|
trace should not contain [
|
|
|
|
string-equal: comparing characters
|
|
|
|
]
|
2015-04-08 07:47:04 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
scenario string-equal-with-empty [
|
|
|
|
run [
|
2015-04-19 07:21:24 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30:literal
|
2015-04-08 07:47:04 +00:00
|
|
|
x:address:array:character <- new []
|
|
|
|
y:address:array:character <- new [abcd]
|
|
|
|
3:boolean/raw <- string-equal x:address:array:character, y:address:array:character
|
|
|
|
]
|
|
|
|
memory should contain [
|
|
|
|
3 <- 0 # "" != abcd
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario string-equal-common-lengths-but-distinct [
|
|
|
|
run [
|
2015-04-19 07:21:24 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30:literal
|
2015-04-08 07:47:04 +00:00
|
|
|
x:address:array:character <- new [abc]
|
|
|
|
y:address:array:character <- new [abd]
|
|
|
|
3:boolean/raw <- string-equal x:address:array:character, y:address:array:character
|
|
|
|
]
|
2015-04-06 19:02:38 +00:00
|
|
|
memory should contain [
|
|
|
|
3 <- 0 # abc != abd
|
|
|
|
]
|
|
|
|
]
|
2015-04-18 03:36:25 +00:00
|
|
|
|
|
|
|
# A new type to help incrementally construct strings.
|
|
|
|
container buffer [
|
|
|
|
length:integer
|
|
|
|
data:address:array:character
|
|
|
|
]
|
|
|
|
|
|
|
|
recipe init-buffer [
|
2015-04-19 07:21:24 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30:literal
|
|
|
|
#? $print default-space:address:array:location
|
2015-04-18 04:51:13 +00:00
|
|
|
#? $print [
|
|
|
|
#? ]
|
2015-04-18 03:36:25 +00:00
|
|
|
result:address:buffer <- new buffer:type
|
|
|
|
len:address:integer <- get-address result:address:buffer/deref, length:offset
|
|
|
|
len:address:integer/deref <- copy 0:literal
|
|
|
|
s:address:address:array:character <- get-address result:address:buffer/deref, data:offset
|
|
|
|
capacity:integer <- next-ingredient
|
|
|
|
s:address:address:array:character/deref <- new character:type, capacity:integer
|
2015-04-18 04:51:13 +00:00
|
|
|
#? $print s:address:address:array:character/deref
|
|
|
|
#? $print [
|
|
|
|
#? ]
|
2015-04-18 03:36:25 +00:00
|
|
|
reply result:address:buffer
|
|
|
|
]
|
2015-04-18 04:51:13 +00:00
|
|
|
|
|
|
|
recipe grow-buffer [
|
2015-04-19 07:21:24 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30:literal
|
2015-04-18 04:51:13 +00:00
|
|
|
in:address:buffer <- next-ingredient
|
|
|
|
# double buffer size
|
|
|
|
x:address:address:array:character <- get-address in:address:buffer/deref, data:offset
|
|
|
|
oldlen:integer <- length x:address:address:array:character/deref/deref
|
|
|
|
newlen:integer <- multiply oldlen:integer, 2:literal
|
|
|
|
olddata:address:array:character <- copy x:address:address:array:character/deref
|
|
|
|
x:address:address:array:character/deref <- new character:type, newlen:integer
|
|
|
|
# copy old contents
|
|
|
|
i:integer <- copy 0:literal
|
|
|
|
{
|
|
|
|
done?:boolean <- greater-or-equal i:integer, oldlen:integer
|
|
|
|
break-if done?:boolean
|
|
|
|
src:character <- index olddata:address:array:character/deref, i:integer
|
|
|
|
dest:address:character <- index-address x:address:address:array:character/deref/deref, i:integer
|
|
|
|
dest:address:character/deref <- copy src:character
|
|
|
|
i:integer <- add i:integer, 1:literal
|
|
|
|
loop
|
|
|
|
}
|
|
|
|
reply in:address:buffer
|
|
|
|
]
|
|
|
|
|
|
|
|
recipe buffer-full? [
|
2015-04-19 07:21:24 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30:literal
|
2015-04-18 04:51:13 +00:00
|
|
|
in:address:buffer <- next-ingredient
|
|
|
|
len:integer <- get in:address:buffer/deref, length:offset
|
|
|
|
s:address:array:character <- get in:address:buffer/deref, data:offset
|
|
|
|
capacity:integer <- length s:address:array:character/deref
|
|
|
|
result:boolean <- greater-or-equal len:integer, capacity:integer
|
|
|
|
reply result:boolean
|
|
|
|
]
|
|
|
|
|
|
|
|
# in:address:buffer <- buffer-append in:address:buffer, c:character
|
|
|
|
recipe buffer-append [
|
2015-04-19 07:21:24 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30:literal
|
2015-04-18 04:51:13 +00:00
|
|
|
in:address:buffer <- next-ingredient
|
|
|
|
c:character <- next-ingredient
|
|
|
|
{
|
|
|
|
# grow buffer if necessary
|
|
|
|
full?:boolean <- buffer-full? in:address:buffer
|
|
|
|
break-unless full?:boolean
|
|
|
|
in:address:buffer <- grow-buffer in:address:buffer
|
|
|
|
}
|
|
|
|
len:address:integer <- get-address in:address:buffer/deref, length:offset
|
|
|
|
s:address:array:character <- get in:address:buffer/deref, data:offset
|
|
|
|
dest:address:character <- index-address s:address:array:character/deref, len:address:integer/deref
|
|
|
|
dest:address:character/deref <- copy c:character
|
|
|
|
len:address:integer/deref <- add len:address:integer/deref, 1:literal
|
|
|
|
reply in:address:buffer/same-as-arg:0
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario buffer-append-works [
|
|
|
|
run [
|
2015-04-19 07:21:24 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30:literal
|
2015-04-18 04:51:13 +00:00
|
|
|
x:address:buffer <- init-buffer 3:literal
|
|
|
|
s1:address:array:character <- get x:address:buffer/deref, data:offset
|
|
|
|
x:address:buffer <- buffer-append x:address:buffer, 97:literal # 'a'
|
|
|
|
x:address:buffer <- buffer-append x:address:buffer, 98:literal # 'b'
|
|
|
|
x:address:buffer <- buffer-append x:address:buffer, 99:literal # 'c'
|
|
|
|
s2:address:array:character <- get x:address:buffer/deref, data:offset
|
|
|
|
1:boolean/raw <- equal s1:address:array:character, s2:address:array:character
|
|
|
|
#? $print s2:address:array:character
|
|
|
|
#? $print [
|
|
|
|
#? ]
|
|
|
|
#? $print 1060:integer/raw
|
|
|
|
#? $print [
|
|
|
|
#? ]
|
|
|
|
#? $print 1061:integer/raw
|
|
|
|
#? $print [
|
|
|
|
#? ]
|
|
|
|
#? $print 1062:integer/raw
|
|
|
|
#? $print [
|
|
|
|
#? ]
|
|
|
|
#? $print 1063:integer/raw
|
|
|
|
#? $print [
|
|
|
|
#? ]
|
|
|
|
#? $print 1064:integer/raw
|
|
|
|
#? $print [
|
|
|
|
#? ]
|
|
|
|
#? $print 1065:integer/raw
|
|
|
|
#? $print [
|
|
|
|
#? ]
|
|
|
|
2:array:character/raw <- copy s2:address:array:character/deref
|
2015-04-18 15:34:48 +00:00
|
|
|
+buffer-filled
|
|
|
|
x:address:buffer <- buffer-append x:address:buffer, 100:literal # 'd'
|
|
|
|
s3:address:array:character <- get x:address:buffer/deref, data:offset
|
|
|
|
10:boolean/raw <- equal s1:address:array:character, s3:address:array:character
|
|
|
|
11:integer/raw <- get x:address:buffer/deref, length:offset
|
|
|
|
12:array:character/raw <- copy s3:address:array:character/deref
|
2015-04-18 04:51:13 +00:00
|
|
|
]
|
|
|
|
memory should contain [
|
2015-04-18 15:34:48 +00:00
|
|
|
# before +buffer-filled
|
2015-04-18 04:51:13 +00:00
|
|
|
1 <- 1 # no change in data pointer
|
|
|
|
2 <- 3 # size of data
|
|
|
|
3 <- 97 # data
|
|
|
|
4 <- 98
|
|
|
|
5 <- 99
|
2015-04-18 15:34:48 +00:00
|
|
|
# in the end
|
|
|
|
10 <- 0 # data pointer has grown
|
|
|
|
11 <- 4 # final length
|
|
|
|
12 <- 6 # but data's capacity has doubled
|
|
|
|
13 <- 97 # data
|
|
|
|
14 <- 98
|
|
|
|
15 <- 99
|
|
|
|
16 <- 100
|
|
|
|
17 <- 0
|
|
|
|
18 <- 0
|
2015-04-18 04:51:13 +00:00
|
|
|
]
|
|
|
|
]
|
2015-04-19 03:11:59 +00:00
|
|
|
|
|
|
|
# result:address:array:character <- integer-to-decimal-string n:integer
|
|
|
|
recipe integer-to-decimal-string [
|
2015-04-19 07:21:24 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30:literal
|
2015-04-19 03:11:59 +00:00
|
|
|
n:integer <- next-ingredient
|
|
|
|
# is it zero?
|
|
|
|
{
|
|
|
|
break-if n:integer
|
|
|
|
result:address:array:character <- new [0]
|
|
|
|
reply result:address:array:character
|
|
|
|
}
|
|
|
|
# save sign
|
|
|
|
negate-result:boolean <- copy 0:literal
|
|
|
|
{
|
|
|
|
negative?:boolean <- less-than n:integer, 0:literal
|
|
|
|
break-unless negative?:boolean
|
|
|
|
negate-result:boolean <- copy 1:literal
|
|
|
|
n:integer <- multiply n:integer -1:literal
|
|
|
|
}
|
|
|
|
# add digits from right to left into intermediate buffer
|
|
|
|
tmp:address:buffer <- init-buffer 30:literal
|
|
|
|
digit-base:integer <- copy 48:literal # '0'
|
|
|
|
{
|
|
|
|
done?:boolean <- equal n:integer, 0:literal
|
|
|
|
break-if done?:boolean
|
|
|
|
n:integer, digit:integer <- divide-with-remainder n:integer, 10:literal
|
|
|
|
c:character <- add digit-base:integer, digit:integer
|
|
|
|
tmp:address:buffer <- buffer-append tmp:address:buffer, c:character
|
|
|
|
loop
|
|
|
|
}
|
|
|
|
# add sign
|
|
|
|
{
|
|
|
|
break-unless negate-result:boolean
|
|
|
|
tmp:address:buffer <- buffer-append tmp:address:buffer, 45:literal # '-'
|
|
|
|
}
|
|
|
|
# reverse buffer into string result
|
|
|
|
len:integer <- get tmp:address:buffer/deref, length:offset
|
|
|
|
buf:address:array:character <- get tmp:address:buffer/deref data:offset
|
|
|
|
result:address:array:character <- new character:type, len:integer
|
|
|
|
i:integer <- subtract len:integer, 1:literal
|
|
|
|
j:integer <- copy 0:literal
|
|
|
|
{
|
|
|
|
# while i >= 0
|
|
|
|
done?:boolean <- less-than i:integer, 0:literal
|
|
|
|
break-if done?:boolean
|
|
|
|
# result[j] = tmp[i]
|
|
|
|
src:character <- index buf:address:array:character/deref, i:integer
|
|
|
|
dest:address:character <- index-address result:address:array:character/deref, j:integer
|
|
|
|
dest:address:character/deref <- copy src:character
|
|
|
|
# ++i
|
|
|
|
i:integer <- subtract i:integer, 1:literal
|
|
|
|
# --j
|
|
|
|
j:integer <- add j:integer, 1:literal
|
|
|
|
loop
|
|
|
|
}
|
|
|
|
reply result:address:array:character
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario integer-to-decimal-digit-zero [
|
|
|
|
run [
|
|
|
|
1:address:array:character/raw <- integer-to-decimal-string 0:literal
|
|
|
|
2:array:character/raw <- copy 1:address:array:character/deref/raw
|
|
|
|
]
|
|
|
|
memory should contain [
|
|
|
|
2 <- 1
|
|
|
|
3 <- 48 # '0'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario integer-to-decimal-digit-positive [
|
|
|
|
run [
|
|
|
|
1:address:array:character/raw <- integer-to-decimal-string 234:literal
|
|
|
|
2:array:character/raw <- copy 1:address:array:character/deref/raw
|
|
|
|
]
|
|
|
|
memory should contain [
|
|
|
|
2 <- 3
|
|
|
|
3 <- 50 # '2'
|
|
|
|
4 <- 51 # '3'
|
|
|
|
5 <- 52 # '4'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario integer-to-decimal-digit-negative [
|
|
|
|
run [
|
|
|
|
1:address:array:character/raw <- integer-to-decimal-string -1:literal
|
|
|
|
2:array:character/raw <- copy 1:address:array:character/deref/raw
|
|
|
|
]
|
|
|
|
memory should contain [
|
|
|
|
2 <- 2
|
|
|
|
3 <- 45 # '-'
|
|
|
|
4 <- 49 # '1'
|
|
|
|
]
|
|
|
|
]
|
2015-04-19 07:13:08 +00:00
|
|
|
|
|
|
|
recipe string-append [
|
2015-04-19 07:21:24 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30:literal
|
2015-04-19 07:13:08 +00:00
|
|
|
# result = new string[a.length + b.length]
|
|
|
|
a:address:array:character <- next-ingredient
|
|
|
|
a-len:integer <- length a:address:array:character/deref
|
|
|
|
b:address:array:character <- next-ingredient
|
|
|
|
b-len:integer <- length b:address:array:character/deref
|
|
|
|
result-len:integer <- add a-len:integer, b-len:integer
|
|
|
|
result:address:array:character <- new character:type, result-len:integer
|
|
|
|
# copy a into result
|
|
|
|
result-idx:integer <- copy 0:literal
|
|
|
|
i:integer <- copy 0:literal
|
|
|
|
{
|
|
|
|
# while i < a.length
|
|
|
|
a-done?:boolean <- greater-or-equal i:integer, a-len:integer
|
|
|
|
break-if a-done?:boolean
|
|
|
|
# result[result-idx] = a[i]
|
|
|
|
out:address:character <- index-address result:address:array:character/deref, result-idx:integer
|
|
|
|
in:character <- index a:address:array:character/deref, i:integer
|
|
|
|
out:address:character/deref <- copy in:character
|
|
|
|
# ++i
|
|
|
|
i:integer <- add i:integer, 1:literal
|
|
|
|
# ++result-idx
|
|
|
|
result-idx:integer <- add result-idx:integer, 1:literal
|
|
|
|
loop
|
|
|
|
}
|
|
|
|
# copy b into result
|
|
|
|
i:integer <- copy 0:literal
|
|
|
|
{
|
|
|
|
# while i < b.length
|
|
|
|
b-done?:boolean <- greater-or-equal i:integer, b-len:integer
|
|
|
|
break-if b-done?:boolean
|
|
|
|
# result[result-idx] = a[i]
|
|
|
|
out:address:character <- index-address result:address:array:character/deref, result-idx:integer
|
|
|
|
in:character <- index b:address:array:character/deref, i:integer
|
|
|
|
out:address:character/deref <- copy in:character
|
|
|
|
# ++i
|
|
|
|
i:integer <- add i:integer, 1:literal
|
|
|
|
# ++result-idx
|
|
|
|
result-idx:integer <- add result-idx:integer, 1:literal
|
|
|
|
loop
|
|
|
|
}
|
|
|
|
reply result:address:array:character
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario string-append-1 [
|
|
|
|
run [
|
|
|
|
1:address:array:character/raw <- new [hello,]
|
|
|
|
2:address:array:character/raw <- new [ world!]
|
|
|
|
3:address:array:character/raw <- string-append 1:address:array:character/raw, 2:address:array:character/raw
|
|
|
|
4:array:character/raw <- copy 3:address:array:character/raw/deref
|
|
|
|
]
|
|
|
|
memory should contain [
|
|
|
|
4 <- 13
|
|
|
|
5 <- 104 # 'h'
|
|
|
|
6 <- 101 # 'e'
|
|
|
|
7 <- 108 # 'l'
|
|
|
|
8 <- 108 # 'l'
|
|
|
|
9 <- 111 # 'o'
|
|
|
|
10 <- 44 # ','
|
|
|
|
11 <- 32 # ' '
|
|
|
|
12 <- 119 # 'w'
|
|
|
|
13 <- 111 # 'o'
|
|
|
|
14 <- 114 # 'r'
|
|
|
|
15 <- 108 # 'l'
|
|
|
|
16 <- 100 # 'd'
|
|
|
|
17 <- 33 # '!'
|
|
|
|
]
|
|
|
|
]
|