Difference between revisions of "LuaAPI/stringx"

From The Battle for Wesnoth Wiki
(Also document the integer indexing support)
 
(11 intermediate revisions by one other user not shown)
Line 4: Line 4:
  
 
In addition, it permits extracting characters from a string by indexing with an integer. Basically, <syntaxhighlight lang=lua inline>str[n]</syntaxhighlight> is a shorthand for <syntaxhighlight lang=lua inline>str:sub(n,n)</syntaxhighlight>. This means negative indices count from the end of the string, and invalid indices output an empty string.
 
In addition, it permits extracting characters from a string by indexing with an integer. Basically, <syntaxhighlight lang=lua inline>str[n]</syntaxhighlight> is a shorthand for <syntaxhighlight lang=lua inline>str:sub(n,n)</syntaxhighlight>. This means negative indices count from the end of the string, and invalid indices output an empty string.
 +
 +
== Functions ==
  
 
=== stringx.split ===
 
=== stringx.split ===
Line 16: Line 18:
 
* '''escape''': Specify a single character as the "escape" character. If a separator character is preceded by this character, then it will not be considered for splitting. The escape character is not removed from the result strings. This option is incompatible with the quote options. For example: <syntaxhighlight lang=lua inline>{escape = [[\]]}</syntaxhighlight>
 
* '''escape''': Specify a single character as the "escape" character. If a separator character is preceded by this character, then it will not be considered for splitting. The escape character is not removed from the result strings. This option is incompatible with the quote options. For example: <syntaxhighlight lang=lua inline>{escape = [[\]]}</syntaxhighlight>
 
