# HG changeset patch # User Mike Pavone # Date 1376067920 25200 # Node ID 7dfa4481deb000e91c9f6bf7b6d66a87c8ed2d35 # Parent 7f442b3e4448d97722a4e92dfcfee30c5726d714 Implement find:else on string objects diff -r 7f442b3e4448 -r 7dfa4481deb0 modules/string.tp --- a/modules/string.tp Fri Aug 09 05:28:35 2013 -0700 +++ b/modules/string.tp Fri Aug 09 10:05:20 2013 -0700 @@ -94,5 +94,32 @@ intret } + llMessage: find:else withVars: { + intret <- obj_int32 ptr + oneedle <- object ptr + ifNotFound <- object ptr + sneedle <- string ptr + i <- uint32_t + notFound <- uint32_t + } andCode: :oneedle :ifNotFound { + sneedle <- mcall: string 1 oneedle + i <- 0 + notFound <- 1 + while: { notFound && i + (sneedle bytes) <= bytes} do: { + if: (memcmp: data + i (sneedle data) (sneedle bytes)) = 0 { + notFound <- 0 + } else: { + i <- i + 1 + } + } + if: notFound { + ccall: ifNotFound 0 + } else: { + intret <- make_object: (addr_of: obj_int32_meta) NULL 0 + intret num!: i + intret + } + } + isInteger? <- { false } } diff -r 7f442b3e4448 -r 7dfa4481deb0 samples/stringops.tp --- a/samples/stringops.tp Fri Aug 09 05:28:35 2013 -0700 +++ b/samples/stringops.tp Fri Aug 09 10:05:20 2013 -0700 @@ -2,5 +2,9 @@ main <- { print: "foo: " . 42 . "\n" print: (string: (length: "foo" . "bar")) . "\n" + print: (string: ("foobarbaz" find: "foo" else: { "not found" })) . "\n" + print: (string: ("foobarbaz" find: "bar" else: { "not found" })) . "\n" + print: (string: ("foobarbaz" find: "baz" else: { "not found" })) . "\n" + print: (string: ("foobarbaz" find: "qux" else: { "not found" })) . "\n" } }