File SMStrings.mys
Essential string utility functions
SMStrings.mys is automatically included when you Include "SMUtils".
- Sylvain Machefert
Sections
| Section | Summary |
|---|---|
| Durations | Clock and durations utility functions
|
| Files | Files utility functions |
| Fonts | Font utility functions. |
| Internet | Internet functions. |
| Strings | strings utility functions |
| Tables | tables utility functions. |
Summary
| Return type | Function and summary |
|---|---|
| string | CommonLeftSubsequence(...) Return the left subsequence that starts all strings in argument. |
| string, string, int, int, int | CompareStrings(string str1, string str2, boolean style) Compare two strings, returns a combination of both with +/- and green/red styling to identify removed and added chars. |
| string | EscapePercent(string str) Escape a string that contain a % that can be interpreted in Lua string functions like gsub. |
| boolean | IsNumeric(string str) Is str representing a number? |
| boolean | IsStyledString(string str) Test if a string is a StyledString |
| RemoveDiacritics(string s, boolean specialChars) Remove diacritics (accents) from the string s. | |
| string, string | SplitPath(string path) Split a path into directory and file, no file if path ends by a '/'. |
| table | SplitUrl(string url) Split an URL into protocol, host, path, query and anchor. |
| string | escape_lines(string s) Escape lines \n and \r as well as tabs \t and backslash \, using backslashes \. |
| string | excel_column_name(int n) Returns the Excel column name of the n-th column. |
| string | fromBytes(table bytes) Convert a table of bytes (integers) into a string. |
| string | gsub_args(string str, ...) Replace %1, %2 in str by first and second following arguments. |
| table | strfind_all(string s, string pattern, boolean plain, boolean allowOverlap) Looks for all matches of pattern in string s. |
| int, int, string | strfind_last(string s, string pattern, boolean plain) Looks for the last match of pattern in string s. |
| RawTable | string2RawTable(string s) Convert a string into a |
| string | string_between(string str, string left, string right) Return the portion of str found between left and right. |
| string | string_concat(string src, string delimiter, ...) Concatenate toAdd after src with a delimiter, handles nil and empty string initialization. |
| boolean | string_ends_with(string str, string rightStr) Does str ends with rightStr? |
| string | string_first(string a, string b) Returns the first of a and b in alphabetical order. |
| string | string_join(table list, string delimiter) Concat the elements of table list, separated by the string delimiter. |
| string | string_last(string a, string b) Returns the last of a and b in alphabetical order. |
| string | string_left(string str, int n) Return the n leftmost chars of the string str. |
| string | string_line_to_space(string str) Replace line \r and \n characters by a space. |
| string | string_right(string str, int n) Return the n rightmost chars of the string str. |
| table | string_slice(string s, int sze, boolean fromEnd) Slice the string into blocks of size, except last one that may be shorter. |
| table | string_split(string s, string delim, int limit) Split the string s by a string delimiter delim. |
| table | string_split_lines(string s) Split the string s by lines, handle \r, \n and \r\n. |
| boolean | string_starts_with(string str, string leftStr) Does str starts with leftStr? |
| string | string_time(number seconds, boolean exact, boolean clockFormat) Converts a time (music duration, debug chrono...) into readable string. |
| string | string_trim(string s, string toTrim, string side) Removes toTrim at the beginning and the end of s. |
| table | toBytes(string s) Convert a string into a table of bytes (integers). |
| string | unescape_lines(string s) Unescape lines \n and \r as well as tabs \t and backslash \, escaped by backslashes. |
Functions
IsStyledString(string str)
Test if a string is a StyledString
| Parameter | Type | Default | Description |
|---|---|---|---|
| str | string | str |
- Return
- boolean:
trueornil(for false).
IsNumeric(string str)
Is str representing a number?
| Parameter | Type | Default | Description |
|---|---|---|---|
| str | string | The string to test |
- Return
- boolean:
trueornil(for false) - Example
IsNumeric("-22.5") --> true IsNumeric("-.5") --> true IsNumeric(".12") --> true IsNumeric("0.12") --> true IsNumeric("1.") --> false IsNumeric("123abc") --> false
EscapePercent(string str)
Escape a string that contain a % that can be interpreted in Lua string functions like gsub.
| Parameter | Type | Default | Description |
|---|---|---|---|
| str | string | str |
- Return
- string: % are escaped by a %.
string_split(string s, string delim, int limit)
Split the string s by a string delimiter delim.
| Parameter | Type | Default | Description |
|---|---|---|---|
| s | string | The string to split | |
| delim | string | The delimiter | |
| limit | int | 0 | Maximum number of slices of string, values <= 1 are ignored.. |
- Return
- table: An array of at least one item (s if delim is not found)
- Example
local t = string_split("Hello World!", " ") print(t[1]) --> Hello print(t[2]) --> World!
string_split_lines(string s)
Split the string s by lines, handle \r, \n and \r\n.
| Parameter | Type | Default | Description |
|---|---|---|---|
| s | string | The string to split |
- Return
- table: A table of at least one element (s if line separator is not found)
string_slice(string s, int sze, boolean fromEnd)
Slice the string into blocks of size, except last one that may be shorter.
| Parameter | Type | Default | Description |
|---|---|---|---|
| s | string | The string to slice | |
| sze | int | Size of each slice | |
| fromEnd | boolean | (defaul=false) Slice from the end of the string, it means that, when there are multiple slices, if fromEnd == true, first slice's length may be smaller than size, if fromEnd == false last slice's length may be smaller than size. |
- Return
- table: A table of slices of s
- Error
- if s is not a string, or size is not a positive number
string_join(table list, string delimiter)
Concat the elements of table list, separated by the string delimiter.
| Parameter | Type | Default | Description |
|---|---|---|---|
| list | table | list | |
| delimiter | string | "" |
- Return
- string: string
- Example
string_join({"Anna", "Bob", "Charlie", "Dolores"}, ", ") --> Anna, Bob, Charlie, Dolores
string_concat(string src, string delimiter, ...)
Concatenate toAdd after src with a delimiter, handles nil and empty string initialization.
- s is not concatenated if empty,
- delimiter is not inserted if src is
nilor empty.
myList = string_concat(myList, ", ", "an item") is a a shortcut for myList = (myList and (myList..", ") or "") .. "an item" with more nil handling.| Parameter | Type | Default | Description |
|---|---|---|---|
| src | string | src | |
| delimiter | string | "" | |
| ... | List of string or number. |
- Return
- string: string
- Error
- if s is
nil
string_trim(string s, string toTrim, string side)
Removes toTrim at the beginning and the end of s.
| Parameter | Type | Default | Description |
|---|---|---|---|
| s | string | If nil, returns "" | |
| toTrim | string | " \t\r\n" | List of chars to remove, if nil, then trim spaces, tabs, lines. |
| side | string | "both" | Side of the trim, "left", "right" or anything else for both sides. |
- Return
- string: The trimmed string
- Example
string_trim(" abcd\t")) --> "abcd" string_trim(" abcd\t", nil, "left") --> "abcd\t" string_trim(" abcd\t", "a \t") --> "bcd" string_trim(" abcd\t", "()") --> " abcd\t"
string_left(string str, int n)
Return the n leftmost chars of the string str.
| Parameter | Type | Default | Description |
|---|---|---|---|
| str | string | str | |
| n | int | n |
- Return
- string: A string of
strlen(str)to n chars:strsub(str, 1, n) - Error
- if str is not a string or
n <= 0
string_right(string str, int n)
Return the n rightmost chars of the string str.
| Parameter | Type | Default | Description |
|---|---|---|---|
| str | string | str | |
| n | int | n |
- Return
- string: A string of
strlen(str)to n chars:strsub(str, -n) - Error
- if str is not a string or
n <= 0
string_starts_with(string str, string leftStr)
Does str starts with leftStr?
| Parameter | Type | Default | Description |
|---|---|---|---|
| str | string | str | |
| leftStr | string | leftStr |
- Return
- boolean:
trueifstring_left(str, strlen(leftStr)) == leftStr,nilfor false. - Error
- if str is not a string, or
strlen(leftStr) == 0
string_ends_with(string str, string rightStr)
Does str ends with rightStr?
| Parameter | Type | Default | Description |
|---|---|---|---|
| str | string | str | |
| rightStr | string | rightStr |
- Return
- boolean:
trueifstring_right(str, strlen(rightStr)) == rightStr,nilfor false. - Error
- if str is not a string, or
strlen(rightStr) == 0
string_between(string str, string left, string right)
Return the portion of str found between left and right.
| Parameter | Type | Default | Description |
|---|---|---|---|
| str | string | str | |
| left | string | left | |
| right | string | right |
- Return
- string:
nilif left or right are not found in str.
string_line_to_space(string str)
Replace line \r and \n characters by a space.
| Parameter | Type | Default | Description |
|---|---|---|---|
| str | string | str |
- Return
- string: string
excel_column_name(int n)
Returns the Excel column name of the n-th column.
"A" for 1, "B" for 2, "Z" for 26, "AA" for 27...
| Parameter | Type | Default | Description |
|---|---|---|---|
| n | int | n |
- Return
- string: string
- Example
excel_column_name(123) --> "DS"
string_time(number seconds, boolean exact, boolean clockFormat)
Converts a time (music duration, debug chrono...) into readable string.
| Parameter | Type | Default | Description |
|---|---|---|---|
| seconds | number | Time in seconds, 0.010 = 10ms | |
| exact | boolean | false | If false, removes details (e.g. no milliseconds if time is minutes and you don't need so much precision). |
| clockFormat | boolean | false | Clock format: returns 00:03.010 instead of 3s 10ms |
- Return
- string: "10 ms", "3 s 456 ms", "1 min 14 s", 00:03.010...
- See
- Stopwatch:new
- Example
s = 1.0102345 -- 1 second, 10,2345 ms print(string_time(s)) --> "1.01 s" print(string_time(s, true)) --> "1 s 10.234 ms" print(string_time(s, false, true)) --> "00:01.010" print(string_time(s, true, true)) --> "00:01.010234" s = s + 2*60 -- add 2 minutes print(string_time(s)) --> "2 min 1 s" print(string_time(s, true)) --> "2 min 1 s 10.234 ms" print(string_time(s, false, true)) --> "02:01.010" print(string_time(s, true, true)) --> "02:01.010234"
string_first(string a, string b)
Returns the first of a and b in alphabetical order.
Comparable to max(a,b) for numbers.
| Parameter | Type | Default | Description |
|---|---|---|---|
| a | string | a | |
| b | string | b |
- Return
- string: string
- Example
print(string_first("Hello", "Harmony")) --> "Harmony"
string_last(string a, string b)
Returns the last of a and b in alphabetical order.
Comparable to max(a,b) for numbers.
| Parameter | Type | Default | Description |
|---|---|---|---|
| a | string | a | |
| b | string | b |
- Return
- string: string
- Example
print(string_last("Harmony", "Assistant")) --> "Harmony"
SplitUrl(string url)
Split an URL into protocol, host, path, query and anchor.
| Parameter | Type | Default | Description |
|---|---|---|---|
| url | string | url |
- Return
- table: Table t with following structure for https://www.myriad-online.com/cgi-bin/bbs/YaBB.pl?board=MYRSCRIPT;action=display;num=1652718688#2
t.Protocol = "https" t.Host = "www.myriad-online.com" t.Domain = "myriad-online.com" t.TLD = "com" -- top level domain, secondary "co.uk", "gouv.fr" are ignored t.Path = "cgi-bin/bbs/YaBB.pl" t.File = "YaBB.pl" t.FileExtension = "pl" t.Query = "board=MYRSCRIPT;action=display;num=1652718688" t.Anchor = "2"
Some of these field may be empty string ornil - Error
- if url is
nilor empty string
SplitPath(string path)
Split a path into directory and file, no file if path ends by a '/'.
| Parameter | Type | Default | Description |
|---|---|---|---|
| path | string | path |
- Returns
- string: directory
- string: file name
RemoveDiacritics(string s, boolean specialChars)
Remove diacritics (accents) from the string s.
| Parameter | Type | Default | Description |
|---|---|---|---|
| s | string | s | |
| specialChars | boolean | false | If true, special white spaces, quotes, dashes, punctuations... are replaced by their ASCII equivalent (space, ", ', -...) |
- Return
- The string without diacritics, e.g. 'รก' is replaced by 'a'.
- Error
- if s is not a string
- See
- https://en.wikipedia.org/wiki/Diacritic
escape_lines(string s)
Escape lines \n and \r as well as tabs \t and backslash \, using backslashes \.
| Parameter | Type | Default | Description |
|---|---|---|---|
| s | string | A string that may contain CR and/or LF |
- Return
- string: escaped string.
- See
- unescape_lines
- Example
s = "string\twith\nnew line" print(s) --> string with --> new line print(escape_lines(s)) --> string\twith\nnew line
unescape_lines(string s)
Unescape lines \n and \r as well as tabs \t and backslash \, escaped by backslashes.
| Parameter | Type | Default | Description |
|---|---|---|---|
| s | string | A escaped string |
- Return
- string: unescaped string ready to be printed.
- See
- escape_lines
- Example
s = "string\\twith\\nnew line" print(s) --> string\twith\nnew line print(unescape_lines(s)) --> string with --> new line
gsub_args(string str, ...)
Replace %1, %2 in str by first and second following arguments.
This is a convenient way to write the following code:
return gsub(gsub(str, "%%1", "1st value"), "%%2", "2nd value")
gsub return 2 value, the string and number of substitutions. Strangely, nested gsub accept the second returned value, but if you write gsub(str, 1, "%%2", "2nd value") you get an error.
But the second returned value may disturb your functions!
This variant return only the modified string, it may save you debugging headaches ;)
| Parameter | Type | Default | Description |
|---|---|---|---|
| str | string | The string containing %1, %2... to be replaced | |
| ... | Undefined number of arguments that must be string, number or nil. |
- Return
- string: The replaced string
- Error
- If one argument has unsupported type.
- Example
local s = "Hello %1, I want %2." print(gsub_args(s, "Bob", "bananas")) --> Hello Bob, I want bananas.
CompareStrings(string str1, string str2, boolean style)
Compare two strings, returns a combination of both with +/- and green/red styling to identify removed and added chars.
| Parameter | Type | Default | Description |
|---|---|---|---|
| str1 | string | First (oldest) string | |
| str2 | string | Second (newest) string | |
| style | boolean | false | Add green/red colors to added/removed chars in result? As discussed in SetStringStyle(...), the font is set to Geneva. |
- Returns
- string: Combination of both string, including removed and added chars. To determinate which chars has been modified, you need style to
true(for visual) or the second returned string (for parsing). - string: A string of same length describing which chars has been modified:
- Added chars are marked with a + (plus)
- Removed chars are marked with a - (minus)
- Common/unchanged chars ar marked with a space.
- int: Number of added chars
- int: Number of removed chars
- int: Number of unchanged chars
- See
- CompareTextFiles
- CompareTables
CommonLeftSubsequence(...)
Return the left subsequence that starts all strings in argument.
| Parameter | Type | Default | Description |
|---|---|---|---|
| ... | One table of string, or a list of strings. |
- Return
- string: The subsequence
- Example
print(CommonLeftSubsequence("abc", "abd")) --> "ab" print(CommonLeftSubsequence({"bac", "abc"})) --> ""
strfind_last(string s, string pattern, boolean plain)
Looks for the last match of pattern in string s.
If it finds one, then it returns the indices of s where this occurrence starts and ends; otherwise, it returns nil. If the pattern specifies regexp captures, the captured strings are returned as extra results. If optional plain argument is true, turns off the regexp pattern matching facilities, so the function does a plain find operation, with no characters in pattern being considered magic.
| Parameter | Type | Default | Description |
|---|---|---|---|
| s | string | s | |
| pattern | string | pattern | |
| plain | boolean | false |
- Returns
- int: start position of found substring, or
nilif no match. - int: end position of found substring
- string: Optional returned values if pattern contains captures.
strfind_all(string s, string pattern, boolean plain, boolean allowOverlap)
Looks for all matches of pattern in string s.
Each occurence produce an item in the returned table:
- indice of s where it starts;
- Indice of s where it ends;
- If the pattern specifies regexp captures, the captured strings are added as extra results.
true, turns off the regexp pattern matching facilities, so the function does a plain find operation, with no characters in pattern being considered magic.| Parameter | Type | Default | Description |
|---|---|---|---|
| s | string | s | |
| pattern | string | pattern | |
| plain | boolean | false | |
| allowOverlap | boolean | false | In plain search of a repetitive pattern, allow (or not) matches that overlap. e.g. s = "lalala"; pattern = "lala" true returns two matches: 1-4 and 3-6, false returns the first one. |
- Return
- table: An item is an occurence, which is a table of:
- int start position of occurence,
- int end position of occurence,
- Optional string first capture,
- Optional string second capture...
toBytes(string s)
Convert a string into a table of bytes (integers).
| Parameter | Type | Default | Description |
|---|---|---|---|
| s | string | s |
- Return
- table: table
- See
- string2RawTable that converts to
, which allow hash operations.
fromBytes(table bytes)
Convert a table of bytes (integers) into a string.
| Parameter | Type | Default | Description |
|---|---|---|---|
| bytes | table | bytes |
- Return
- string: string
string2RawTable(string s)
Convert a string into a
| Parameter | Type | Default | Description |
|---|---|---|---|
| s | string | s |