utils.path
Path utils.
Definition of path used by this file:
'...toto.tutu....'
means 'toto.tutu'
'toto...tutu.tata..foo.bar'
means 'toto.tutu.tata.foo.bar'
split('a.2', 1)
-> ('a', 2)
(where 2 is returned as a number) split('a.b.2e3.c', 1)
-> ('a', 'b.2000.c')
utils.path
utils.path.clean(path) | Cleans a path. |
utils.path.concat(varargs) | Concatenates a sequence of path strings together. |
utils.path.find(t, path, force) | Retrieves the element in a sub-table corresponding to the path. |
utils.path.get(t, path) | Gets the value of a table field. |
utils.path.gsplit(path) | Enumerates path partitions in a for-loop generator, starting from the right. |
utils.path.segments(path) | Splits a path into segments. |
utils.path.set(t, path, value) | Sets a value in a tree-like table structure. |
utils.path.split(path, n) | Splits a path into two halves, can be used to get path root, tail etc. |
utils.path
utils.path.clean(path)
path
:
string containing the path to clean.
cleaned path as a string.
utils.path.concat(varargs)
varargs
:
list of strings to concatenate into a valid path
resulting path as a string
utils.path.find(t, path, force)
t
:
is the table to look into.
path
:
can be either a string (see segments) or an array where
path[1]
is the root and path[n]
is the leaf.
force
:
parameter allows to create intermediate tables as specified
by the path, if necessary.
returned values depend on force value:
if force is false (or nil), find returns the table if it finds one, or it returns nil followed by the subpath that points to non table value
if force is true, find overwrites or create tables as necessary so it always returns a table.
if force is 'noowr', find creates tables as necessary but does not
overwrite non-table values. So as with force=false
, it only returns a
table if possible, and nil followed by the path that points to the first
neither-table-nor-nil value otherwise.
config = {toto={titi={tutu = 5}}}
find(config, "toto.titi") -- will return the table titi
utils.path.get(t, path)
The field to get is indicated by a path relative to the table.
t
:
table where to set the value.
path
:
can be eiher a string (see split) or an array where path[1] is the root and path[n] is the leaf.
utils.path.gsplit(path)
gsplit "a.b.c"
will generate successively
("a.b.c", ""), ("a.b", "c"), ("a", "b.c"), ("", "a.b.c")
.
path
:
the path as a string
the for-loop iterator function
utils.path.segments(path)
path
:
string containing the path to split.
list of split path elements.
utils.path.set(t, path, value)
t
:
table where to set the value.
path
:
can be either a string (see #utils.path.split)
or an array where path[1] is the root and path[n] is the leaf.
value
:
the value to set.
utils.path.split(path, n)
n
segments in the first half if n>0
,
-n
segments in the second half if n<0
.
If there are less then n
segments, returns the path argument followed by
an empty path.
If there are less then -n
segments, returns an empty path followed by the
path argument.
Note that if a half is a single element and that this element can be converted into a number, it is returned as a number.
path
:
the path as a string
n
:
number defining how the path is splitted (see above description).
the two halves
local root, tail = split('a.b.c', 1)
->root contains 'a', tail contains 'b.c'