Appearance
String
The String module provides various extensions on the default Lua string library.
Separated Strings
One common pattern is to store lists of elements as a string with a fixed separator. The String module provides two APIs to help facilitate this as demonstrated below.
lua
local MyModule = select(2, ...).MyModule
local String = MyModule:From("LibTSMUtil"):Include("Lua.String")
local SEP = ","
local favoriteClasses = strjoin(SEP, "Rogue", "Mage", "Warrior")
print(String.SeparatedCount(favoriteClasses, SEP)) -- 3
print(String.SeparatedContains(favoriteClasses, SEP, "Rogue")) -- true
print(String.SeparatedContains(favoriteClasses, SEP, "Hunter")) -- false
for class in String.SplitIterator(favoriteClasses, SEP) do
print(class)
end
-- Rogue
-- Mage
-- WarriorAPI
Inherits: LibTSMModule
Functions
Escape
lua
function Lua.String.Escape(str)
-> stringEscapes any magic characters used by lua's pattern matching.
Parameters:
| Name | Type | Description |
|---|---|---|
str | string | The string to be escaped |
Returns:
| Type | Description |
|---|---|
string |
FormatToMatchPattern
lua
function Lua.String.FormatToMatchPattern(str)
-> stringGenerates a pattern matching string from a format string.
Parameters:
| Name | Type | Description |
|---|---|---|
str | string | The format string |
Returns:
| Type | Description |
|---|---|
string |
SafeSplit
lua
function Lua.String.SafeSplit(str, sep, resultTbl)
-> string[]Splits a string in a way which won't cause stack overflows for large inputs. The lua strsplit function causes a stack overflow if passed large inputs. This API fixes that issue and also supports separators which are more than one character in length.
Parameters:
| Name | Type | Description |
|---|---|---|
str | string | The string to be split |
sep | string | The separator to use to split the string |
resultTbl | table | An optional table to store the result in |
Returns:
| Type | Description |
|---|---|
string[] |
SeparatedContains
lua
function Lua.String.SeparatedContains(str, sep, value)
-> booleanCheck if a string which contains multiple values separated by a specific string contains the value.
Parameters:
| Name | Type | Description |
|---|---|---|
str | string | The string to be searched |
sep | string | The separating string |
value | string | The value to search for |
Returns:
| Type | Description |
|---|---|
boolean |
SeparatedCount
lua
function Lua.String.SeparatedCount(str, sep)
-> numberGets the count of separated string parts.
Parameters:
| Name | Type | Description |
|---|---|---|
str | string | The string to count the parts in |
sep | string | The separating string |
Returns:
| Type | Description |
|---|---|
number |
SplitIterator
lua
function Lua.String.SplitIterator(str, sep)
-> fun():stringIterates over the parts of a string which are separated by a character.
Parameters:
| Name | Type | Description |
|---|---|---|
str | string | The string to be split |
sep | string | The separator to use to split the string |
Returns:
| Type | Description |
|---|---|
fun():string | Iterator with fields: part |
StrictMatch
lua
function Lua.String.StrictMatch(str, pattern, pos)
-> ...stringCalls strmatch() and enforces that there's a match.
Parameters:
| Name | Type | Description |
|---|---|---|
str | string | |
pattern | string | |
pos | number |
Returns:
| Type | Description |
|---|---|
...string |
StrictMatchNumber
lua
function Lua.String.StrictMatchNumber(str, pattern, pos)
-> numberCalls strmatch() and converts the result to a number, erroring if it's not a valid number.
Parameters:
| Name | Type | Description |
|---|---|---|
str | string | |
pattern | string | |
pos | number |
Returns:
| Type | Description |
|---|---|
number |