Section Strings
strings utility functions
Summary
Constant | Type | Summary |
---|---|---|
UNICODE_DELTA | string | Unicode delta: U+0394 = Δ |
UNICODE_DOUBLE_FLAT | string | Unicode double flat: U+1D12B = 𝄫 |
UNICODE_DOUBLE_SHARP | string | Unicode double sharp: U+1D12A = 𝄪 |
UNICODE_FLAT | string | Unicode flat: U+266D = ♭ |
UNICODE_NATURAL | string | Unicode natural: U+266E = ♮ |
UNICODE_SHARP | string | Unicode sharp: U+266F = ♯ |
UNICODE_SLASHED_O | string | Unicode slashed-O: U+00F8 = ø |
Return type | Function and summary |
---|---|
string | CompareMyrScriptVersion(string version) Is MyrScript =, < or > version x.y.z? |
string | CompareVersions(string a, string b) Compare two version numbers in x.y.z string format. |
Deserialize(string str) Deserialize a string into object. | |
number, number | DrawText(Graph graphContext, string text, string fontFamily, int fontSize, int fontFace, number left, number top, number right, number bottom, int verticalAlign, int horizontalAlign, boolean allowOverflow, int originX, int originY, boolean autoLineBreak, Score score) Draw a text, using given font family, size and face, aligned within boundaries. |
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) Remove diacritics (accents) from the string s. | |
string, int | SMPitchToString(int pitch, int preferredAccidental, int compatible, string preferredNote) Extension and bugfix for builtin PitchToString function. |
int, int | SMStringToPitch(string name) Extension and bugfix for builtin StringToPitch function. |
string | Serialize(obj) Serialize an object to store it as string and later deserialization. |
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. |
number, number | StringSizes(Graph graphContext, string s, string fontFamily, int fontSize, int fontFace, Score score) Get the string size using given font family, size and face. |
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 | gsub_args(string str, ...) Replace %1, %2 in str by first and second following arguments. |
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. |
string | unescape_lines(string s) Unescape lines \n and \r as well as tabs \t and backslash \, escaped by backslashes. |
Constants
string UNICODE_DOUBLE_FLAT
Unicode double flat: U+1D12B = 𝄫
string UNICODE_DOUBLE_SHARP
Unicode double sharp: U+1D12A = 𝄪
Functions
CompareMyrScriptVersion(string version)
Is MyrScript =, < or > version x.y.z?
As Application.MyrScriptVersion
is a string, comparison may fail in the future, "10.1.1" is greater than "9.9.9" but string comparison will stay it's lower (because "1" < "9").
Parameter | Type | Default | Description |
---|---|---|---|
version | string | Version to compare, in x.y.z form (e.g.: "1.6.9") |
- Return
- string:
"<", ">"
or"="
ifApplication.MyrScriptVersion
is lower, greater or equal to version.
CompareVersions(string a, string b)
Compare two version numbers in x.y.z string format.
Parameter | Type | Default | Description |
---|---|---|---|
a | string | a | |
b | string | b |
- Return
- string:
"<", ">"
or"="
if a is lower, greater or equal to b. - Example
CompareVersions("9.9.7", "9.9.6") --> ">"
SMPitchToString(int pitch, int preferredAccidental, int compatible, string preferredNote)
Extension and bugfix for builtin PitchToString function.
- Allow pitches out of 0..127 bounds, but keep in mind that HA can't handle them!
PitchToString(11)
gives 11,SMPitchToString(11)
returns B-1
Parameter | Type | Default | Description |
---|---|---|---|
pitch | int | pitch | |
preferredAccidental | int | AUTO | Preferred accidental in MSDefine. HA can handle A## and Bbb as note names. If a string is provided, then if it contains a "b" or a "#", it's interpreted as FLAT or SHARP constants. |
compatible | int | ffalse | If true return value that HA can handle, 11 instead of B-1, a value in bounds. |
preferredNote | string | "" | English note "A" to "G", if no preferred accidental or AUTO, select the enharmonic from the preferred note. If result is "A" and preferred note is "G" then result will be "G##", unless allowDoubleAccidental is false. |
- Returns
- string: Anglo-saxon note name followed by octave, e.g. "Ab5".
- int: Accidental for the note, one of MSDefine NATURAL,FLAT,SHARP,DOUBLE_FLAT,DOUBLE_SHARP constants.
SMStringToPitch(string name)
Extension and bugfix for builtin StringToPitch function.
- Allow pitches out of 0..127 bounds
StringToPitch("B-1")
gives the strange 71 result,SMStringToPitch("B-1")
returns 11- Handles double-sharp (## or x) and double-flat (bb)
Parameter | Type | Default | Description |
---|---|---|---|
name | string | English not name followed by octave. If octave is missing, considers it's the 4th octave. |
- Returns
- int: May be out of 0..127 range.
- int: Preferred accidental, one of AUTO, SHARP, FLAT... constants from MSDefine
Serialize(obj)
Serialize an object to store it as string and later deserialization.
Parameter | Type | Default | Description |
---|---|---|---|
obj | A number, string, table or a class that own :serialize(...) method. |
- Return
- string: string
- Error
- If something can't be serialized.
Deserialize(string str)
Deserialize a string into object.
Parameter | Type | Default | Description |
---|---|---|---|
str | string | str |
- Return
- Copy of previously serialized object
StringSizes(Graph graphContext, string s, string fontFamily, int fontSize, int fontFace, Score score)
Get the string size using given font family, size and face.
Multi-line is not handled.
Parameter | Type | Default | Description |
---|---|---|---|
graphContext | Graph | The GraphContext: dialog.GraphContext , surface.GraphContext ... | |
s | string | The string, rich string is handled, tokens are transformed (e.g. Moderato $(4) = 80) | |
fontFamily | string | "geneva" | |
fontSize | int | 11 | |
fontFace | int | FACE_NONE | Sum of FACE_* constants in MSDefine |
score | Score | nil | A Score need for special command replacements such as "$T" replaced by score's title. |
- Returns
- number: Width of the string.
- number: Height of the string.
DrawText(Graph graphContext, string text, string fontFamily, int fontSize, int fontFace, number left, number top, number right, number bottom, int verticalAlign, int horizontalAlign, boolean allowOverflow, int originX, int originY, boolean autoLineBreak, Score score)
Draw a text, using given font family, size and face, aligned within boundaries.
This may be useful if you create your own component that need to display text.
Parameter | Type | Default | Description |
---|---|---|---|
graphContext | Graph | The GraphContext: dialog.GraphContext , surface.GraphContext ... | |
text | string | The text to draw, rich strings are handled, tokens are transformed (e.g. $(4) = 80) | |
fontFamily | string | "geneva" | |
fontSize | int | 11 | |
fontFace | int | FACE_NONE | Sum of FACE_* constants in MSDefine |
left | number | Left boundary | |
top | number | Top boundary | |
right | number | Right boundary | |
bottom | number | Bottom boundary | |
verticalAlign | int | VERTICAL_ALIGN_TOP, VERTICAL_ALIGN_MIDDLE or VERTICAL_ALIGN_BOTTOM | |
horizontalAlign | int | ALIGN_LEFT, ALIGN_CENTER or ALIGN_RIGHT | |
allowOverflow | boolean | true | Accepts that text can be displayed outside of the boundaries? If not allowed, alignment may be changed to minimize the risk of truncation. |
originX | int | nil | Horizontal origin of alignment, e.g. right aligned from the center (left+bottom)/2 |
originY | int | nil | Vertical origin of alignment, e.g. top aligned from 1/3 of the height. |
autoLineBreak | boolean | false | Split text into lines to try to avoid horizontal overflow? |
score | Score | nil | In case of text with tokens like $T (title), $N (number of pages), if score is provided, tokens are transformed. See Text commands in manual. |
- Returns
- number: Width needed to draw the full text, even if it overflows and as been truncated.
- number: Height needed to draw the full text, even if it overflows and as been truncated.
IsStyledString(string str)
Test if a string is a StyledString
Parameter | Type | Default | Description |
---|---|---|---|
str | string | str |
- Return
- boolean:
true
ornil
(for false).
IsNumeric(string str)
Is str representing a number?
Parameter | Type | Default | Description |
---|---|---|---|
str | string | The string to test |
- Return
- boolean:
true
ornil
(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(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_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 | delimiter |
- Return
- string: string
- Example
string_join({"Anna", "Bob", "Charlie", "Dolores"}, ", ") --> Anna, Bob, Charlie, Dolores
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 | delimiter |
- 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.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:
true
ifstring_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?
Parameter | Type | Default | Description |
---|---|---|---|
str | string | str | |
rightStr | string | rightStr |
- Return
- boolean:
true
ifstring_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.
Parameter | Type | Default | Description |
---|---|---|---|
str | string | str | |
left | string | left | |
right | string | 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.
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_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.Path = "cgi-bin/bbs/YaBB.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
nil
or 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)
Remove diacritics (accents) from the string s.
Parameter | Type | Default | Description |
---|---|---|---|
s | string | s |
- 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.