File SMStrings.mys

Essential string utility functions

SMStrings.mys is automatically included when you Include "SMUtils".

Author
Sylvain Machefert

Sections

SectionSummary
DurationsClock and durations utility functions
  • Clock: measure elapsed time, time formating,
  • Conversions from/to milliseconds, note duration, percentage of note duration, 256th of quarter
FilesFiles utility functions
FontsFont utility functions.
InternetInternet functions.
Stringsstrings utility functions
Tablestables utility functions.

Summary

Return typeFunction and summary
stringCommonLeftSubsequence(...)
Return the left subsequence that starts all strings in argument.
string, string, int, int, intCompareStrings(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.
stringEscapePercent(string str)
Escape a string that contain a % that can be interpreted in Lua string functions like gsub.
booleanIsNumeric(string str)
Is str representing a number?
booleanIsStyledString(string str)
Test if a string is a StyledString
 RemoveDiacritics(string s, boolean specialChars)
Remove diacritics (accents) from the string s.
string, stringSplitPath(string path)
Split a path into directory and file, no file if path ends by a '/'.
tableSplitUrl(string url)
Split an URL into protocol, host, path, query and anchor.
stringescape_lines(string s)
Escape lines \n and \r as well as tabs \t and backslash \, using backslashes \.
stringexcel_column_name(int n)
Returns the Excel column name of the n-th column.
stringfromBytes(table bytes)
Convert a table of bytes (integers) into a string.
stringgsub_args(string str, ...)
Replace %1, %2 in str by first and second following arguments.
tablestrfind_all(string s, string pattern, boolean plain, boolean allowOverlap)
Looks for all matches of pattern in string s.
int, int, stringstrfind_last(string s, string pattern, boolean plain)
Looks for the last match of pattern in string s.
RawTablestring2RawTable(string s)
Convert a string into a which allow hash operation.
stringstring_between(string str, string left, string right)
Return the portion of str found between left and right.
stringstring_concat(string src, string delimiter, ...)
Concatenate toAdd after src with a delimiter, handles nil and empty string initialization.
booleanstring_ends_with(string str, string rightStr)
Does str ends with rightStr?
stringstring_first(string a, string b)
Returns the first of a and b in alphabetical order.
stringstring_join(table list, string delimiter)
Concat the elements of table list, separated by the string delimiter.
stringstring_last(string a, string b)
Returns the last of a and b in alphabetical order.
stringstring_left(string str, int n)
Return the n leftmost chars of the string str.
stringstring_line_to_space(string str)
Replace line \r and \n characters by a space.
stringstring_right(string str, int n)
Return the n rightmost chars of the string str.
tablestring_slice(string s, int sze, boolean fromEnd)
Slice the string into blocks of size, except last one that may be shorter.
tablestring_split(string s, string delim, int limit)
Split the string s by a string delimiter delim.
tablestring_split_lines(string s)
Split the string s by lines, handle \r, \n and \r\n.
booleanstring_starts_with(string str, string leftStr)
Does str starts with leftStr?
stringstring_time(number seconds, boolean exact, boolean clockFormat)
Converts a time (music duration, debug chrono...) into readable string.
stringstring_trim(string s, string toTrim, string side)
Removes toTrim at the beginning and the end of s.
tabletoBytes(string s)
Convert a string into a table of bytes (integers).
stringunescape_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

ParameterTypeDefaultDescription
strstring str
Return
boolean: true or nil (for false).

IsNumeric(string str)

Is str representing a number?

ParameterTypeDefaultDescription
strstring The string to test
Return
boolean: true or nil (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.

ParameterTypeDefaultDescription
strstring str
Return
string: % are escaped by a %.

string_split(string s, string delim, int limit)

Split the string s by a string delimiter delim.

ParameterTypeDefaultDescription
sstring The string to split
delimstring The delimiter
limitint0Maximum 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.

ParameterTypeDefaultDescription
sstring 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.

ParameterTypeDefaultDescription
sstring The string to slice
szeint Size of each slice
fromEndboolean (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.

ParameterTypeDefaultDescription
listtable list
delimiterstring"" 
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 nil or empty.
myList = string_concat(myList, ", ", "an item") is a a shortcut for myList = (myList and (myList..", ") or "") .. "an item" with more nil handling.

ParameterTypeDefaultDescription
srcstring src
delimiterstring"" 
...  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.

ParameterTypeDefaultDescription
sstring If nil, returns ""
toTrimstring" \t\r\n"List of chars to remove, if nil, then trim spaces, tabs, lines.
sidestring"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.

ParameterTypeDefaultDescription
strstring str
nint 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.

ParameterTypeDefaultDescription
strstring str
nint 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?

ParameterTypeDefaultDescription
strstring str
leftStrstring leftStr
Return
boolean: true if string_left(str, strlen(leftStr)) == leftStr, nil for false.
Error
if str is not a string, or strlen(leftStr) == 0

string_ends_with(string str, string rightStr)

Does str ends with rightStr?

ParameterTypeDefaultDescription
strstring str
rightStrstring rightStr
Return
boolean: true if string_right(str, strlen(rightStr)) == rightStr, nil for 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.

ParameterTypeDefaultDescription
strstring str
leftstring left
rightstring right
Return
string: nil if left or right are not found in str.

string_line_to_space(string str)

Replace line \r and \n characters by a space.

ParameterTypeDefaultDescription
strstring 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...

ParameterTypeDefaultDescription
nint 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.

ParameterTypeDefaultDescription
secondsnumber Time in seconds, 0.010 = 10ms
exactbooleanfalseIf false, removes details (e.g. no milliseconds if time is minutes and you don't need so much precision).
clockFormatbooleanfalseClock 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.

ParameterTypeDefaultDescription
astring a
bstring 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.

ParameterTypeDefaultDescription
astring a
bstring b
Return
string: string
Example

print(string_last("Harmony", "Assistant")) --> "Harmony"

SplitUrl(string url)

Split an URL into protocol, host, path, query and anchor.

ParameterTypeDefaultDescription
urlstring 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 or nil
Error
if url is nil or empty string

SplitPath(string path)

Split a path into directory and file, no file if path ends by a '/'.

ParameterTypeDefaultDescription
pathstring path
Returns
string: directory
string: file name

RemoveDiacritics(string s, boolean specialChars)

Remove diacritics (accents) from the string s.

ParameterTypeDefaultDescription
sstring s
specialCharsbooleanfalseIf 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 \.

ParameterTypeDefaultDescription
sstring 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.

ParameterTypeDefaultDescription
sstring 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 ;)

ParameterTypeDefaultDescription
strstring 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.

ParameterTypeDefaultDescription
str1string First (oldest) string
str2string Second (newest) string
stylebooleanfalseAdd 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.

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

ParameterTypeDefaultDescription
sstring s
patternstring pattern
plainbooleanfalse 
Returns
int: start position of found substring, or nil if 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.
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.

ParameterTypeDefaultDescription
sstring s
patternstring pattern
plainbooleanfalse 
allowOverlapbooleanfalseIn 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:
  1. int start position of occurence,
  2. int end position of occurence,
  3. Optional string first capture,
  4. Optional string second capture...
If no occurence found, the table is empty.

toBytes(string s)

Convert a string into a table of bytes (integers).

ParameterTypeDefaultDescription
sstring 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.

ParameterTypeDefaultDescription
bytestable bytes
Return
string: string

string2RawTable(string s)

Convert a string into a which allow hash operation.

ParameterTypeDefaultDescription
sstring s
Return
RawTable: RawTable
See
toBytes that converts to table
Example


 local str = "abc"
 local rt = string2RawTable(str)
 local md5 = rt.MD5Digest --> 900150983cd24fb0d6963f7d28e17f72
 local fastHash = rt.Hash --> a2f8b62e5f34c610