Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -0,0 +1,23 @@
# syntax: GAWK -f MULTISPLIT.AWK
BEGIN {
str = "a!===b=!=c"
sep = "(==|!=|=)"
printf("str: %s\n",str)
printf("sep: %s\n\n",sep)
n = split(str,str_arr,sep,sep_arr)
printf("parsed: ")
for (i=1; i<=n; i++) {
printf("'%s'",str_arr[i])
if (i<n) { printf(" '%s' ",sep_arr[i]) }
}
printf("\n\nstrings: ")
for (i=1; i<=n; i++) {
printf("'%s' ",str_arr[i])
}
printf("\n\nseparators: ")
for (i=1; i<n; i++) {
printf("'%s' ",sep_arr[i])
}
printf("\n")
exit(0)
}

View file

@ -1,12 +1,12 @@
multisplit=:4 :0
'sep begin'=.|:t=. y /:~&.:(|."1)@;@(i.@#@[ ,.L:0"0 I.@E.L:0) x
multisplit=: 4 :0
'sep begin'=. |: t=. y /:~&.:(|."1)@;@(i.@#@[ ,.L:0"0 I.@E.L:0) x
end=. begin + sep { #@>y
last=.next=.0
r=.2 0$0
while.next<#begin do.
r=.r,.(last}.x{.~next{begin);next{t
last=.next{end
next=.1 i.~(begin>next{begin)*.begin>:last
last=. next=. 0
r=. 2 0$0
while. next<#begin do.
r=. r,.(last}.x{.~next{begin);next{t
last=. next{end
next=. 1 i.~(begin>next{begin)*.begin>:last
end.
r=.r,.'';~last}.x
r=. r,.'';~last}.x
)

View file

@ -1,4 +1,4 @@
S=:'a!===b=!=c'
S=: 'a!===b=!=c'
S multisplit '==';'!=';'='
┌───┬───┬───┬───┬─┐
│a │ │b │ │c│

View file

@ -1,24 +1,22 @@
>>> def ms(txt="a!===b=!=c", sep=["==", "!=", "="]):
if not txt or not sep:
return []
size = [len(s) for s in sep]
ans, pos0 = [], 0
def getfinds():
return [(-txt.find(s, pos0), -sepnum, size[sepnum])
for sepnum, s in enumerate(sep)
if s in txt[pos0:]]
def multisplit(text, sep):
lastmatch = i = 0
matches = []
while i < len(text):
for j, s in enumerate(sep):
if text[i:].startswith(s):
if i > lastmatch:
matches.append(text[lastmatch:i])
matches.append((j, i)) # Replace the string containing the matched separator with a tuple of which separator and where in the string the match occured
lastmatch = i + len(s)
i += len(s)
break
else:
i += 1
if i > lastmatch:
matches.append(text[lastmatch:i])
return matches
finds = getfinds()
while finds:
pos, snum, sz = max(finds)
pos, snum = -pos, -snum
ans += [ txt[pos0:pos], [snum, pos] ]
pos0 = pos+sz
finds = getfinds()
if txt[pos0:]: ans += [ txt[pos0:] ]
return ans
>>> ms()
['a', [1, 1], '', [0, 3], 'b', [2, 6], '', [1, 7], 'c']
>>> ms(txt="a!===b=!=c", sep=["=", "!=", "=="])
['a', [1, 1], '', [0, 3], '', [0, 4], 'b', [0, 6], '', [1, 7], 'c']
>>> multisplit('a!===b=!=c', ['==', '!=', '='])
['a', (1, 1), (0, 3), 'b', (2, 6), (1, 7), 'c']
>>> multisplit('a!===b=!=c', ['!=', '==', '='])
['a', (0, 1), (1, 3), 'b', (2, 6), (0, 7), 'c']