extracted some functions from util

alfons-task
Nathan DECHER 4 years ago
parent ab77025516
commit a5b4024eb6
  1. 25
      stringutil.moon
  2. 66
      tableutil.moon
  3. 90
      util.moon

@ -0,0 +1,25 @@
import match, gmatch, sub from string
import upper, lower from string
GLOB_PATT='^([^%%]*)%%([^%%]*)$'
patsubst = (str, pattern, replacement) ->
return [patsubst s, pattern, replacement for s in *str] if (type str)=='table'
prefix, suffix = match pattern, GLOB_PATT
return str unless prefix
reprefix, resuffix = match replacement, GLOB_PATT
return replacement unless reprefix
if (sub str, 1, #prefix)==prefix and (sub str, -#suffix)==suffix
return reprefix..(sub str, #prefix+1, -#suffix-1)..resuffix
str
splitsp = (str) ->
[elem for elem in gmatch str, '%S+']
{
:patsubst
:splitsp
:upper, :lower
}

@ -0,0 +1,66 @@
import insert, remove, concat, sort from table
unpack or= table.unpack
sortedpairs = (table, cmp) ->
keys = [k for k in pairs table]
sort keys, cmp
coroutine.wrap ->
for key in *keys
coroutine.yield key, table[key]
min = (table, cmp=(a, b) -> a<b) ->
val = table[1]
for i=2, #table
elem = table[i]
if cmp val, elem
val = elem
val
max = (table, cmp=(a, b) -> a<b) ->
val = table[1]
for i=2, #table
elem = table[i]
if not cmp val, elem
val = elem
val
foreach = (tab, fn) ->
[fn e for e in *tab]
first = (tab, fn) ->
for e in *tab
return e if fn e
exclude = (tab, ...) ->
i=1
while i<=#tab
removed=false
for j=1, select '#', ...
if tab[i]==select j, ...
remove tab, i
removed = true
break
i += 1 unless removed
tab
flatten = (tab) ->
return {tab} if (type tab)!='table'
out = {}
for e in *tab
if (type e)=='table'
insert out, v for v in *flatten e
else
insert out, e
out
{
:min, :max
:foreach
:first
:exclude
:flatten
:sortedpairs
:insert, :remove, :concat, :sort
:unpack
}

@ -1,54 +1,12 @@
import wildcard, exists, isdir, mtime from require 'fsutil' import wildcard, exists, isdir, mtime from require 'fsutil'
import foreach, first, flatten, exclude, sortedpairs, min, max from require 'tableutil'
import patsubst, splitsp from require 'stringutil'
import insert, concat, sort from table import insert, concat, sort, pairs from require 'tableutil'
unpack or=table.unpack import upper, lower from require 'stringutil'
GLOB_PATT='^([^%%]*)%%([^%%]*)$' GLOB_PATT='^([^%%]*)%%([^%%]*)$'
-- min and max of table
max= (t) ->
m=t[1]
for i=2, #t
v=t[i]
m=v if v>m
m
min= (t) ->
m=t[1]
for i=2, #t
v=t[i]
m=v if v<m
m
-- simpler constructs
foreach= (tab, fn) ->
[fn e for e in *tab]
first= (tab, fn) ->
for e in *tab
return e if fn e
exclude= (tab, ...) ->
i=1
while i<=#tab
removed=false
for j=1, select '#', ...
if tab[i]==select j, ...
table.remove tab, i
removed=true
break
i+=1 unless removed
tab
flatten= (tab) ->
return {tab} if (type tab)!='table'
out={}
for e in *tab
if (type e)=='table'
insert out, v for v in *flatten e
else
insert out, e
out
-- command functions -- command functions
escapecmdpart= (p) -> escapecmdpart= (p) ->
if (type p)=='table' if (type p)=='table'
@ -84,21 +42,6 @@ findclib= (name, mode='all') ->
insert args, '--libs' if mode=='all' or mode=='ld' insert args, '--libs' if mode=='all' or mode=='ld'
[arg for arg in (popen 'pkg-config', args)\read('*a')\gmatch '%S+'] [arg for arg in (popen 'pkg-config', args)\read('*a')\gmatch '%S+']
-- string pattern
patsubst= (str, pattern, replacement) ->
return [patsubst s, pattern, replacement for s in *str] if (type str)=='table'
prefix, suffix=pattern\match GLOB_PATT
return str unless prefix
reprefix, resuffix=replacement\match GLOB_PATT
return replacement unless reprefix
if (str\sub 1, #prefix)==prefix and (str\sub -#suffix)==suffix
return reprefix..(str\sub #prefix+1, -#suffix-1)..resuffix
str
splitsp= (str) ->
[elem for elem in str\gmatch '%S+']
-- glob match -- glob match
match= (str, glob) -> match= (str, glob) ->
prefix, suffix=glob\match GLOB_PATT prefix, suffix=glob\match GLOB_PATT
@ -113,26 +56,22 @@ isglob= (glob) ->
else else
false false
-- getenv
env= (key, def) -> env= (key, def) ->
(os.getenv key) or def (os.getenv key) or def
sortedpairs= (table, cmp) ->
keys = [k for k in pairs table]
sort keys, cmp
coroutine.wrap ->
for key in *keys
coroutine.yield key, table[key]
{ {
-- table functions -- table function
:min, :max :min, :max
:foreach :foreach
:first :first
:insert, :unpack, :concat, :sort
:exclude :exclude
:flatten :flatten
:sortedpairs :sortedpairs
:insert, :remove, :concat, :sort
:unpack
-- file functions -- file functions
:wildcard :wildcard
:mtime :mtime
@ -143,7 +82,14 @@ sortedpairs= (table, cmp) ->
:calccdeps, :findclib :calccdeps, :findclib
-- string functions -- string functions
:patsubst, :match, :isglob :patsubst
:env
:splitsp :splitsp
:upper, :lower
-- glob functions
:match, :isglob
-- env functions
:env
} }

Loading…
Cancel
Save