260. String

String manipulation routines

260.1. Summary of Module Globals

List of Global Functions

  • CAlnum - The 62 upper and lowercase ASCII letters and digits

  • CAlpha - The 52 upper and lowercase ASCII letters

  • CDigit - Digits: 0123456789

  • CGraph - Printable ASCII charcters except whitespace, 33-126

  • CLower - The 26 lowercase ASCII letters

  • CPrint - Printable ASCII characters including whitespace

  • CPunct - The ASCII printable non-blank non-alphanumeric characters

  • CSpace - ASCII whitespace: SPACE CR LF HT VT FF

  • CUpper - The 26 uppercase ASCII letters

  • CXdigit - Hexadecimal digits: 0123456789ABCDEFabcdef

  • CutBlanks - Remove blanks at begin and end of input string.

  • CutRegexMatch - Remove first or every match of given regular expression from a string

  • CutZeros - Remove any leading zeros

  • EscapeTags - Function for escaping (replacing) (HTML|XML...) tags with their (HTML|XML...) meaning.

  • FirstChunk - Shorthand for select (splitstring (s, separators), 0, "") Useful now that the above produces a deprecation warning.

  • FormatFilename - Format file name - truncate the middle part of the directory to fit to the reqested lenght. Path elements in the middle of the string are replaced by ellipsis (...). The result migth be longer that requested size if size of the last element (with ellipsis) is longer than the requested size. If the requested size is greater than size of the input then the string is not modified. The last part (file name) is never removed.

  • FormatRate - Return a pretty description of a download rate

  • FormatRateMessage - Add a download rate status to a message.

  • FormatSize - Return a pretty description of a byte count

  • FormatSizeWithPrecision - Return a pretty description of a byte count

  • GetCommentLines - Get comment without metadata

  • GetMetaDataLines - Get metadata lines from input string

  • NewlineItems -

  • NonEmpty -

  • OptFormat - Optional formatted text

  • OptParens - Optional parenthesized text

  • Pad - Add spaces after the text to make it long enough

  • PadZeros - Add zeros before the text to make it long enough.

  • ParseOptions - Parse string of values

  • ParseSysconfigComment - Parse metadata from a sysconfig comment

  • Quote - Quote a string with 's

  • Random - Make a random base-36 number. srandom should be called beforehand.

  • Replace - Replace substring in a string. All substrings source are replaced by string target.

  • TextTable - Function creates text table without using HTML tags. (Useful for commandline) Undefined option uses the default one.

  • UnQuote - Unquote a string with 's (quoted with quote)

  • UnderlinedHeader - Function returns underlined text header without using HTML tags. (Useful for commandline)

  • ValidCharsFilename - Characters valid in a filename (not pathname). Naturally "/" is disallowed. Otherwise, the graphical ASCII characters are allowed.

  • WrapAt - Returns text wrapped at defined margin. Very useful for translated strings used for pop-up windows or dialogs where you can't know the width. It controls the maximum width of the string so the text should allways fit into the minimal ncurses window. If you expect some long words, such us URLs or words with a hyphen inside, you can also set the additional split-characters to "/-". Then the function can wrap the word also after these characters. This function description was wrapped using the function String::WrapAt().

