# Some useful helpers for dealing with text (arrays of characters) # to-text-line gets called implicitly in various places # define it to be identical to 'to-text' by default def to-text-line x:_elem -> y:address:array:character [ local-scope load-ingredients y <- to-text x ] # variant for arrays (since we can't pass them around otherwise) def array-to-text-line x:address:array:_elem -> y:address:array:character [ local-scope load-ingredients y <- to-text *x ] def equal a:address:array:character, b:address:array:character -> result:boolean [ local-scope load-ingredients a-len:number <- length *a b-len:number <- length *b # compare lengths { trace 99, [text-equal], [comparing lengths] length-equal?:boolean <- equal a-len, b-len break-if length-equal? return 0 } # compare each corresponding character trace 99, [text-equal], [comparing characters] i:number <- copy 0 { done?:boolean <- greater-or-equal i, a-len break-if done? a2:character <- index *a, i b2:character <- index *b, i { chars-match?:boolean <- equal a2, b2 break-if chars-match? return 0 } i <- add i, 1 loop } return 1 ] scenario text-equal-reflexive [ run [ local-scope x:address:array:character <- new [abc] 10:boolean/raw <- equal x, x ] memory-should-contain [ 10 <- 1 # x == x for all x ] ] scenario text-equal-identical [ run [ local-scope x:address:array:character <- new [abc] y:address:array:character <- new [abc] 10:boolean/raw <- equal x, y ] memory-should-contain [ 10 <- 1 # abc == abc ] ] scenario text-equal-distinct-lengths [ run [ local-scope x:address:array:character <- new [abc] y:address:array:character <- new [abcd] 10:boolean/raw <- equal x, y ] memory-should-contain [ 10 <- 0 # abc != abcd ] trace-should-contain [ text-equal: comparing lengths ] trace-should-not-contain [ text-equal: comparing characters ] ] scenario text-equal-with-empty [ run [ local-scope x:address:array:character <- new [] y:address:array:character <- new [abcd] 10:boolean/raw <- equal x, y ] memory-should-contain [ 10 <- 0 # "" != abcd ] ] scenario text-equal-common-lengths-but-distinct [ run [ local-scope x:address:array:character <- new [abc] y:address:array:character <- new [abd] 10:boolean/raw <- equal x, y ] memory-should-contain [ 10 <- 0 # abc != abd ] ] # A new type to help incrementally construct texts. container buffer [ length:number data:address:array:character ] def new-buffer capacity:number -> result:address:buffer [ local-scope load-ingredients result <- new buffer:type *result <- put *result, length:offset, 0 data:address:array:character <- new character:type, capacity *result <- put *result, data:offset, data return result ] def grow-buffer in:address:buffer -> in:address:buffer [ local-scope load-ingredients # double buffer size olddata:address:array:character <- get *in, data:offset oldlen:number <- length *olddata newlen:number <- multiply oldlen, 2 newdata:address:array:character <- new ch
Copyright (c) 2009, 2010 hut <hut@lavabit.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.