2015-11-21 18:01:12 +00:00
|
|
|
# Some useful helpers for dealing with text (arrays of characters)
|
2015-04-17 18:22:59 +00:00
|
|
|
|
2015-11-29 09:00:30 +00:00
|
|
|
# to-text-line gets called implicitly in various places
|
|
|
|
# define it to be identical to 'to-text' by default
|
2016-04-24 18:54:30 +00:00
|
|
|
def to-text-line x:_elem -> y:address:array:character [
|
2015-11-29 09:00:30 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
y <- to-text x
|
|
|
|
]
|
|
|
|
|
2016-03-21 09:53:34 +00:00
|
|
|
# variant for arrays (since we can't pass them around otherwise)
|
2016-04-24 18:54:30 +00:00
|
|
|
def array-to-text-line x:address:array:_elem -> y:address:array:character [
|
2016-03-21 09:53:34 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
y <- to-text *x
|
|
|
|
]
|
|
|
|
|
2015-11-29 09:00:30 +00:00
|
|
|
# to-text on text is just the identity function
|
2016-04-24 18:54:30 +00:00
|
|
|
def to-text x:address:array:character -> y:address:array:character [
|
2015-11-28 05:38:09 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
2016-03-08 09:30:14 +00:00
|
|
|
return x
|
2015-11-28 05:38:09 +00:00
|
|
|
]
|
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def equal a:address:array:character, b:address:array:character -> result:boolean [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-19 05:36:36 +00:00
|
|
|
load-ingredients
|
2015-07-29 21:37:57 +00:00
|
|
|
a-len:number <- length *a
|
|
|
|
b-len:number <- length *b
|
2015-04-03 19:53:33 +00:00
|
|
|
# compare lengths
|
|
|
|
{
|
2015-11-21 18:01:12 +00:00
|
|
|
trace 99, [text-equal], [comparing lengths]
|
2015-07-29 21:37:57 +00:00
|
|
|
length-equal?:boolean <- equal a-len, b-len
|
|
|
|
break-if length-equal?
|
2016-03-08 09:30:14 +00:00
|
|
|
return 0
|
2015-04-03 19:53:33 +00:00
|
|
|
}
|
|
|
|
# compare each corresponding character
|
2015-11-21 18:01:12 +00:00
|
|
|
trace 99, [text-equal], [comparing characters]
|
2015-07-28 21:33:22 +00:00
|
|
|
i:number <- copy 0
|
2015-04-03 19:53:33 +00:00
|
|
|
{
|
2015-07-29 21:37:57 +00:00
|
|
|
done?:boolean <- greater-or-equal i, a-len
|
|
|
|
break-if done?
|
|
|
|
a2:character <- index *a, i
|
|
|
|
b2:character <- index *b, i
|
2015-04-03 19:53:33 +00:00
|
|
|
{
|
2015-07-29 21:37:57 +00:00
|
|
|
chars-match?:boolean <- equal a2, b2
|
|
|
|
break-if chars-match?
|
2016-03-08 09:30:14 +00:00
|
|
|
return 0
|
2015-04-03 19:53:33 +00:00
|
|
|
}
|
2015-07-29 21:37:57 +00:00
|
|
|
i <- add i, 1
|
2015-04-03 19:53:33 +00:00
|
|
|
loop
|
|
|
|
}
|
2016-03-08 09:30:14 +00:00
|
|
|
return 1
|
2015-04-03 19:53:33 +00:00
|
|
|
]
|
2015-04-06 19:02:38 +00:00
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-equal-reflexive [
|
2015-04-06 19:02:38 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30
|
|
|
|
x:address:array:character <- new [abc]
|
2015-11-22 19:53:20 +00:00
|
|
|
3:boolean/raw <- equal x, x
|
2015-04-06 19:02:38 +00:00
|
|
|
]
|
2015-05-02 22:52:22 +00:00
|
|
|
memory-should-contain [
|
2015-04-06 19:02:38 +00:00
|
|
|
3 <- 1 # x == x for all x
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-equal-identical [
|
2015-04-06 19:02:38 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30
|
|
|
|
x:address:array:character <- new [abc]
|
|
|
|
y:address:array:character <- new [abc]
|
2015-11-22 19:53:20 +00:00
|
|
|
3:boolean/raw <- equal x, y
|
2015-04-06 19:02:38 +00:00
|
|
|
]
|
2015-05-02 22:52:22 +00:00
|
|
|
memory-should-contain [
|
2015-04-08 07:47:04 +00:00
|
|
|
3 <- 1 # abc == abc
|
2015-04-06 19:02:38 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-equal-distinct-lengths [
|
2015-04-06 19:02:38 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30
|
|
|
|
x:address:array:character <- new [abc]
|
|
|
|
y:address:array:character <- new [abcd]
|
2015-11-22 19:53:20 +00:00
|
|
|
3:boolean/raw <- equal x, y
|
2015-04-06 19:02:38 +00:00
|
|
|
]
|
2015-05-02 22:52:22 +00:00
|
|
|
memory-should-contain [
|
2015-04-08 07:47:04 +00:00
|
|
|
3 <- 0 # abc != abcd
|
|
|
|
]
|
2015-05-02 22:52:22 +00:00
|
|
|
trace-should-contain [
|
2015-11-21 18:01:12 +00:00
|
|
|
text-equal: comparing lengths
|
2015-04-13 17:09:46 +00:00
|
|
|
]
|
2015-05-02 22:52:22 +00:00
|
|
|
trace-should-not-contain [
|
2015-11-21 18:01:12 +00:00
|
|
|
text-equal: comparing characters
|
2015-04-13 17:09:46 +00:00
|
|
|
]
|
2015-04-08 07:47:04 +00:00
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-equal-with-empty [
|
2015-04-08 07:47:04 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30
|
|
|
|
x:address:array:character <- new []
|
|
|
|
y:address:array:character <- new [abcd]
|
2015-11-22 19:53:20 +00:00
|
|
|
3:boolean/raw <- equal x, y
|
2015-04-08 07:47:04 +00:00
|
|
|
]
|
2015-05-02 22:52:22 +00:00
|
|
|
memory-should-contain [
|
2015-04-08 07:47:04 +00:00
|
|
|
3 <- 0 # "" != abcd
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-equal-common-lengths-but-distinct [
|
2015-04-08 07:47:04 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
default-space:address:array:location <- new location:type, 30
|
|
|
|
x:address:array:character <- new [abc]
|
|
|
|
y:address:array:character <- new [abd]
|
2015-11-22 19:53:20 +00:00
|
|
|
3:boolean/raw <- equal x, y
|
2015-04-08 07:47:04 +00:00
|
|
|
]
|
2015-05-02 22:52:22 +00:00
|
|
|
memory-should-contain [
|
2015-04-06 19:02:38 +00:00
|
|
|
3 <- 0 # abc != abd
|
|
|
|
]
|
|
|
|
]
|
2015-04-18 03:36:25 +00:00
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
# A new type to help incrementally construct texts.
|
2015-04-18 03:36:25 +00:00
|
|
|
container buffer [
|
2015-05-13 17:03:26 +00:00
|
|
|
length:number
|
2016-04-24 18:54:30 +00:00
|
|
|
data:address:array:character
|
2015-04-18 03:36:25 +00:00
|
|
|
]
|
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def new-buffer capacity:number -> result:address:buffer [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-19 05:36:36 +00:00
|
|
|
load-ingredients
|
|
|
|
result <- new buffer:type
|
2016-04-11 03:47:44 +00:00
|
|
|
*result <- put *result, length:offset, 0
|
2016-04-24 18:54:30 +00:00
|
|
|
data:address:array:character <- new character:type, capacity
|
2016-04-11 03:47:44 +00:00
|
|
|
*result <- put *result, data:offset, data
|
2016-03-08 09:30:14 +00:00
|
|
|
return result
|
2015-04-18 03:36:25 +00:00
|
|
|
]
|
2015-04-18 04:51:13 +00:00
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def grow-buffer in:address:buffer -> in:address:buffer [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-19 05:36:36 +00:00
|
|
|
load-ingredients
|
2015-04-18 04:51:13 +00:00
|
|
|
# double buffer size
|
2016-04-24 18:54:30 +00:00
|
|
|
olddata:address:array:character <- get *in, data:offset
|
2016-04-11 03:47:44 +00:00
|
|
|
oldlen:number <- length *olddata
|
2015-07-29 21:37:57 +00:00
|
|
|
newlen:number <- multiply oldlen, 2
|
2016-04-24 18:54:30 +00:00
|
|
|
newdata:address:array:character <- new character:type, newlen
|
2016-04-11 03:47:44 +00:00
|
|
|
*in <- put *in, data:offset, newdata
|
2015-04-18 04:51:13 +00:00
|
|
|
# copy old contents
|
2015-07-28 21:33:22 +00:00
|
|
|
i:number <- copy 0
|
2015-04-18 04:51:13 +00:00
|
|
|
{
|
2015-07-29 21:37:57 +00:00
|
|
|
done?:boolean <- greater-or-equal i, oldlen
|
|
|
|
break-if done?
|
|
|
|
src:character <- index *olddata, i
|
2016-04-23 21:51:20 +00:00
|
|
|
*newdata <- put-index *newdata, i, src
|
2015-07-29 21:37:57 +00:00
|
|
|
i <- add i, 1
|
2015-04-18 04:51:13 +00:00
|
|
|
loop
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def buffer-full? in:address:buffer -> result:boolean [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-19 05:36:36 +00:00
|
|
|
load-ingredients
|
2015-07-29 21:37:57 +00:00
|
|
|
len:number <- get *in, length:offset
|
2016-04-24 18:54:30 +00:00
|
|
|
s:address:array:character <- get *in, data:offset
|
2015-07-29 21:37:57 +00:00
|
|
|
capacity:number <- length *s
|
2015-11-19 05:36:36 +00:00
|
|
|
result <- greater-or-equal len, capacity
|
2015-04-18 04:51:13 +00:00
|
|
|
]
|
|
|
|
|
2015-11-28 05:38:09 +00:00
|
|
|
# most broadly applicable definition of append to a buffer: just call to-text
|
2016-04-24 18:54:30 +00:00
|
|
|
def append buf:address:buffer, x:_elem -> buf:address:buffer [
|
2015-11-28 05:38:09 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
2016-04-24 18:54:30 +00:00
|
|
|
text:address:array:character <- to-text x
|
2015-11-28 05:38:09 +00:00
|
|
|
len:number <- length *text
|
|
|
|
i:number <- copy 0
|
|
|
|
{
|
|
|
|
done?:boolean <- greater-or-equal i, len
|
|
|
|
break-if done?
|
|
|
|
c:character <- index *text, i
|
|
|
|
buf <- append buf, c
|
|
|
|
i <- add i, 1
|
|
|
|
loop
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def append in:address:buffer, c:character -> in:address:buffer [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-19 05:36:36 +00:00
|
|
|
load-ingredients
|
2016-04-11 03:47:44 +00:00
|
|
|
len:number <- get *in, length:offset
|
2015-06-01 06:44:52 +00:00
|
|
|
{
|
|
|
|
# backspace? just drop last character if it exists and return
|
2015-07-29 21:37:57 +00:00
|
|
|
backspace?:boolean <- equal c, 8/backspace
|
|
|
|
break-unless backspace?
|
2016-04-11 03:47:44 +00:00
|
|
|
empty?:boolean <- lesser-or-equal len, 0
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if empty?
|
2016-04-11 03:47:44 +00:00
|
|
|
len <- subtract len, 1
|
2016-04-16 15:12:30 +00:00
|
|
|
*in <- put *in, length:offset, len
|
2016-03-08 09:30:14 +00:00
|
|
|
return
|
2015-06-01 06:44:52 +00:00
|
|
|
}
|
2015-04-18 04:51:13 +00:00
|
|
|
{
|
|
|
|
# grow buffer if necessary
|
2015-07-29 21:37:57 +00:00
|
|
|
full?:boolean <- buffer-full? in
|
|
|
|
break-unless full?
|
|
|
|
in <- grow-buffer in
|
2015-04-18 04:51:13 +00:00
|
|
|
}
|
2016-04-24 18:54:30 +00:00
|
|
|
s:address:array:character <- get *in, data:offset
|
2016-04-23 21:51:20 +00:00
|
|
|
*s <- put-index *s, len, c
|
2016-04-11 03:47:44 +00:00
|
|
|
len <- add len, 1
|
2016-04-16 15:12:30 +00:00
|
|
|
*in <- put *in, length:offset, len
|
2015-04-18 04:51:13 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
scenario buffer-append-works [
|
|
|
|
run [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2016-04-24 18:54:30 +00:00
|
|
|
x:address:buffer <- new-buffer 3
|
|
|
|
s1:address:array:character <- get *x, data:offset
|
2016-02-09 00:20:53 +00:00
|
|
|
c:character <- copy 97/a
|
|
|
|
x <- append x, c
|
|
|
|
c:character <- copy 98/b
|
|
|
|
x <- append x, c
|
|
|
|
c:character <- copy 99/c
|
|
|
|
x <- append x, c
|
2016-04-24 18:54:30 +00:00
|
|
|
s2:address:array:character <- get *x, data:offset
|
2016-01-20 07:18:03 +00:00
|
|
|
1:boolean/raw <- equal s1, s2
|
|
|
|
2:array:character/raw <- copy *s2
|
2015-04-18 15:34:48 +00:00
|
|
|
+buffer-filled
|
2016-02-09 00:20:53 +00:00
|
|
|
c:character <- copy 100/d
|
|
|
|
x <- append x, c
|
2016-04-24 18:54:30 +00:00
|
|
|
s3:address:array:character <- get *x, data:offset
|
2016-01-20 07:18:03 +00:00
|
|
|
10:boolean/raw <- equal s1, s3
|
|
|
|
11:number/raw <- get *x, length:offset
|
|
|
|
12:array:character/raw <- copy *s3
|
2015-04-18 04:51:13 +00:00
|
|
|
]
|
2015-05-02 22:52:22 +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
|
|
|
|
2015-06-06 17:15:43 +00:00
|
|
|
scenario buffer-append-handles-backspace [
|
|
|
|
run [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2016-04-24 18:54:30 +00:00
|
|
|
x:address:buffer <- new-buffer 3
|
2016-02-09 00:20:53 +00:00
|
|
|
c:character <- copy 97/a
|
|
|
|
x <- append x, c
|
|
|
|
c:character <- copy 98/b
|
|
|
|
x <- append x, c
|
|
|
|
c:character <- copy 8/backspace
|
|
|
|
x <- append x, c
|
2016-04-24 18:54:30 +00:00
|
|
|
s:address:array:character <- buffer-to-array x
|
2015-08-24 23:59:28 +00:00
|
|
|
1:array:character/raw <- copy *s
|
2015-06-06 17:15:43 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
1 <- 1 # length
|
|
|
|
2 <- 97 # contents
|
|
|
|
3 <- 0
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def to-text n:number -> result:address:array:character [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-19 05:36:36 +00:00
|
|
|
load-ingredients
|
|
|
|
# is n zero?
|
2015-04-19 03:11:59 +00:00
|
|
|
{
|
2015-07-29 21:37:57 +00:00
|
|
|
break-if n
|
2015-11-19 05:36:36 +00:00
|
|
|
result <- new [0]
|
2016-03-08 09:30:14 +00:00
|
|
|
return
|
2015-04-19 03:11:59 +00:00
|
|
|
}
|
|
|
|
# save sign
|
2015-07-28 21:33:22 +00:00
|
|
|
negate-result:boolean <- copy 0
|
2015-04-19 03:11:59 +00:00
|
|
|
{
|
2015-07-29 21:37:57 +00:00
|
|
|
negative?:boolean <- lesser-than n, 0
|
|
|
|
break-unless negative?
|
|
|
|
negate-result <- copy 1
|
|
|
|
n <- multiply n, -1
|
2015-04-19 03:11:59 +00:00
|
|
|
}
|
|
|
|
# add digits from right to left into intermediate buffer
|
2016-04-24 18:54:30 +00:00
|
|
|
tmp:address:buffer <- new-buffer 30
|
2015-07-28 21:33:22 +00:00
|
|
|
digit-base:number <- copy 48 # '0'
|
2015-04-19 03:11:59 +00:00
|
|
|
{
|
2015-07-29 21:37:57 +00:00
|
|
|
done?:boolean <- equal n, 0
|
|
|
|
break-if done?
|
|
|
|
n, digit:number <- divide-with-remainder n, 10
|
|
|
|
c:character <- add digit-base, digit
|
2016-04-24 18:54:30 +00:00
|
|
|
tmp:address:buffer <- append tmp, c
|
2015-04-19 03:11:59 +00:00
|
|
|
loop
|
|
|
|
}
|
|
|
|
# add sign
|
|
|
|
{
|
|
|
|
break-unless negate-result:boolean
|
2016-02-12 00:13:10 +00:00
|
|
|
minus:character <- copy 45/-
|
|
|
|
tmp <- append tmp, minus
|
2015-04-19 03:11:59 +00:00
|
|
|
}
|
2015-11-21 18:01:12 +00:00
|
|
|
# reverse buffer into text result
|
2015-07-29 21:37:57 +00:00
|
|
|
len:number <- get *tmp, length:offset
|
2016-04-24 18:54:30 +00:00
|
|
|
buf:address:array:character <- get *tmp, data:offset
|
2015-11-19 05:36:36 +00:00
|
|
|
result <- new character:type, len
|
2015-07-29 21:37:57 +00:00
|
|
|
i:number <- subtract len, 1 # source index, decreasing
|
|
|
|
j:number <- copy 0 # destination index, increasing
|
2015-04-19 03:11:59 +00:00
|
|
|
{
|
|
|
|
# while i >= 0
|
2015-07-29 21:37:57 +00:00
|
|
|
done?:boolean <- lesser-than i, 0
|
|
|
|
break-if done?
|
2015-04-19 03:11:59 +00:00
|
|
|
# result[j] = tmp[i]
|
2015-07-29 21:37:57 +00:00
|
|
|
src:character <- index *buf, i
|
2016-04-23 21:51:20 +00:00
|
|
|
*result <- put-index *result, j, src
|
2015-07-29 21:37:57 +00:00
|
|
|
i <- subtract i, 1
|
|
|
|
j <- add j, 1
|
2015-04-19 03:11:59 +00:00
|
|
|
loop
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def to-text x:boolean -> result:address:array:character [
|
2016-01-30 20:07:24 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
n:number <- copy x:boolean
|
|
|
|
result <- to-text n
|
|
|
|
]
|
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def to-text x:address:_elem -> result:address:array:character [
|
2016-02-06 20:51:25 +00:00
|
|
|
local-scope
|
|
|
|
load-ingredients
|
|
|
|
n:number <- copy x
|
|
|
|
result <- to-text n
|
|
|
|
]
|
2016-01-30 20:07:24 +00:00
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def buffer-to-array in:address:buffer -> result:address:array:character [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-19 05:36:36 +00:00
|
|
|
load-ingredients
|
2015-05-27 21:56:01 +00:00
|
|
|
{
|
|
|
|
# propagate null buffer
|
2015-07-29 21:37:57 +00:00
|
|
|
break-if in
|
2016-03-08 09:30:14 +00:00
|
|
|
return 0
|
2015-05-27 21:56:01 +00:00
|
|
|
}
|
2015-07-29 21:37:57 +00:00
|
|
|
len:number <- get *in, length:offset
|
2016-04-24 18:54:30 +00:00
|
|
|
s:address:array:character <- get *in, data:offset
|
2015-05-27 21:56:01 +00:00
|
|
|
# we can't just return s because it is usually the wrong length
|
2015-11-19 05:36:36 +00:00
|
|
|
result <- new character:type, len
|
2015-07-28 21:33:22 +00:00
|
|
|
i:number <- copy 0
|
2015-05-27 21:56:01 +00:00
|
|
|
{
|
2015-07-29 21:37:57 +00:00
|
|
|
done?:boolean <- greater-or-equal i, len
|
|
|
|
break-if done?
|
|
|
|
src:character <- index *s, i
|
2016-04-23 21:51:20 +00:00
|
|
|
*result <- put-index *result, i, src
|
2015-07-29 21:37:57 +00:00
|
|
|
i <- add i, 1
|
2015-05-27 21:56:01 +00:00
|
|
|
loop
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2015-04-19 03:11:59 +00:00
|
|
|
scenario integer-to-decimal-digit-zero [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character/raw <- to-text 0
|
|
|
|
2:array:character/raw <- copy *1:address:array:character/raw
|
2015-04-19 03:11:59 +00:00
|
|
|
]
|
2015-05-02 22:52:22 +00:00
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
2:array:character <- [0]
|
2015-04-19 03:11:59 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario integer-to-decimal-digit-positive [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character/raw <- to-text 234
|
|
|
|
2:array:character/raw <- copy *1:address:array:character/raw
|
2015-04-19 03:11:59 +00:00
|
|
|
]
|
2015-05-02 22:52:22 +00:00
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
2:array:character <- [234]
|
2015-04-19 03:11:59 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario integer-to-decimal-digit-negative [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character/raw <- to-text -1
|
|
|
|
2:array:character/raw <- copy *1:address:array:character/raw
|
2015-04-19 03:11:59 +00:00
|
|
|
]
|
2015-05-02 22:52:22 +00:00
|
|
|
memory-should-contain [
|
2015-04-19 03:11:59 +00:00
|
|
|
2 <- 2
|
|
|
|
3 <- 45 # '-'
|
|
|
|
4 <- 49 # '1'
|
|
|
|
]
|
|
|
|
]
|
2015-04-19 07:13:08 +00:00
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def append a:address:array:character, b:address:array:character -> result:address:array:character [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-19 05:36:36 +00:00
|
|
|
load-ingredients
|
2015-05-23 19:35:10 +00:00
|
|
|
# result = new character[a.length + b.length]
|
2015-07-29 21:37:57 +00:00
|
|
|
a-len:number <- length *a
|
|
|
|
b-len:number <- length *b
|
|
|
|
result-len:number <- add a-len, b-len
|
2015-11-19 05:36:36 +00:00
|
|
|
result <- new character:type, result-len
|
2015-04-19 07:13:08 +00:00
|
|
|
# copy a into result
|
2015-07-28 21:33:22 +00:00
|
|
|
result-idx:number <- copy 0
|
|
|
|
i:number <- copy 0
|
2015-04-19 07:13:08 +00:00
|
|
|
{
|
|
|
|
# while i < a.length
|
2015-07-29 21:37:57 +00:00
|
|
|
a-done?:boolean <- greater-or-equal i, a-len
|
|
|
|
break-if a-done?
|
2015-04-19 07:13:08 +00:00
|
|
|
# result[result-idx] = a[i]
|
2015-07-29 21:37:57 +00:00
|
|
|
in:character <- index *a, i
|
2016-04-23 21:51:20 +00:00
|
|
|
*result <- put-index *result, result-idx, in
|
2015-07-29 21:37:57 +00:00
|
|
|
i <- add i, 1
|
|
|
|
result-idx <- add result-idx, 1
|
2015-04-19 07:13:08 +00:00
|
|
|
loop
|
|
|
|
}
|
|
|
|
# copy b into result
|
2015-07-29 21:37:57 +00:00
|
|
|
i <- copy 0
|
2015-04-19 07:13:08 +00:00
|
|
|
{
|
|
|
|
# while i < b.length
|
2015-07-29 21:37:57 +00:00
|
|
|
b-done?:boolean <- greater-or-equal i, b-len
|
|
|
|
break-if b-done?
|
2015-04-19 07:13:08 +00:00
|
|
|
# result[result-idx] = a[i]
|
2015-07-29 21:37:57 +00:00
|
|
|
in:character <- index *b, i
|
2016-04-23 21:51:20 +00:00
|
|
|
*result <- put-index *result, result-idx, in
|
2015-07-29 21:37:57 +00:00
|
|
|
i <- add i, 1
|
|
|
|
result-idx <- add result-idx, 1
|
2015-04-19 07:13:08 +00:00
|
|
|
loop
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-append-1 [
|
2015-04-19 07:13:08 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character/raw <- new [hello,]
|
|
|
|
2:address:array:character/raw <- new [ world!]
|
|
|
|
3:address:array:character/raw <- append 1:address:array:character/raw, 2:address:array:character/raw
|
|
|
|
4:array:character/raw <- copy *3:address:array:character/raw
|
2015-04-19 07:13:08 +00:00
|
|
|
]
|
2015-05-02 22:52:22 +00:00
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
4:array:character <- [hello, world!]
|
2015-04-19 07:13:08 +00:00
|
|
|
]
|
|
|
|
]
|
2015-04-20 17:25:02 +00:00
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario replace-character-in-text [
|
2015-08-20 05:13:15 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character/raw <- new [abc]
|
|
|
|
1:address:array:character/raw <- replace 1:address:array:character/raw, 98/b, 122/z
|
|
|
|
2:array:character/raw <- copy *1:address:array:character/raw
|
2015-08-20 05:13:15 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
2:array:character <- [azc]
|
2015-08-20 05:13:15 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def replace s:address:array:character, oldc:character, newc:character, from:number/optional -> s:address:array:character [
|
2015-08-20 05:13:15 +00:00
|
|
|
local-scope
|
2015-11-19 05:36:36 +00:00
|
|
|
load-ingredients
|
2015-08-20 05:13:15 +00:00
|
|
|
len:number <- length *s
|
|
|
|
i:number <- find-next s, oldc, from
|
|
|
|
done?:boolean <- greater-or-equal i, len
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if done?, s/same-as-ingredient:0
|
2016-04-23 21:51:20 +00:00
|
|
|
*s <- put-index *s, i, newc
|
2015-08-20 05:13:15 +00:00
|
|
|
i <- add i, 1
|
2015-11-21 18:01:12 +00:00
|
|
|
s <- replace s, oldc, newc, i
|
2015-08-20 05:13:15 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
scenario replace-character-at-start [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character/raw <- new [abc]
|
|
|
|
1:address:array:character/raw <- replace 1:address:array:character/raw, 97/a, 122/z
|
|
|
|
2:array:character/raw <- copy *1:address:array:character/raw
|
2015-08-20 05:13:15 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
2:array:character <- [zbc]
|
2015-08-20 05:13:15 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario replace-character-at-end [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character/raw <- new [abc]
|
|
|
|
1:address:array:character/raw <- replace 1:address:array:character/raw, 99/c, 122/z
|
|
|
|
2:array:character/raw <- copy *1:address:array:character/raw
|
2015-08-20 05:13:15 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
2:array:character <- [abz]
|
2015-08-20 05:13:15 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario replace-character-missing [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character/raw <- new [abc]
|
|
|
|
1:address:array:character/raw <- replace 1:address:array:character/raw, 100/d, 122/z
|
|
|
|
2:array:character/raw <- copy *1:address:array:character/raw
|
2015-08-20 05:13:15 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
2:array:character <- [abc]
|
2015-08-20 05:13:15 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario replace-all-characters [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character/raw <- new [banana]
|
|
|
|
1:address:array:character/raw <- replace 1:address:array:character/raw, 97/a, 122/z
|
|
|
|
2:array:character/raw <- copy *1:address:array:character/raw
|
2015-08-20 05:13:15 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
2:array:character <- [bznznz]
|
2015-08-20 05:13:15 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-04-20 17:25:02 +00:00
|
|
|
# replace underscores in first with remaining args
|
2016-04-24 18:54:30 +00:00
|
|
|
def interpolate template:address:array:character -> result:address:array:character [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-19 05:36:36 +00:00
|
|
|
load-ingredients # consume just the template
|
2015-04-20 17:25:02 +00:00
|
|
|
# compute result-len, space to allocate for result
|
2015-07-29 21:37:57 +00:00
|
|
|
tem-len:number <- length *template
|
|
|
|
result-len:number <- copy tem-len
|
2015-04-20 17:25:02 +00:00
|
|
|
{
|
2015-11-19 05:36:36 +00:00
|
|
|
# while ingredients remain
|
2016-04-24 18:54:30 +00:00
|
|
|
a:address:array:character, arg-received?:boolean <- next-ingredient
|
2015-07-29 21:37:57 +00:00
|
|
|
break-unless arg-received?
|
|
|
|
# result-len = result-len + arg.length - 1 (for the 'underscore' being replaced)
|
|
|
|
a-len:number <- length *a
|
|
|
|
result-len <- add result-len, a-len
|
|
|
|
result-len <- subtract result-len, 1
|
2015-04-20 17:25:02 +00:00
|
|
|
loop
|
|
|
|
}
|
|
|
|
rewind-ingredients
|
|
|
|
_ <- next-ingredient # skip template
|
2016-01-20 07:18:03 +00:00
|
|
|
result <- new character:type, result-len
|
2015-04-20 17:25:02 +00:00
|
|
|
# repeatedly copy sections of template and 'holes' into result
|
2015-07-28 21:33:22 +00:00
|
|
|
result-idx:number <- copy 0
|
|
|
|
i:number <- copy 0
|
2015-04-20 17:25:02 +00:00
|
|
|
{
|
|
|
|
# while arg received
|
2016-04-24 18:54:30 +00:00
|
|
|
a:address:array:character, arg-received?:boolean <- next-ingredient
|
2015-07-29 21:37:57 +00:00
|
|
|
break-unless arg-received?
|
2015-04-20 17:25:02 +00:00
|
|
|
# copy template into result until '_'
|
|
|
|
{
|
|
|
|
# while i < template.length
|
2015-07-29 21:37:57 +00:00
|
|
|
tem-done?:boolean <- greater-or-equal i, tem-len
|
|
|
|
break-if tem-done?, +done:label
|
2015-04-20 17:25:02 +00:00
|
|
|
# while template[i] != '_'
|
2015-07-29 21:37:57 +00:00
|
|
|
in:character <- index *template, i
|
|
|
|
underscore?:boolean <- equal in, 95/_
|
|
|
|
break-if underscore?
|
2015-04-20 17:25:02 +00:00
|
|
|
# result[result-idx] = template[i]
|
2016-04-23 21:51:20 +00:00
|
|
|
*result <- put-index *result, result-idx, in
|
2015-07-29 21:37:57 +00:00
|
|
|
i <- add i, 1
|
|
|
|
result-idx <- add result-idx, 1
|
2015-04-20 17:25:02 +00:00
|
|
|
loop
|
|
|
|
}
|
|
|
|
# copy 'a' into result
|
2015-07-28 21:33:22 +00:00
|
|
|
j:number <- copy 0
|
2015-04-20 17:25:02 +00:00
|
|
|
{
|
|
|
|
# while j < a.length
|
2015-07-29 21:37:57 +00:00
|
|
|
arg-done?:boolean <- greater-or-equal j, a-len
|
|
|
|
break-if arg-done?
|
2015-04-20 17:25:02 +00:00
|
|
|
# result[result-idx] = a[j]
|
2015-07-29 21:37:57 +00:00
|
|
|
in:character <- index *a, j
|
2016-04-23 21:51:20 +00:00
|
|
|
*result <- put-index *result, result-idx, in
|
2015-07-29 21:37:57 +00:00
|
|
|
j <- add j, 1
|
|
|
|
result-idx <- add result-idx, 1
|
2015-04-20 17:25:02 +00:00
|
|
|
loop
|
|
|
|
}
|
|
|
|
# skip '_' in template
|
2015-07-29 21:37:57 +00:00
|
|
|
i <- add i, 1
|
2015-04-20 17:25:02 +00:00
|
|
|
loop # interpolate next arg
|
|
|
|
}
|
2015-05-16 20:17:36 +00:00
|
|
|
+done
|
2015-04-20 17:25:02 +00:00
|
|
|
# done with holes; copy rest of template directly into result
|
|
|
|
{
|
|
|
|
# while i < template.length
|
2015-07-29 21:37:57 +00:00
|
|
|
tem-done?:boolean <- greater-or-equal i, tem-len
|
|
|
|
break-if tem-done?
|
2015-04-20 17:25:02 +00:00
|
|
|
# result[result-idx] = template[i]
|
2015-07-29 21:37:57 +00:00
|
|
|
in:character <- index *template, i
|
2016-04-23 21:51:20 +00:00
|
|
|
*result <- put-index *result, result-idx, in
|
2015-07-29 21:37:57 +00:00
|
|
|
i <- add i, 1
|
|
|
|
result-idx <- add result-idx, 1
|
2015-04-20 17:25:02 +00:00
|
|
|
loop
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario interpolate-works [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character/raw <- new [abc _]
|
|
|
|
2:address:array:character/raw <- new [def]
|
|
|
|
3:address:array:character/raw <- interpolate 1:address:array:character/raw, 2:address:array:character/raw
|
|
|
|
4:array:character/raw <- copy *3:address:array:character/raw
|
2015-04-20 17:25:02 +00:00
|
|
|
]
|
2015-05-02 22:52:22 +00:00
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
4:array:character <- [abc def]
|
2015-04-20 17:25:02 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario interpolate-at-start [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character/raw <- new [_, hello!]
|
|
|
|
2:address:array:character/raw <- new [abc]
|
|
|
|
3:address:array:character/raw <- interpolate 1:address:array:character/raw, 2:address:array:character/raw
|
|
|
|
4:array:character/raw <- copy *3:address:array:character/raw
|
2015-04-20 17:25:02 +00:00
|
|
|
]
|
2015-05-02 22:52:22 +00:00
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
4:array:character <- [abc, hello!]
|
2015-04-20 17:25:02 +00:00
|
|
|
16 <- 0 # out of bounds
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario interpolate-at-end [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character/raw <- new [hello, _]
|
|
|
|
2:address:array:character/raw <- new [abc]
|
|
|
|
3:address:array:character/raw <- interpolate 1:address:array:character/raw, 2:address:array:character/raw
|
|
|
|
4:array:character/raw <- copy *3:address:array:character/raw
|
2015-04-20 17:25:02 +00:00
|
|
|
]
|
2015-05-02 22:52:22 +00:00
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
4:array:character <- [hello, abc]
|
2015-04-20 17:25:02 +00:00
|
|
|
]
|
|
|
|
]
|
2015-05-23 18:37:44 +00:00
|
|
|
|
2015-05-23 19:35:10 +00:00
|
|
|
# result:boolean <- space? c:character
|
2016-03-08 09:30:14 +00:00
|
|
|
def space? c:character -> result:boolean [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-19 05:36:36 +00:00
|
|
|
load-ingredients
|
2015-05-23 19:35:10 +00:00
|
|
|
# most common case first
|
2015-11-19 05:36:36 +00:00
|
|
|
result <- equal c, 32/space
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 10/newline
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 9/tab
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 13/carriage-return
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-05-23 19:30:58 +00:00
|
|
|
# remaining uncommon cases in sorted order
|
|
|
|
# http://unicode.org code-points in unicode-set Z and Pattern_White_Space
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 11/ctrl-k
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 12/ctrl-l
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 133/ctrl-0085
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 160/no-break-space
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 5760/ogham-space-mark
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 8192/en-quad
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 8193/em-quad
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 8194/en-space
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 8195/em-space
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 8196/three-per-em-space
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 8197/four-per-em-space
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 8198/six-per-em-space
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 8199/figure-space
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 8200/punctuation-space
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 8201/thin-space
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 8202/hair-space
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 8206/left-to-right
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 8207/right-to-left
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 8232/line-separator
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 8233/paragraph-separator
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 8239/narrow-no-break-space
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 8287/medium-mathematical-space
|
2016-03-08 09:30:14 +00:00
|
|
|
return-if result
|
2015-07-29 21:37:57 +00:00
|
|
|
result <- equal c, 12288/ideographic-space
|
2015-05-23 19:30:58 +00:00
|
|
|
]
|
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def trim s:address:array:character -> result:address:array:character [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-19 05:36:36 +00:00
|
|
|
load-ingredients
|
2015-07-29 21:37:57 +00:00
|
|
|
len:number <- length *s
|
2015-05-23 18:37:44 +00:00
|
|
|
# left trim: compute start
|
2015-07-28 21:33:22 +00:00
|
|
|
start:number <- copy 0
|
2015-05-23 18:37:44 +00:00
|
|
|
{
|
|
|
|
{
|
2015-07-29 21:37:57 +00:00
|
|
|
at-end?:boolean <- greater-or-equal start, len
|
|
|
|
break-unless at-end?
|
2015-11-19 05:36:36 +00:00
|
|
|
result <- new character:type, 0
|
2016-03-08 09:30:14 +00:00
|
|
|
return
|
2015-05-23 18:37:44 +00:00
|
|
|
}
|
2015-07-29 21:37:57 +00:00
|
|
|
curr:character <- index *s, start
|
|
|
|
whitespace?:boolean <- space? curr
|
|
|
|
break-unless whitespace?
|
|
|
|
start <- add start, 1
|
2015-05-23 18:37:44 +00:00
|
|
|
loop
|
|
|
|
}
|
|
|
|
# right trim: compute end
|
2015-07-29 21:37:57 +00:00
|
|
|
end:number <- subtract len, 1
|
2015-05-23 18:37:44 +00:00
|
|
|
{
|
2015-07-29 21:37:57 +00:00
|
|
|
not-at-start?:boolean <- greater-than end, start
|
|
|
|
assert not-at-start?, [end ran up against start]
|
|
|
|
curr:character <- index *s, end
|
|
|
|
whitespace?:boolean <- space? curr
|
|
|
|
break-unless whitespace?
|
|
|
|
end <- subtract end, 1
|
2015-05-23 18:37:44 +00:00
|
|
|
loop
|
|
|
|
}
|
2015-05-23 19:35:10 +00:00
|
|
|
# result = new character[end+1 - start]
|
2015-07-29 21:37:57 +00:00
|
|
|
new-len:number <- subtract end, start, -1
|
2016-04-24 18:54:30 +00:00
|
|
|
result:address:array:character <- new character:type, new-len
|
2015-08-03 17:09:59 +00:00
|
|
|
# copy the untrimmed parts between start and end
|
2015-07-29 21:37:57 +00:00
|
|
|
i:number <- copy start
|
2015-07-28 21:33:22 +00:00
|
|
|
j:number <- copy 0
|
2015-05-23 18:37:44 +00:00
|
|
|
{
|
|
|
|
# while i <= end
|
2015-07-29 21:37:57 +00:00
|
|
|
done?:boolean <- greater-than i, end
|
|
|
|
break-if done?
|
2015-05-23 18:37:44 +00:00
|
|
|
# result[j] = s[i]
|
2015-07-29 21:37:57 +00:00
|
|
|
src:character <- index *s, i
|
2016-04-23 21:51:20 +00:00
|
|
|
*result <- put-index *result, j, src
|
2015-07-29 21:37:57 +00:00
|
|
|
i <- add i, 1
|
|
|
|
j <- add j, 1
|
2015-05-23 18:37:44 +00:00
|
|
|
loop
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario trim-unmodified [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc]
|
|
|
|
2:address:array:character <- trim 1:address:array:character
|
|
|
|
3:array:character <- copy *2:address:array:character
|
2015-05-23 18:37:44 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
3:array:character <- [abc]
|
2015-05-23 18:37:44 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario trim-left [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [ abc]
|
|
|
|
2:address:array:character <- trim 1:address:array:character
|
|
|
|
3:array:character <- copy *2:address:array:character
|
2015-05-23 18:37:44 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
3:array:character <- [abc]
|
2015-05-23 18:37:44 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario trim-right [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc ]
|
|
|
|
2:address:array:character <- trim 1:address:array:character
|
|
|
|
3:array:character <- copy *2:address:array:character
|
2015-05-23 18:37:44 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
3:array:character <- [abc]
|
2015-05-23 18:37:44 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario trim-left-right [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [ abc ]
|
|
|
|
2:address:array:character <- trim 1:address:array:character
|
|
|
|
3:array:character <- copy *2:address:array:character
|
2015-05-23 18:37:44 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
3:array:character <- [abc]
|
2015-05-23 18:37:44 +00:00
|
|
|
]
|
|
|
|
]
|
2015-05-23 19:30:58 +00:00
|
|
|
|
|
|
|
scenario trim-newline-tab [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [ abc
|
2015-05-23 19:30:58 +00:00
|
|
|
]
|
2016-04-24 18:54:30 +00:00
|
|
|
2:address:array:character <- trim 1:address:array:character
|
|
|
|
3:array:character <- copy *2:address:array:character
|
2015-05-23 19:30:58 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
3:array:character <- [abc]
|
2015-05-23 19:30:58 +00:00
|
|
|
]
|
|
|
|
]
|
2015-06-19 19:28:36 +00:00
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def find-next text:address:array:character, pattern:character, idx:number -> next-index:number [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2016-01-13 06:10:38 +00:00
|
|
|
load-ingredients
|
2015-07-29 21:37:57 +00:00
|
|
|
len:number <- length *text
|
2015-06-19 19:28:36 +00:00
|
|
|
{
|
2015-07-29 21:37:57 +00:00
|
|
|
eof?:boolean <- greater-or-equal idx, len
|
|
|
|
break-if eof?
|
|
|
|
curr:character <- index *text, idx
|
|
|
|
found?:boolean <- equal curr, pattern
|
|
|
|
break-if found?
|
|
|
|
idx <- add idx, 1
|
2015-06-19 19:28:36 +00:00
|
|
|
loop
|
|
|
|
}
|
2016-03-08 09:30:14 +00:00
|
|
|
return idx
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-find-next [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [a/b]
|
|
|
|
2:number <- find-next 1:address:array:character, 47/slash, 0/start-index
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
2 <- 1
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-find-next-empty [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new []
|
|
|
|
2:number <- find-next 1:address:array:character, 47/slash, 0/start-index
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
2 <- 0
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-find-next-initial [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [/abc]
|
|
|
|
2:number <- find-next 1:address:array:character, 47/slash, 0/start-index
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
2 <- 0 # prefix match
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-find-next-final [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc/]
|
|
|
|
2:number <- find-next 1:address:array:character, 47/slash, 0/start-index
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
2 <- 3 # suffix match
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-find-next-missing [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc]
|
|
|
|
2:number <- find-next 1:address:array:character, 47/slash, 0/start-index
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
2 <- 3 # no match
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-find-next-invalid-index [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc]
|
|
|
|
2:number <- find-next 1:address:array:character, 47/slash, 4/start-index
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
2 <- 4 # no change
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-find-next-first [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [ab/c/]
|
|
|
|
2:number <- find-next 1:address:array:character, 47/slash, 0/start-index
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
2 <- 2 # first '/' of multiple
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-find-next-second [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [ab/c/]
|
|
|
|
2:number <- find-next 1:address:array:character, 47/slash, 3/start-index
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
2 <- 4 # second '/' of multiple
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
# search for a pattern of multiple characters
|
2015-06-19 19:28:36 +00:00
|
|
|
# fairly dumb algorithm
|
2016-04-24 18:54:30 +00:00
|
|
|
def find-next text:address:array:character, pattern:address:array:character, idx:number -> next-index:number [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-19 05:36:36 +00:00
|
|
|
load-ingredients
|
2015-07-29 21:37:57 +00:00
|
|
|
first:character <- index *pattern, 0
|
2015-06-19 19:28:36 +00:00
|
|
|
# repeatedly check for match at current idx
|
2015-07-29 21:37:57 +00:00
|
|
|
len:number <- length *text
|
2015-06-19 19:28:36 +00:00
|
|
|
{
|
2015-11-21 18:01:12 +00:00
|
|
|
# does some unnecessary work checking even when there isn't enough of text left
|
2015-07-29 21:37:57 +00:00
|
|
|
done?:boolean <- greater-or-equal idx, len
|
|
|
|
break-if done?
|
|
|
|
found?:boolean <- match-at text, pattern, idx
|
|
|
|
break-if found?
|
|
|
|
idx <- add idx, 1
|
2015-06-19 19:28:36 +00:00
|
|
|
# optimization: skip past indices that definitely won't match
|
2015-07-29 21:37:57 +00:00
|
|
|
idx <- find-next text, first, idx
|
2015-06-19 19:28:36 +00:00
|
|
|
loop
|
|
|
|
}
|
2016-03-08 09:30:14 +00:00
|
|
|
return idx
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario find-next-text-1 [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc]
|
|
|
|
2:address:array:character <- new [bc]
|
|
|
|
3:number <- find-next 1:address:array:character, 2:address:array:character, 0
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 1
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario find-next-text-2 [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abcd]
|
|
|
|
2:address:array:character <- new [bc]
|
|
|
|
3:number <- find-next 1:address:array:character, 2:address:array:character, 1
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 1
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario find-next-no-match [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc]
|
|
|
|
2:address:array:character <- new [bd]
|
|
|
|
3:number <- find-next 1:address:array:character, 2:address:array:character, 0
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 3 # not found
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario find-next-suffix-match [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abcd]
|
|
|
|
2:address:array:character <- new [cd]
|
|
|
|
3:number <- find-next 1:address:array:character, 2:address:array:character, 0
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 2
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario find-next-suffix-match-2 [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abcd]
|
|
|
|
2:address:array:character <- new [cde]
|
|
|
|
3:number <- find-next 1:address:array:character, 2:address:array:character, 0
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 4 # not found
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
# checks if pattern matches at index 'idx'
|
2016-04-24 18:54:30 +00:00
|
|
|
def match-at text:address:array:character, pattern:address:array:character, idx:number -> result:boolean [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-19 05:36:36 +00:00
|
|
|
load-ingredients
|
2015-07-29 21:37:57 +00:00
|
|
|
pattern-len:number <- length *pattern
|
2015-06-19 19:28:36 +00:00
|
|
|
# check that there's space left for the pattern
|
|
|
|
{
|
2015-07-29 21:37:57 +00:00
|
|
|
x:number <- length *text
|
|
|
|
x <- subtract x, pattern-len
|
|
|
|
enough-room?:boolean <- lesser-or-equal idx, x
|
|
|
|
break-if enough-room?
|
2016-03-08 09:30:14 +00:00
|
|
|
return 0/not-found
|
2015-06-19 19:28:36 +00:00
|
|
|
}
|
|
|
|
# check each character of pattern
|
2015-07-28 21:33:22 +00:00
|
|
|
pattern-idx:number <- copy 0
|
2015-06-19 19:28:36 +00:00
|
|
|
{
|
2015-07-29 21:37:57 +00:00
|
|
|
done?:boolean <- greater-or-equal pattern-idx, pattern-len
|
|
|
|
break-if done?
|
|
|
|
c:character <- index *text, idx
|
|
|
|
exp:character <- index *pattern, pattern-idx
|
2015-06-19 19:28:36 +00:00
|
|
|
{
|
2015-07-29 21:37:57 +00:00
|
|
|
match?:boolean <- equal c, exp
|
|
|
|
break-if match?
|
2016-03-08 09:30:14 +00:00
|
|
|
return 0/not-found
|
2015-06-19 19:28:36 +00:00
|
|
|
}
|
2015-07-29 21:37:57 +00:00
|
|
|
idx <- add idx, 1
|
|
|
|
pattern-idx <- add pattern-idx, 1
|
2015-06-19 19:28:36 +00:00
|
|
|
loop
|
|
|
|
}
|
2016-03-08 09:30:14 +00:00
|
|
|
return 1/found
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario match-at-checks-pattern-at-index [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc]
|
|
|
|
2:address:array:character <- new [ab]
|
|
|
|
3:boolean <- match-at 1:address:array:character, 2:address:array:character, 0
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 1 # match found
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario match-at-reflexive [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc]
|
|
|
|
3:boolean <- match-at 1:address:array:character, 1:address:array:character, 0
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 1 # match found
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario match-at-outside-bounds [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc]
|
|
|
|
2:address:array:character <- new [a]
|
|
|
|
3:boolean <- match-at 1:address:array:character, 2:address:array:character, 4
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 0 # never matches
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario match-at-empty-pattern [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc]
|
|
|
|
2:address:array:character <- new []
|
|
|
|
3:boolean <- match-at 1:address:array:character, 2:address:array:character, 0
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 1 # always matches empty pattern given a valid index
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario match-at-empty-pattern-outside-bound [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc]
|
|
|
|
2:address:array:character <- new []
|
|
|
|
3:boolean <- match-at 1:address:array:character, 2:address:array:character, 4
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 0 # no match
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario match-at-empty-text [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new []
|
|
|
|
2:address:array:character <- new [abc]
|
|
|
|
3:boolean <- match-at 1:address:array:character, 2:address:array:character, 0
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 0 # no match
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario match-at-empty-against-empty [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new []
|
|
|
|
3:boolean <- match-at 1:address:array:character, 1:address:array:character, 0
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 1 # matches because pattern is also empty
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario match-at-inside-bounds [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc]
|
|
|
|
2:address:array:character <- new [bc]
|
|
|
|
3:boolean <- match-at 1:address:array:character, 2:address:array:character, 1
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
2015-11-21 18:01:12 +00:00
|
|
|
3 <- 1 # match
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
scenario match-at-inside-bounds-2 [
|
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc]
|
|
|
|
2:address:array:character <- new [bc]
|
|
|
|
3:boolean <- match-at 1:address:array:character, 2:address:array:character, 0
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 0 # no match
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def split s:address:array:character, delim:character -> result:address:array:address:array:character [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-19 05:36:36 +00:00
|
|
|
load-ingredients
|
2015-11-21 18:01:12 +00:00
|
|
|
# empty text? return empty array
|
2015-07-29 21:37:57 +00:00
|
|
|
len:number <- length *s
|
2015-06-19 19:28:36 +00:00
|
|
|
{
|
2015-07-29 21:37:57 +00:00
|
|
|
empty?:boolean <- equal len, 0
|
|
|
|
break-unless empty?
|
2016-04-24 18:54:30 +00:00
|
|
|
result <- new {(address array character): type}, 0
|
2016-03-08 09:30:14 +00:00
|
|
|
return
|
2015-06-19 19:28:36 +00:00
|
|
|
}
|
|
|
|
# count #pieces we need room for
|
2015-07-28 21:33:22 +00:00
|
|
|
count:number <- copy 1 # n delimiters = n+1 pieces
|
|
|
|
idx:number <- copy 0
|
2015-06-19 19:28:36 +00:00
|
|
|
{
|
2015-07-29 21:37:57 +00:00
|
|
|
idx <- find-next s, delim, idx
|
|
|
|
done?:boolean <- greater-or-equal idx, len
|
|
|
|
break-if done?
|
|
|
|
idx <- add idx, 1
|
|
|
|
count <- add count, 1
|
2015-06-19 19:28:36 +00:00
|
|
|
loop
|
|
|
|
}
|
|
|
|
# allocate space
|
2016-04-24 18:54:30 +00:00
|
|
|
result <- new {(address array character): type}, count
|
2015-06-19 19:28:36 +00:00
|
|
|
# repeatedly copy slices start..end until delimiter into result[curr-result]
|
2015-07-28 21:33:22 +00:00
|
|
|
curr-result:number <- copy 0
|
|
|
|
start:number <- copy 0
|
2015-06-19 19:28:36 +00:00
|
|
|
{
|
|
|
|
# while next delim exists
|
2015-07-29 21:37:57 +00:00
|
|
|
done?:boolean <- greater-or-equal start, len
|
|
|
|
break-if done?
|
|
|
|
end:number <- find-next s, delim, start
|
2015-06-19 19:28:36 +00:00
|
|
|
# copy start..end into result[curr-result]
|
2016-04-24 18:54:30 +00:00
|
|
|
dest:address:array:character <- copy-range s, start, end
|
2016-04-23 21:51:20 +00:00
|
|
|
*result <- put-index *result, curr-result, dest
|
2015-06-19 19:28:36 +00:00
|
|
|
# slide over to next slice
|
2015-07-29 21:37:57 +00:00
|
|
|
start <- add end, 1
|
|
|
|
curr-result <- add curr-result, 1
|
2015-06-19 19:28:36 +00:00
|
|
|
loop
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-split-1 [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [a/b]
|
|
|
|
2:address:array:address:array:character <- split 1:address:array:character, 47/slash
|
|
|
|
3:number <- length *2:address:array:address:array:character
|
|
|
|
4:address:array:character <- index *2:address:array:address:array:character, 0
|
|
|
|
5:address:array:character <- index *2:address:array:address:array:character, 1
|
|
|
|
10:array:character <- copy *4:address:array:character
|
|
|
|
20:array:character <- copy *5:address:array:character
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 2 # length of result
|
2015-11-21 17:26:30 +00:00
|
|
|
10:array:character <- [a]
|
|
|
|
20:array:character <- [b]
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-split-2 [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [a/b/c]
|
|
|
|
2:address:array:address:array:character <- split 1:address:array:character, 47/slash
|
|
|
|
3:number <- length *2:address:array:address:array:character
|
|
|
|
4:address:array:character <- index *2:address:array:address:array:character, 0
|
|
|
|
5:address:array:character <- index *2:address:array:address:array:character, 1
|
|
|
|
6:address:array:character <- index *2:address:array:address:array:character, 2
|
|
|
|
10:array:character <- copy *4:address:array:character
|
|
|
|
20:array:character <- copy *5:address:array:character
|
|
|
|
30:array:character <- copy *6:address:array:character
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 3 # length of result
|
2015-11-21 17:26:30 +00:00
|
|
|
10:array:character <- [a]
|
|
|
|
20:array:character <- [b]
|
|
|
|
30:array:character <- [c]
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-split-missing [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc]
|
|
|
|
2:address:array:address:array:character <- split 1:address:array:character, 47/slash
|
|
|
|
3:number <- length *2:address:array:address:array:character
|
|
|
|
4:address:array:character <- index *2:address:array:address:array:character, 0
|
|
|
|
10:array:character <- copy *4:address:array:character
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 1 # length of result
|
2015-11-21 17:26:30 +00:00
|
|
|
10:array:character <- [abc]
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-split-empty [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new []
|
|
|
|
2:address:array:address:array:character <- split 1:address:array:character, 47/slash
|
|
|
|
3:number <- length *2:address:array:address:array:character
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 0 # empty result
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-split-empty-piece [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [a/b//c]
|
|
|
|
2:address:array:address:array:character <- split 1:address:array:character, 47/slash
|
|
|
|
3:number <- length *2:address:array:address:array:character
|
|
|
|
4:address:array:character <- index *2:address:array:address:array:character, 0
|
|
|
|
5:address:array:character <- index *2:address:array:address:array:character, 1
|
|
|
|
6:address:array:character <- index *2:address:array:address:array:character, 2
|
|
|
|
7:address:array:character <- index *2:address:array:address:array:character, 3
|
|
|
|
10:array:character <- copy *4:address:array:character
|
|
|
|
20:array:character <- copy *5:address:array:character
|
|
|
|
30:array:character <- copy *6:address:array:character
|
|
|
|
40:array:character <- copy *7:address:array:character
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
|
|
|
3 <- 4 # length of result
|
2015-11-21 17:26:30 +00:00
|
|
|
10:array:character <- [a]
|
|
|
|
20:array:character <- [b]
|
|
|
|
30:array:character <- []
|
|
|
|
40:array:character <- [c]
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def split-first text:address:array:character, delim:character -> x:address:array:character, y:address:array:character [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-19 05:36:36 +00:00
|
|
|
load-ingredients
|
2015-11-21 18:01:12 +00:00
|
|
|
# empty text? return empty texts
|
2015-07-29 21:37:57 +00:00
|
|
|
len:number <- length *text
|
2015-06-19 19:28:36 +00:00
|
|
|
{
|
2015-07-29 21:37:57 +00:00
|
|
|
empty?:boolean <- equal len, 0
|
|
|
|
break-unless empty?
|
2016-04-24 18:54:30 +00:00
|
|
|
x:address:array:character <- new []
|
|
|
|
y:address:array:character <- new []
|
2016-03-08 09:30:14 +00:00
|
|
|
return
|
2015-06-19 19:28:36 +00:00
|
|
|
}
|
2015-07-29 21:37:57 +00:00
|
|
|
idx:number <- find-next text, delim, 0
|
2016-04-24 18:54:30 +00:00
|
|
|
x:address:array:character <- copy-range text, 0, idx
|
2015-07-29 21:37:57 +00:00
|
|
|
idx <- add idx, 1
|
2016-04-24 18:54:30 +00:00
|
|
|
y:address:array:character <- copy-range text, idx, len
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-split-first [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [a/b]
|
|
|
|
2:address:array:character, 3:address:array:character <- split-first 1:address:array:character, 47/slash
|
|
|
|
10:array:character <- copy *2:address:array:character
|
|
|
|
20:array:character <- copy *3:address:array:character
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
10:array:character <- [a]
|
|
|
|
20:array:character <- [b]
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2016-04-24 18:54:30 +00:00
|
|
|
def copy-range buf:address:array:character, start:number, end:number -> result:address:array:character [
|
2015-07-14 05:43:16 +00:00
|
|
|
local-scope
|
2015-11-19 05:36:36 +00:00
|
|
|
load-ingredients
|
2015-06-19 19:28:36 +00:00
|
|
|
# if end is out of bounds, trim it
|
2015-07-29 21:37:57 +00:00
|
|
|
len:number <- length *buf
|
|
|
|
end:number <- min len, end
|
2015-06-19 19:28:36 +00:00
|
|
|
# allocate space for result
|
2015-07-29 21:37:57 +00:00
|
|
|
len <- subtract end, start
|
2016-04-24 18:54:30 +00:00
|
|
|
result:address:array:character <- new character:type, len
|
2015-06-19 19:28:36 +00:00
|
|
|
# copy start..end into result[curr-result]
|
2015-07-29 21:37:57 +00:00
|
|
|
src-idx:number <- copy start
|
2015-07-28 21:33:22 +00:00
|
|
|
dest-idx:number <- copy 0
|
2015-06-19 19:28:36 +00:00
|
|
|
{
|
2015-07-29 21:37:57 +00:00
|
|
|
done?:boolean <- greater-or-equal src-idx, end
|
|
|
|
break-if done?
|
|
|
|
src:character <- index *buf, src-idx
|
2016-04-23 21:51:20 +00:00
|
|
|
*result <- put-index *result, dest-idx, src
|
2015-07-29 21:37:57 +00:00
|
|
|
src-idx <- add src-idx, 1
|
|
|
|
dest-idx <- add dest-idx, 1
|
2015-06-19 19:28:36 +00:00
|
|
|
loop
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-copy-copies-partial-text [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc]
|
|
|
|
2:address:array:character <- copy-range 1:address:array:character, 1, 3
|
|
|
|
3:array:character <- copy *2:address:array:character
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
3:array:character <- [bc]
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-copy-out-of-bounds [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc]
|
|
|
|
2:address:array:character <- copy-range 1:address:array:character, 2, 4
|
|
|
|
3:array:character <- copy *2:address:array:character
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
3:array:character <- [c]
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
2015-11-21 18:01:12 +00:00
|
|
|
scenario text-copy-out-of-bounds-2 [
|
2015-06-19 19:28:36 +00:00
|
|
|
run [
|
2016-04-24 18:54:30 +00:00
|
|
|
1:address:array:character <- new [abc]
|
|
|
|
2:address:array:character <- copy-range 1:address:array:character, 3, 3
|
|
|
|
3:array:character <- copy *2:address:array:character
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
memory-should-contain [
|
2015-11-21 17:26:30 +00:00
|
|
|
3:array:character <- []
|
2015-06-19 19:28:36 +00:00
|
|
|
]
|
|
|
|
]
|