This is great but theres another one that goes with and is built on those two called token.
Basically it pulls a token up to a separator string off the front of a string and returns the token (if any) and the remainder of the string.
allowing such simplified inanities as
s = 'command:=p1,p2,p3' # a line fulled from a config file , say
tag = s.token(':=', out s)
assert tag == 'command' and s.startsWith('p1')
arg = s.token(',', out s)
assert arg == 'p1'
arg = s.token(',', out s)
assert arg == 'p2'
arg = s.token(',', out s)
assert arg == 'p3'
assert not s.length
Heres the implementation and tests
My original version had just token returning a single value and using an out variable
but I thought it may be a better direction to take to use the cobra multi return value capability so
theres one doing that as well ( tokenl) - your call as to which if any is preferable.
namespace Cobra.Lang
extend String
# ... before and after
def token(sep as String, rest as out String) as String
"""
Return substring before 'sep' and set the remainder of the string
after the separator into 'rest'.
If no separator in string return the entire string and set rest to an
empty string.
"""
test
s = 'cmd1:p1,p2'
c = s.token(':', out s)
assert c == 'cmd1' and s.startsWith('p1')
arg = s.token(',', out s)
assert arg == 'p1'
arg = s.token(',', out s)
assert arg == 'p2'
assert not s.length
s = 'cmd1: p1, p2'
c = s.token(': ', out s)
assert c == 'cmd1' and s.startsWith('p1')
arg = s.token(', ', out s)
assert arg == 'p1'
arg = s.token(', ', out s)
assert arg == 'p2'
assert not s.length
s0='pp,qq'
ss,s = s0.tokenl('=')
assert ss == 'pp,qq'
assert ss == s0
assert not s.length
body
t = .before(sep)
rest = .after(sep)
return t
def tokenl(sep as String) as List<of String>
"""
Return 2 element list, first element is first token from the string
up to the separator and the second element is the remainder of
the string after the separator.
If no separator in string returns the entire string as first element and
an empty string as second element.
"""
test
s = 'cmd1: p1, p2, p3'
c,s = s.tokenl(':')
assert c == 'cmd1'
assert s.startsWith(' p1')
s1,s = s.tokenl(',')
assert s1 == ' p1'
s2,s = s.tokenl(',')
assert s2 == ' p2'
s3,s = s.tokenl(',')
assert s3 == ' p3'
assert not s.length
s3,s = s.tokenl(',')
assert not s3.length
assert not s.length
s='pp,qq'
s3,s = s.tokenl('=')
assert s3 == 'pp,qq'
assert not s.length
body
t = .before(sep)
s = .after(sep)
return [t,s]
# tests and examples
class Entry
def main is shared
Entry.testTkn
def testTkn is shared, private
s = 'filename.exe'
assert s.before('.') == 'filename'
assert s.after('.') == 'exe'
s = 'filename1'
assert s.before('.') == 'filename1'
assert s.after('.') == ''
s = '.ext'
assert s.before('.') == ''
assert s.after('.') == 'ext'
s = 'wookie'
assert s.before('.') == 'wookie'
assert s.after('.') == ''
assert 'exeter.exe'.before('.') + '.pag' == 'exeter.pag'
sw='sent1.sent2'
assert sw.after('.') +'.' + sw.before('.') == 'sent2.sent1'
s = 'command:=p1,p2,p3'
c = s.token(':=', out s)
assert c == 'command' and s.startsWith('p1')
arg = s.token(',', out s)
assert arg == 'p1'
arg = s.token(',', out s)
assert arg == 'p2'
arg = s.token(',', out s)
assert arg == 'p3'
assert not s.length
s = 'this is a set of word tokens' # 1 spc seperated
res = ['this', 'is', 'a', 'set', 'of', 'word', 'tokens']
i =0
while true
w = s.token(' ', out s)
if not w.length, break
assert w == res[i]
i += 1
assert not s.length
s = 'simple,comma,sep words,and, phrases'
while (w = s.token(',', out s)).length
assert w in {'simple', 'comma', 'sep words', 'and', ' phrases'}
assert not s.length