List of Global Variables

    260.2. Global Functions

    260.2.1. CAlnum

    The 62 upper and lowercase ASCII letters and digits

    Return value

    • string

    260.2.2. CAlpha

    The 52 upper and lowercase ASCII letters

    Return value

    • string

    260.2.3. CDigit

    Digits: 0123456789

    Return value

    • string

    260.2.4. CGraph

    Printable ASCII charcters except whitespace, 33-126

    Return value

    • string

    260.2.5. CLower

    The 26 lowercase ASCII letters

    Return value

    • string

    260.2.6. CPrint

    Printable ASCII characters including whitespace

    Return value

    • string

    260.2.7. CPunct

    The ASCII printable non-blank non-alphanumeric characters

    Return value

    • string

    260.2.8. CSpace

    ASCII whitespace: SPACE CR LF HT VT FF

    Return value

    • string

    260.2.9. CUpper

    The 26 uppercase ASCII letters

    Return value

    • string

    260.2.10. CXdigit

    Hexadecimal digits: 0123456789ABCDEFabcdef

    Return value

    • string

    260.2.11. CutBlanks

    Remove blanks at begin and end of input string.

    Function parameters

    • string input

    Return value

    • string - stripped string

    Example 84. 

     CutBlanks("  any  input     ") -> "any  input"


    260.2.12. CutRegexMatch

    Remove first or every match of given regular expression from a string

    Function parameters

    • string input

    • string regex

    • boolean glob

    Return value

    • string - that has matches removed

    260.2.13. CutZeros

    Remove any leading zeros

    Function parameters

    • string input

    Return value

    • string - that has leading zeros removed

    260.2.14. EscapeTags

    Function for escaping (replacing) (HTML|XML...) tags with their (HTML|XML...) meaning.

    Function parameters

    • string text

    Return value

    • string - escaped text

    260.2.15. FirstChunk

    Shorthand for select (splitstring (s, separators), 0, "") Useful now that the above produces a deprecation warning.

    Function parameters

    • string s

    • string separators

    Return value

    • string - first component or ""

    260.2.16. FormatFilename

    Format file name - truncate the middle part of the directory to fit to the reqested lenght. Path elements in the middle of the string are replaced by ellipsis (...). The result migth be longer that requested size if size of the last element (with ellipsis) is longer than the requested size. If the requested size is greater than size of the input then the string is not modified. The last part (file name) is never removed.

    Function parameters

    • string file_path

    • integer len

    Return value

    • string - Truncated file name

    Example 85. 

     FormatFilename("/really/very/long/file/name", 15) -> "/.../file/name"
     FormatFilename("/really/very/long/file/name", 5) -> ".../name"
     FormatFilename("/really/very/long/file/name", 100) -> "/really/very/long/file/name"
    


    260.2.17. FormatRate

    Return a pretty description of a download rate

    Function parameters

    • integer bytes_per_second

    Return value

    • string - formatted string

    Example 86. 

     FormatRate(6780) -> ""
     FormatRate(0) -> ""
     FormatRate(895321) -> ""


    260.2.18. FormatRateMessage

    Add a download rate status to a message.

    Function parameters

    • string text

    • integer avg_bps

    • integer curr_bps

    Return value

    • string - formatted message

    260.2.19. FormatSize

    Return a pretty description of a byte count

    Function parameters

    • integer bytes

    Return value

    • string - formatted string

    Example 87. 

     FormatSize(23456767890) -> "223.70 MB"


    260.2.20. FormatSizeWithPrecision

    Return a pretty description of a byte count

    Function parameters

    • integer bytes

    • integer precision

    • boolean omit_zeroes

    Return value

    • string - formatted string

    Example 88. 

     FormatSizeWithPrecision(128, 2, true) -> "128 B"
     FormatSizeWithPrecision(4096, 2, true) -> "4 kB"
     FormatSizeWithPrecision(4096, 2, false) -> "4.00 kB"
     FormatSizeWithPrecision(1024*1024, 2, true) -> "1 MB"


    260.2.21. GetCommentLines

    Get comment without metadata

    Function parameters

    • string input

    Return value

    • string - Comment used as variable description

    260.2.22. GetMetaDataLines

    Get metadata lines from input string

    Function parameters

    • string input

    Return value

    • list<string> - Metadata lines in list

    260.2.23. NewlineItems

    Function parameters

    • string s

    Return value

    • list<string> - the items as a list, with empty lines removed

    260.2.24. NonEmpty

    Function parameters

    • list<string> l

    Return value

    • list<string> - only non-"" items

    260.2.25. OptFormat

    Optional formatted text

    Function parameters

    • string f

    • string s

    Return value

    • string - sformat (f, s) if s is neither empty or nil, else ""

    260.2.26. OptParens

    Optional parenthesized text

    Function parameters

    • string s

    Return value

    • string - " (Foo)" if Foo is neither empty or nil, else ""

    260.2.27. Pad

    Add spaces after the text to make it long enough

    Function parameters

    • string text

    • integer length

    Return value

    • string - padded text

    260.2.28. PadZeros

    Add zeros before the text to make it long enough.

    Function parameters

    • string text

    • integer length

    Return value

    • string - padded text

    260.2.29. ParseOptions

    Parse string of values

    Function parameters

    • string options

    • map parameters

    Return value

    • list<string> - List of strings

    260.2.30. ParseSysconfigComment

    Parse metadata from a sysconfig comment

    Function parameters

    • string comment

    Return value

    • map<string, string> - parsed metadata

    260.2.31. Quote

    Quote a string with 's

    Function parameters

    • string var

    Return value

    • string - quoted string

    Example 89. 

     quote("a'b") -> "a'\''b"


    260.2.32. Random

    Make a random base-36 number. srandom should be called beforehand.

    Function parameters

    • integer len

    Return value

    • string - random string of 0-9 and a-z

    260.2.33. Replace

    Replace substring in a string. All substrings source are replaced by string target.

    Function parameters

    • string s

    • string source

    • string target

    Return value

    • string - result

    260.2.34. TextTable

    Function creates text table without using HTML tags. (Useful for commandline) Undefined option uses the default one.

    Function parameters

    • list<string> header

    • list <list <string> > items

    • map <string, any> options

    Return value

    • string - table Header: [ "Id", "Configuration", "Device" ] Items: [ [ "1", "aaa", "Samsung Calex" ], [ "2", "bbb", "Trivial Trinitron" ] ] Possible Options: horizontal_padding (for columns), table_left_padding (for table)

    260.2.35. UnQuote

    Unquote a string with 's (quoted with quote)

    Function parameters

    • string var

    Return value

    • string - unquoted string

    260.2.36. UnderlinedHeader

    Function returns underlined text header without using HTML tags. (Useful for commandline)

    Function parameters

    • string header_line

    • integer left_padding

    Return value

    • string - underlined header line

    260.2.37. ValidCharsFilename

    Characters valid in a filename (not pathname). Naturally "/" is disallowed. Otherwise, the graphical ASCII characters are allowed.

    Return value

    • string - for ValidChars

    260.2.38. WrapAt

    Returns text wrapped at defined margin. Very useful for translated strings used for pop-up windows or dialogs where you can't know the width. It controls the maximum width of the string so the text should allways fit into the minimal ncurses window. If you expect some long words, such us URLs or words with a hyphen inside, you can also set the additional split-characters to "/-". Then the function can wrap the word also after these characters. This function description was wrapped using the function String::WrapAt().

    Function parameters

    • string text

    • integer width

    • string split_string

    Return value

    • string - wrapped string

    Example 90. 

     String::WrapAt("Some very long text",30,"/-");
    


    260.3. Global Variables

    260.4. Module Requirements

    none