# HG changeset patch # User Mike Pavone # Date 1376079640 25200 # Node ID 3e9cb69e516db14bf175fc0a0f7ad39b54c3986c # Parent 7dfa4481deb000e91c9f6bf7b6d66a87c8ed2d35 Added from and from:withLength for doing substring operations diff -r 7dfa4481deb0 -r 3e9cb69e516d modules/string.tp --- 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 } } diff -r 7dfa4481deb0 -r 3e9cb69e516d samples/stringops.tp --- 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" } }