changeset 151:3e9cb69e516d

Added from and from:withLength for doing substring operations
author Mike Pavone <pavone@retrodev.com>
date Fri, 09 Aug 2013 13:20:40 -0700
parents 7dfa4481deb0
children a6739206a9e3
files modules/string.tp samples/stringops.tp
diffstat 2 files changed, 33 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/modules/string.tp	Fri Aug 09 10:05:20 2013 -0700
+++ b/modules/string.tp	Fri Aug 09 13:20:40 2013 -0700
@@ -121,5 +121,35 @@
 		}
 	}
 
+	llMessage: from:withLength withVars: {
+		from <- obj_int32 ptr
+		tocopy <- obj_int32 ptr
+		ret <- string ptr
+		start <- int32_t
+		clampedLen <- int32_t
+	} andCode: :from :tocopy {
+		start <- from num
+		if: start < 0 {
+			start <- bytes + start
+		}
+		if: start > bytes {
+			start <- bytes
+		}
+		clampedLen <- tocopy num
+		if: start + clampedLen > bytes {
+			clampedLen <- bytes - start
+		}
+		ret <- make_object: (addr_of: string_meta) NULL 0
+		ret data!: (GC_MALLOC_ATOMIC: clampedLen + 1)
+		memcpy: (ret data) data + start clampedLen
+		ret len!: clampedLen
+		ret bytes!: clampedLen
+		ret
+	}
+
+	from <- :start {
+		from: start withLength: length
+	}
+
 	isInteger? <- { false }
 }
--- a/samples/stringops.tp	Fri Aug 09 10:05:20 2013 -0700
+++ b/samples/stringops.tp	Fri Aug 09 13:20:40 2013 -0700
@@ -6,5 +6,8 @@
 		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"
+
+		print: ("foobarbaz" from: 3) . "\n"
+		print: ("foobarbaz" from: 3 withLength: 3) . "\n"
 	}
 }