Module utils.path Path utils.

Definition of path used by this file:

Type 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.

Type utils.path

Field(s)

utils.path.clean(path)

Cleans a path.
Removes trailing/preceding/doubling '.'.

Parameter

Return value

cleaned path as a string.

utils.path.concat(varargs)

Concatenates a sequence of path strings together.

Parameter

Return value

resulting path as a string

utils.path.find(t, path, force)

Retrieves the element in a sub-table corresponding to the path.

Parameters

Return value

returned values depend on force value:

Usage:

config = {toto={titi={tutu = 5}}}
     find(config, "toto.titi") -- will return the table titi

utils.path.get(t, path)

Gets the value of a table field.
The field can be in a sub table.

The field to get is indicated by a path relative to the table.

Parameters

Return values

  1. value if the field is found.
  2. nil otherthise.

utils.path.gsplit(path)

Enumerates path partitions in a for-loop generator, starting from the right.
For instance, gsplit "a.b.c" will generate successively ("a.b.c", ""), ("a.b", "c"), ("a", "b.c"), ("", "a.b.c").

Parameter

Return value

the for-loop iterator function

utils.path.segments(path)

Splits a path into segments.
Each segment is delimited by '.' pattern.

Parameter

Return value

list of split path elements.

utils.path.set(t, path, value)

Sets a value in a tree-like table structure.
The value to set is indicated by the path relative to the table. This function creates the table structure to store the value, unless the value to set is nil. If the value to set is nil and the table structure already exists then the value is set to nil. If the value is not nil, then the table structure is always created/overwritten and the value set.

Parameters

utils.path.split(path, n)

Splits a path into two halves, can be used to get path root, tail etc.
The content of the two halves depends on 'n' param: there will be 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.

Parameters

Return value

the two halves

Usage:

local root, tail = split('a.b.c', 1)
 ->root contains 'a', tail contains 'b.c'