Module lua-nucleo.string

String-related tools

This file is a part of lua-nucleo library

Info:

  • Copyright: lua-nucleo authors (see file COPYRIGHT for the license)

Local Functions

split_by_char (str, delimiter) Split a string by char.
count_substrings (str, substr) Count the number of substring occurrences.
split_by_offset (str, offset) Split a string into two parts at offset.
fill_placeholders_ex (capture, str, dict) Expands variables in input string matched by capture string with values from dictionary.
fill_placeholders (str, dict) Expands variables like $(varname) with values from dictionary.
fill_curly_placeholders (str, dict) Expands variables like ${varname} with values from dictionary.
kv_concat (t, kv_glue, pair_glue, pairs_fn) Convert non-hierarchical table into string.
get_escaped_chars_in_ranges (ranges) Returns '%'-separated character string.
tjson_simple (t) Serialize table into json string.


Local Functions

split_by_char (str, delimiter)
Split a string by char.

Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the char delimiter.

Parameters:

Returns:

    table Returns an array of strings created by splitting the string parameter on boundaries formed by the delimiter
count_substrings (str, substr)
Count the number of substring occurrences.

Parameters:

  • str string The string to search in
  • substr string The substring to search for, must be not empty

Returns:

    number Returns the number of substring occurrences
split_by_offset (str, offset)
Split a string into two parts at offset.

Parameters:

  • str string Input string
  • offset number Offset at which string will be splitted

Returns:

    table Returns two strings, the first one - is to the left from offset and the second one to the right from offset
fill_placeholders_ex (capture, str, dict)
Expands variables in input string matched by capture string with values from dictionary.

Parameters:

  • capture string Variable matching expression
  • str string Input string, containing variables to expand
  • dict table Dictionary, containing variables's values

Returns:

    string A result string, where variables substituted with values

See also:

Usage:

    Universal value substitution to any placeholder, for example:
      fill_placeholders_ex("%$%((.-)%)", "a = $(a)", { a = 42 })
      returns "a = 42"
fill_placeholders (str, dict)
Expands variables like $(varname) with values from dictionary.

Parameters:

  • str string Input string, containing variables to expand
  • dict table Dictionary, containing variables's values

Returns:

    string A result string, where variables substituted with values

Usage:

    fill_placeholders("a = $(a)", { a = 42 })
       returns "a = 42"
fill_curly_placeholders (str, dict)
Expands variables like ${varname} with values from dictionary.

Parameters:

  • str string Input string, containing variables to expand
  • dict table Dictionary, containing variables's values

Returns:

    string A result string, where variables substituted with values

Usage:

    fill_placeholders("a = ${a}", { a = 42 })
       returns "a = 42"
kv_concat (t, kv_glue, pair_glue, pairs_fn)
Convert non-hierarchical table into string.

Values of key and value are concatted using custom glue kv_glue. Allowed values for key and value are numbers and strings. Pairs are concatted using custom glue pair_glue. Table can be traversed using custom iterator pairs_fn.

Parameters:

  • t table Non-hierarchical table with [key]=value pairs
  • kv_glue string Glue between key and value
  • pair_glue string Glue between pairs (defaut: "")
  • pairs_fn function Table iterator (default: pairs)

Returns:

    string A result string

Usage:

    kv_concat({a = 1, b = 2}, " => ", "; ", pairs)
get_escaped_chars_in_ranges (ranges)
Returns '%'-separated character string. separated) from char with ranges[1] code to char with ranges[2] code, concats it to same way to ranges[3] - ranges[4], and so on.

If range[i], range[i+1] are strings, ignore all string chars but first, and concats all chars ('%' separated) from ranges[1][1] to ranges[2][1], concats it to ranges[3][1] - ranges[4][1], and so on.

If range[i], range[i+1] are different types, also works fine, for example: get_escaped_chars_in_ranges({"0",50}) returns "%0%1%2".

Parameters:

  • ranges If range[i], range[i+1] are numbers, concats all chars ('%'

Returns:

    string Returns '%'-separated character string.
tjson_simple (t)
Serialize table into json string.

Tables with string keys only becomes objects. Tables with integer keys without gaps becomes arrays. Value types nil, NaN, +Inf, -Inf is not supported.

Parameters:

  • t table Table without self-references

Returns:

    string A result string

Usage:

    tjson_simple({a = 1, b = 2})
       returns '{"a":1,"b":2}'
generated by LDoc 1.4.6 Last updated 2021-04-22 13:57:02