* '''quote''': Specify one or more quote characters. Any separators within the matching instances of the quotes will not be considered for splitting. For example: <syntaxhighlight lang=lua inline>{quote = [['"]]}</syntaxhighlight>
 
* '''quote''': Specify one or more quote characters. Any separators within the matching instances of the quotes will not be considered for splitting. For example: <syntaxhighlight lang=lua inline>{quote = [['"]]}</syntaxhighlight>
* '''quote_left''', '''quote_right''': As above, but you can separately specify opening and closing versions of each quote. The first left quote will match the first right quote, the second left quote will match the second right quote, and so on. For example: <syntaxhighlight lang=lua inline>{quote_left = [=[([]=], quote_right = [=[)]]=]</syntaxhighlight>
+
* '''quote_left''', '''quote_right''': As above, but you can separately specify opening and closing versions of each quote. The first left quote will match the first right quote, the second left quote will match the second right quote, and so on. For example: <syntaxhighlight lang=lua inline>{quote_left = '([', quote_right = ')]'</syntaxhighlight>
 
* '''expand_anim''': If true, expand the progressive string syntax of [[AnimationWML]]. This option is compatible with the quote options, but since the progressive string syntax treats square brackets as special characters, they may not be specified as quote characters with this option.
 
* '''expand_anim''': If true, expand the progressive string syntax of [[AnimationWML]]. This option is compatible with the quote options, but since the progressive string syntax treats square brackets as special characters, they may not be specified as quote characters with this option.
  
Line 23: Line 25:
 
=== stringx.escaped_split ===
 
=== stringx.escaped_split ===
  
* '''stringx.escaped_split'''(''str'', ''separator'', ''escape'') &rarr; ''list of strings''
+
* '''stringx.escaped_split'''(''str'', [''separator'', [''escape'']]) &rarr; ''list of strings''
* ''str'':'''escaped_split'''(''separator'', ''escape'') &rarr; ''list of strings''
+
* ''str'':'''escaped_split'''([''separator'', [''escape'']]) &rarr; ''list of strings''
  
A convenient shorthand for specifying the '''escape''' option in '''stringx.split'''.
+
A convenient shorthand for specifying the '''escape''' option in '''stringx.split'''. The separator and escape default to <tt>,</tt> and <tt>\</tt> respectively.
  
 
=== stringx.quoted_split ===
 
=== stringx.quoted_split ===
  
* '''stringx.quoted_split'''(''str'', ''separator'', ''quotes'' [, ''quotes'']) &rarr; ''list of strings''
+
* '''stringx.quoted_split'''(''str'', [''separator'', [''quotes'', [''quotes'']]]) &rarr; ''list of strings''
* ''str'':'''quoted_split'''(''separator'', ''quotes'' [, ''quotes'']) &rarr; ''list of strings''
+
* ''str'':'''quoted_split'''([''separator'', [''quotes'', [''quotes'']]]) &rarr; ''list of strings''
  
A convenient shorthand for specifying quote options in '''stringx.split'''. If two quote parameters are passed, they are in the order left, right.
+
A convenient shorthand for specifying quote options in '''stringx.split'''. If two quote parameters are passed, they are in the order left, right. The separator defaults to <tt>,</tt> and the quotes default to <tt>(</tt> and <tt>)</tt>.
  
 
=== stringx.anim_split ===
 
=== stringx.anim_split ===
  
* '''stringx.anim_split'''(''str'', ''separator'') &rarr; ''list of strings''
+
* '''stringx.anim_split'''(''str'', [''separator'']) &rarr; ''list of strings''
* ''str'':'''anim_split'''(''separator'') &rarr; ''list of strings''
+
* ''str'':'''anim_split'''([''separator'']) &rarr; ''list of strings''
  
A convenient shorthand for specifying the '''expand_anim''' option in '''stringx.split'''.
+
A convenient shorthand for specifying the '''expand_anim''' option in '''stringx.split'''. The separator defaults to <tt>,</tt>.
  
 
=== stringx.parenthetical_split ===
 
=== stringx.parenthetical_split ===
Line 47: Line 49:
 
* ''str'':'''parenthetical_split'''(''left_parens'', ''right_parens'') &rarr; ''list of strings''
 
* ''str'':'''parenthetical_split'''(''left_parens'', ''right_parens'') &rarr; ''list of strings''
  
Splits a string into parenthesized portions and portions between the parenthesized portions. With multiple parenthesis types, only the toplevel type at a given point participates in the split.
+
Splits a string into parenthesized portions and portions between the parenthesized portions. With multiple parenthesis types, only the toplevel type at a given point participates in the split. The parentheses default to <tt>(</tt> and <tt>)</tt> respectively.
  
 
<!-- TODO: Demonstrate by example -->
 
<!-- TODO: Demonstrate by example -->
Line 53: Line 55:
 
=== stringx.map_split ===
 
=== stringx.map_split ===
  
* '''stringx.map_split'''(''str'', ''item separator'', ''key-value separator'' [, ''options'']) &rarr; ''string map''
+
* '''stringx.map_split'''(''str'', [''item separator'', [''key-value separator'', [''options'']]) &rarr; ''string map''
* ''str'':'''map_split'''(''item separator'', ''key-value separator'' [, ''options'']) &rarr; ''string map''
+
* ''str'':'''map_split'''([''item separator'', [''key-value separator'', [''options'']]) &rarr; ''string map''
  
Splits a string into key-value pairs based on the two separators, which currently must each be a single character. The options table supports the following keys:
+
Splits a string into key-value pairs based on the two separators, which currently must each be a single character. The item separator defaults to <tt>,</tt> and the key-value separator defaults to <tt>:</tt>. The options table supports the following keys:
  
 
* '''remove_empty''', '''strip_spaces''': Same as in [[#stringx.split|stringx.split]] above.
 
* '''remove_empty''', '''strip_spaces''': Same as in [[#stringx.split|stringx.split]] above.
Line 65: Line 67:
 
=== stringx.join ===
 
=== stringx.join ===
  
* '''stringx.join'''(''list of strings'', ''separator'') &rarr; ''string''
+
* '''stringx.join'''(''list of strings'', [''separator'']) &rarr; ''string''
 
* ''separator'':'''join'''(''list of strings'') &rarr; ''string''
 
* ''separator'':'''join'''(''list of strings'') &rarr; ''string''
  
Joins a list of strings into a single string using the specified separator string. Accepts its arguments in either order.
+
Joins a list of strings into a single string using the specified separator string. Accepts its arguments in either order. If omitted, the separator defaults to <tt>,</tt>.
  
 
=== stringx.join_map ===
 
=== stringx.join_map ===
  
* '''stringx.join_map'''(''string map'', ''item separator'', ''key-value separator'') &rarr; ''string''
+
* '''stringx.join_map'''(''string map'', [''item separator'', [''key-value separator'']]) &rarr; ''string''
* ''item_separator'':'''join_map'''(''string map'', ''key-value separator'') &rarr; ''string''
+
* ''item_separator'':'''join_map'''(''string map'', [''key-value separator'']) &rarr; ''string''
 +
 
 +
Joins a map of strings into a single string using the specified separators. Keys will be output in lexicographical order. Accepts its arguments in any order, with the caveat that the ''item separator'' must appear before the ''key-value separator''. If omitted, the item separator defaults to <tt>,</tt> and the key-value separator defaults to <tt>:</tt>.
 +
 
 +
=== stringx.vformat ===
 +
 
 +
* '''stringx.vformat'''(''format_string'', ''values'') &rarr; ''formatted string''
 +
* ''string'':'''vformat'''(''values'') &rarr; ''formatted string''
 +
 
 +
Similar to '''string.format''' but using Wesnoth's variable substitution syntax, and also supporting translatable strings. Any valid WML table can be passed as the values, though typically you will only need key-value pairs. You could also substitute WML variables into a string by passing '''wml.all_variables''' as the second parameter.
 +
 
 +
This function preserves the translatability of the input format string.
 +
 
 +
=== stringx.format_conjunct_list ===
 +
 
 +
* '''stringx.format_conjunct_list'''(''empty'', ''list_of_strings'') &rarr; ''formatted string''
 +
 
 +
Formats a list of usually-translatable strings into a localized list string of the form "A, B, and C". If the list contains no elements, returns ''empty''.
 +
 
 +
=== stringx.format_disjunct_list ===
 +
 
 +
* '''stringx.format_disjunct_list'''(''empty'', ''list_of_strings'') &rarr; ''formatted string''
 +
 
 +
Formats a list of usually-translatable strings into a localized list string of the form "A, B, or C". If the list contains no elements, returns ''empty''.
 +
 
 +
=== stringx.trim ===
 +
 
 +
* '''stringx.trim'''(''str'') &rarr; ''trimmed string''
 +
* ''str'':'''trim'''() &rarr; ''trimmed string''
 +
 
 +
Strips out any leading and trailing whitespace from the string and returns the resulting string.
 +
 
 +
=== stringx.parse_range ===
 +
 
 +
* '''stringx.parse_range'''(''range string'') &rarr; ''start'', ''end''
 +
* ''range'':'''parse_range'''() &rarr; ''start'', ''end''
 +
 
 +
Parses a range of the form <syntaxhighlight lang=lua inline>"1-5"</syntaxhighlight> and returns the endpoints of the range as two separate values.
 +
 
 +
=== stringx.iter_range ===
 +
 
 +
* '''stringx.iter_range'''(''range string'') &rarr; ''iterator'' &rArr; ''integer''
 +
* ''range'':'''iter_range'''() &rarr; ''iterator'' &rArr; ''integer''
 +
 
 +
Iterate over a range of the form <syntaxhighlight lang=lua inline>"1-5"</syntaxhighlight>.
 +
 
 +
<syntaxhighlight lang=lua>
 +
for i in stringx.iter_range "1-3" do
 +
  print(i)
 +
end
 +
-- Output:
 +
-- 1
 +
-- 2
 +
-- 3
 +
</syntaxhighlight>
 +
 
 +
=== stringx.iter_ranges ===
 +
 
 +
* '''stringx.iter_ranges'''(''range strings'') &rarr; ''iterator'' &rArr; ''integer''
 +
* ''ranges'':'''iter_ranges'''() &rarr; ''iterator'' &rArr; ''integer''
 +
 
 +
Iterates over a sequence of comma-separated ranges. If the ranges overlap, numbers will be yielded more than once.
 +
 
 +
<syntaxhighlight lang=lua>
 +
for i in stringx.iter_ranges "1-3,7-9" do
 +
  print(i)
 +
end
 +
-- Output:
 +
-- 1
 +
-- 2
 +
-- 3
 +
-- 7
 +
-- 8
 +
-- 9
 +
for i in stringx.iter_ranges "1-3,3-6" do
 +
  print(i)
 +
end
 +
-- Output:
 +
-- 1
 +
-- 2
 +
-- 3
 +
-- 3
 +
-- 4
 +
-- 5
 +
-- 6
 +
</syntaxhighlight>
  
Joins a map of strings into a single string using the specified separators. Keys will be output in lexicographical order. Accepts its arguments in any order, with the caveat that the ''item separator'' must appear before the ''key-value separator''.
+
[[Category:Lua Reference]]

Latest revision as of 18:04, 18 April 2024

(Version 1.15.3 and later only)

The stringx module contains additional string support functions. It also registers itself as the string metatable so that these functions can be called directly on strings.

In addition, it permits extracting characters from a string by indexing with an integer. Basically, str[n] is a shorthand for str:sub(n,n). This means negative indices count from the end of the string, and invalid indices output an empty string.

Functions

stringx.split

  • stringx.split(str, [separator] , [options]) → list of strings
  • str:split([separator] , [options]) → list of strings

Splits a string on a separator character, which currently must be a single character and defaults to ','. The optional options table controls how the string is split (beyond the basic option of the separator character). It can contain the following keys:

  • remove_empty: If true, empty strings are omitted from the result. If the options table is omitted, this will default to true; otherwise, it will default to false.
  • strip_spaces: Spaces surrounding the separator character are stripped. If the options table is omitted, this will default to true; otherwise, it will default to false.
  • escape: Specify a single character as the "escape" character. If a separator character is preceded by this character, then it will not be considered for splitting. The escape character is not removed from the result strings. This option is incompatible with the quote options. For example: {escape = [[\]]}
  • quote: Specify one or more quote characters. Any separators within the matching instances of the quotes will not be considered for splitting. For example: {quote = [['"]]}
  • quote_left, quote_right: As above, but you can separately specify opening and closing versions of each quote. The first left quote will match the first right quote, the second left quote will match the second right quote, and so on. For example: {quote_left = '([', quote_right = ')]'
  • expand_anim: If true, expand the progressive string syntax of AnimationWML. This option is compatible with the quote options, but since the progressive string syntax treats square brackets as special characters, they may not be specified as quote characters with this option.


stringx.escaped_split

  • stringx.escaped_split(str, [separator, [escape]]) → list of strings
  • str:escaped_split([separator, [escape]]) → list of strings

A convenient shorthand for specifying the escape option in stringx.split. The separator and escape default to , and \ respectively.

stringx.quoted_split

  • stringx.quoted_split(str, [separator, [quotes, [quotes]]]) → list of strings
  • str:quoted_split([separator, [quotes, [quotes]]]) → list of strings

A convenient shorthand for specifying quote options in stringx.split. If two quote parameters are passed, they are in the order left, right. The separator defaults to , and the quotes default to ( and ).

stringx.anim_split

  • stringx.anim_split(str, [separator]) → list of strings
  • str:anim_split([separator]) → list of strings

A convenient shorthand for specifying the expand_anim option in stringx.split. The separator defaults to ,.

stringx.parenthetical_split

  • stringx.parenthetical_split(str, left_parens, right_parens) → list of strings
  • str:parenthetical_split(left_parens, right_parens) → list of strings

Splits a string into parenthesized portions and portions between the parenthesized portions. With multiple parenthesis types, only the toplevel type at a given point participates in the split. The parentheses default to ( and ) respectively.


stringx.map_split

  • stringx.map_split(str, [item separator, [key-value separator, [options]]) → string map
  • str:map_split([item separator, [key-value separator, [options]]) → string map

Splits a string into key-value pairs based on the two separators, which currently must each be a single character. The item separator defaults to , and the key-value separator defaults to :. The options table supports the following keys:

  • remove_empty, strip_spaces: Same as in stringx.split above.
  • default: Used as the value if any item is missing the key_value_separator.


stringx.join

  • stringx.join(list of strings, [separator]) → string
  • separator:join(list of strings) → string

Joins a list of strings into a single string using the specified separator string. Accepts its arguments in either order. If omitted, the separator defaults to ,.

stringx.join_map

  • stringx.join_map(string map, [item separator, [key-value separator]]) → string
  • item_separator:join_map(string map, [key-value separator]) → string

Joins a map of strings into a single string using the specified separators. Keys will be output in lexicographical order. Accepts its arguments in any order, with the caveat that the item separator must appear before the key-value separator. If omitted, the item separator defaults to , and the key-value separator defaults to :.

stringx.vformat

  • stringx.vformat(format_string, values) → formatted string
  • string:vformat(values) → formatted string

Similar to string.format but using Wesnoth's variable substitution syntax, and also supporting translatable strings. Any valid WML table can be passed as the values, though typically you will only need key-value pairs. You could also substitute WML variables into a string by passing wml.all_variables as the second parameter.

This function preserves the translatability of the input format string.

stringx.format_conjunct_list

  • stringx.format_conjunct_list(empty, list_of_strings) → formatted string

Formats a list of usually-translatable strings into a localized list string of the form "A, B, and C". If the list contains no elements, returns empty.

stringx.format_disjunct_list

  • stringx.format_disjunct_list(empty, list_of_strings) → formatted string

Formats a list of usually-translatable strings into a localized list string of the form "A, B, or C". If the list contains no elements, returns empty.

stringx.trim

  • stringx.trim(str) → trimmed string
  • str:trim() → trimmed string

Strips out any leading and trailing whitespace from the string and returns the resulting string.

stringx.parse_range

  • stringx.parse_range(range string) → start, end
  • range:parse_range() → start, end

Parses a range of the form "1-5" and returns the endpoints of the range as two separate values.

stringx.iter_range

  • stringx.iter_range(range string) → iteratorinteger
  • range:iter_range() → iteratorinteger

Iterate over a range of the form "1-5".

for i in stringx.iter_range "1-3" do
  print(i)
end
-- Output:
-- 1
-- 2
-- 3

stringx.iter_ranges

  • stringx.iter_ranges(range strings) → iteratorinteger
  • ranges:iter_ranges() → iteratorinteger

Iterates over a sequence of comma-separated ranges. If the ranges overlap, numbers will be yielded more than once.

for i in stringx.iter_ranges "1-3,7-9" do
  print(i)
end
-- Output:
-- 1
-- 2
-- 3
-- 7
-- 8
-- 9
for i in stringx.iter_ranges "1-3,3-6" do
  print(i)
end
-- Output:
-- 1
-- 2
-- 3
-- 3
-- 4
-- 5
-- 6
This page was last edited on 18 April 2024, at 18:04.