changeset 150:7dfa4481deb0

Implement find:else on string objects
author Mike Pavone <pavone@retrodev.com>
date Fri, 09 Aug 2013 10:05:20 -0700
parents 7f442b3e4448
children 3e9cb69e516d
files modules/string.tp samples/stringops.tp
diffstat 2 files changed, 31 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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 }
 }
--- 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"
 	}
 }