# HG changeset patch # User Mike Pavone # Date 1376104324 25200 # Node ID a6739206a9e362c7cb55e3f999f0e56a1c896e4a # Parent 3e9cb69e516db14bf175fc0a0f7ad39b54c3986c Add splitOn and partitionOn to string objects diff -r 3e9cb69e516d -r a6739206a9e3 modules/string.tp --- a/modules/string.tp Fri Aug 09 13:20:40 2013 -0700 +++ b/modules/string.tp Fri Aug 09 20:12:04 2013 -0700 @@ -151,5 +151,36 @@ from: start withLength: length } + partitionOn <- :delim { + pos <- find: delim else: { -1 } + if: pos >= 0 { + _before <- from: 0 withLength: pos + _after <- from: (pos + (delim length)) + #{ + before <- _before + after <- _after + } + } else: { + _before <- self + #{ + before <- _before + after <- "" + } + } + } + + splitOn <- :delim { + pos <- 0 + pieces <- #[] + while: { + pos <- find: delim else: { -1 } + pos >= 0 + } do: { + pieces append: (from: 0 withLength: pos) + self <- from: pos + (delim length) + } + pieces append: self + } + isInteger? <- { false } } diff -r 3e9cb69e516d -r a6739206a9e3 samples/stringops.tp --- a/samples/stringops.tp Fri Aug 09 13:20:40 2013 -0700 +++ b/samples/stringops.tp Fri Aug 09 20:12:04 2013 -0700 @@ -9,5 +9,13 @@ print: ("foobarbaz" from: 3) . "\n" print: ("foobarbaz" from: 3 withLength: 3) . "\n" + + foreach: ("foo,bar,baz,qux" splitOn: ",") :idx val { + print: val . "\n" + } + + res <- "foobarbaz" partitionOn: "bar" + print: "Before: " . (res before) . "\n" + print: "After: " . (res after) . "\n" } }