virtual class text
Provides utility functions for text processing. All functions are defined as static.
| text | Provides utility functions for text processing. |
| Common Arguments | |
| Functions | |
| capitalize | static Returns a copy of the given string with the first character uppercased and the remainder lowercased. |
| center | static Returns a string of the specified width with the given string centerted and padded with the specified character. |
| change | static Returns a copy of the given string with the characters in the specified range replaced with the specified substring. |
| chomp | static Returns a copy of the given string with the last newline character removed (if present). |
| chop | static Returns the last character of the given string. |
| colorize | static Returns a copy of the given string with ANSI escape codes. |
| contains | static Returns 1 if the given string contains the specified substring. |
| contains_str | static If the given string contains the specified substring, returns that substring. |
| count | static Returns the number of non-overlapping occurrences of the specified substring in the given string. |
| delete | static Returns a copy of the given string with the specified string removed. |
| ends_with | static Returns 1 if the given string ends with one of the specified suffixes. |
| find_any | static Returns the lowest index in the given string where each specified substring is found. |
| hash | static Returns the hash value of the given string. |
| index | static Returns the index of the first occurrence of the specified substring in the given string within the optionally specified range. |
| insert | static Returns a copy of the given string with the specified substring inserted at the specified position. |
| is_alpha | static Returns 1 if all characters in the given string are alphabetic. |
| is_digit | static Returns 1 if all characters in the given string are digits. |
| is_lower | static Returns 1 if all cased characters in the given string are lowercase. |
| is_printable | static Returns 1 if all characters in the given string are printable. |
| is_single_bit_type | static Returns 1 if the given string is bit, logic, or reg. |
| is_space | static Returns 1 if all characters in the given string are whitespace characters: a space (“ “), a tab (\t), or a newline (\n). |
| is_upper | static Returns 1 if all cased characters in the given string are uppercase. |
| join_str | static Returns a string by concatenating the strings in the given string queue, separated by the specified separator. |
| lc_first | static Returns a copy of the given string with the first character lowercased and the remainder unchanged. |
| ljust | static Returns a string of the specified width with the given string left justified and padded with the specified character. |
| lstrip | static Returns a copy of the given string with leading characters removed. |
| only | static Returns 1 if the given string consists of only the specified set of characters. |
| partition | static Searches the first occurrence of the specified separator in the given string and returns an array of three strings. |
| replace | static Returns a copy of the given string with the specified string replaced with a new string. |
| reverse | static Returns a copy of the given string with the characters in reverse order. |
| rfind_any | static Returns the highest index in the given string where each specified substring is found. |
| rindex | static Returns the index of the last occurrence of the specified substring in the given string within the optionally specified range. |
| rjust | static Returns a string of the specified width with the given string right justified and padded with the specified character. |
| rpartition | static Searches the last occurrence of the specified separator in the given string and returns an array of three strings. |
| rsplit | static Returns a queue of substrings by dividing the given string by the specified separator from the right. |
| rstrip | static Returns a copy of the given string with trailing characters removed. |
| slice | static Returns a substring in the specified range. |
| slice_len | static Returns a substring in the specified range. |
| split | static Returns a queue of substrings by dividing the given string by the specified separator. |
| starts_with | static Returns 1 if the given string starts with one of the specified prefixes. |
| strip | static Returns a copy of the given string with leading and trailing characters removed. |
| swap_case | static Returns a copy of the given string with uppercase characters converted to lowercase, and lowercase characters converted to uppercase. |
| title_case | static Returns a copy of the given string with the first character of words uppercased and the remainder lowercased. |
| trim | static Returns a copy of the given string with the specified numbers of leading and trailing characters removed. |
| uc_first | static Returns a copy of the given string with the first character uppercased and the remainder unchanged. |
| untabify | static Returns a copy of the given string where all tab characters (\t) are replaced by one or more spaces, depending on the tab positions. |
| start_pos | Specifies the start position in a string. The position of the first character is 0, the position of the second character is 1, and so on. The position can be specified as a negative number. The position of the last character can be specified as -1, the position of the second to the last character can be specified as -2, and so on. The default is 0 (the first character). |
| end_pos | Specifies the end position in a string using the same rule as the start_pos. The default is -1 (the last character). |
____ position 0 or -26
/ ____ position 5 or -21
/ / ____ position 15 or -11
/ / / ____ position 25 or -1
/ / / /
V V V V
"How common arguments work."
|----------------------->| start_pos = 0, end_pos = 25 \
|----------------------->| start_pos = 0, end_pos = -1 \ these specify the same range of the string
|----------------------->| start_pos = -26, end_pos = 25 /
|----------------------->| start_pos = -26, end_pos = -1 /
|-------->| start_pos = 5, end_pos = 15 \
|-------->| start_pos = 5, end_pos = -11 \ these specify the same range of the string
|-------->| start_pos = -21, end_pos = 15 /
|-------->| start_pos = -21, end_pos = -11 /
static function string capitalize( string s )
static Returns a copy of the given string with the first character uppercased and the remainder lowercased.
| s | A string to be capitalized. |
A copy of s with the first character uppercased and the remainder lowercased.
assert( text::capitalize( "capitalize me!" ) == "Capitalize me!" );
static function string center( string s, int width, byte fill_char = " ", bit trim_ends = 0 )
static Returns a string of the specified width with the given string centerted and padded with the specified character.
| s | A string to be centered. |
| width | The width of the returned string. |
| fill_char | optional The character used for padding if width is wider than the length of s. The default is a space character (“ “). |
| trim_ends | optional If width is narrower than the length of s and trim_ends is 1, then the head and the tail of s are trimmed to fit within width. If trim_ends is 0, then width is widened to the length of s. If width is wider than or equal to the length of s, trim_ends is ignored. The default is 0. |
A string with s placed at the center and padded with fill_char.
assert( text::center( "center me", 15 ) == " center me " ); assert( text::center( "center me", 15, "-" ) == "---center me---" ); assert( text::center( "center me", 7 ) == "center me" ); // widened to fit assert( text::center( "center me", 7, .trim_ends( 1 ) ) == "enter m" ); // trimmed
static function string change( string s, string sub, int start_pos = 0, int end_pos = -1 )
static Returns a copy of the given string with the characters in the specified range replaced with the specified substring.
| s | A string to be changed. |
| sub | A substring. |
| start_pos | optional Specifies the left-most position in s to be replaced. See Common Arguments. |
| end_pos | optional Specifies the right-most position in s to be replaced. See Common Arguments. |
A copy of s with the characters in the specified range replaced with sub. If the specified range is invalid, s is returned without a change. If s is an empty string (“”), no change is made.
assert( text::change( "a primary library", "function", .start_pos( 10 ) ) == "a primary function" ); // |---->| // 10
static function string chomp( string s )
static Returns a copy of the given string with the last newline character removed (if present). Returns the given string as is if the given string does not end in a newline character.
| s | A string to be chomped |
Returns a copy of s with the last newline character removed (if present). Returns s as is if the given string does not end in a newline character.
assert( text::chomp( "abc" ) == "abc" ); assert( text::chomp( "abc\n" ) == "abc" ); assert( text::chomp( "abc\n\n" ) == "abc\n" );
static function string colorize( string s, fg_color_e fg = FG_BLACK, bg_color_e bg = BG_WHITE, bit bold = 0, bit underline = 0, bit blink = 0, bit reverse = 0 )
static Returns a copy of the given string with ANSI escape codes.
| s | A string to be colorized. |
| fg | optional The foreground color of s. See fg_color_e for available colors. The default is black. |
| bg | optional The background color of s. See bg_color_e for available colors. The default is white. |
| bold | optional If 1, s is boldfaced. The default is 0. |
| underline | optional If 1, s is underlined. The default is 0. |
| blink | optional If 1, s is blinked. The default is 0. |
| reverse | optional If 1, the foreground and the background colors of s are reversed. The default is 0. |
A copy of s with ANSI escape codes.
$display( text::colorize( "display me in red", FG_RED ) );
static function bit contains( string s, string sub, int start_pos = 0, int end_pos = -1 )
static Returns 1 if the given string contains the specified substring.
| s | An input string. |
| sub | A substring to search. An empty substring (“”) matches no input string. |
| start_pos | optional Specifies the position in s to begin the search. See Common Arguments. |
| end_pos | optional Specifies the position in s to end the search. See Common Arguments. |
If s contains sub, 1 is returned. Otherwise, 0 is returned.
assert( text::contains( "a primary library", "primary" ) == 1 ); assert( text::contains( "a primary library", "primary", .start_pos( 3 ) ) == 0 ); // |----------->| // 3 assert( text::contains( "a primary library", "primary", .end_pos( 7 ) ) == 0 ); // |----->| // 7 assert( text::contains( "a primary library", "primary", .end_pos( -9 ) ) == 1 ); // |------>| // -9
contains_str, count, ends_with, find_any, index, only, rfind_any, rindex, starts_with
static function string contains_str( string s, string sub, int start_pos = 0, int end_pos = -1 )
static If the given string contains the specified substring, returns that substring.
| s | An input string. |
| sub | A substring to search. An empty substring (“”) matches no input string. |
| start_pos | optional Specifies the position in s to begin the search. See Common Arguments. |
| end_pos | optional Specifies the position in s to end the search. See Common Arguments. |
If s contains sub, sub is returned. Otherwise, an empty string (“”) is returned.
assert( text::contains_str( "a primary library", "primary" ) == "primary" ); assert( text::contains_str( "a primary library", "primary", .start_pos( 3 ) ) == "" ); // |----------->| // 3 assert( text::contains_str( "a primary library", "primary", .end_pos( 7 ) ) == "" ); // |----->| // 7 assert( text::contains_str( "a primary library", "primary", .end_pos( -9 ) ) == "primary" ); // |------>| // -9
contains, count, ends_with, find_any, index, only, rfind_any, rindex, starts_with
static function int unsigned count( string s, string sub, int start_pos = 0, int end_pos = -1 )
static Returns the number of non-overlapping occurrences of the specified substring in the given string.
| s | An input string. |
| sub | A substring to search. An empty substring (“”) matches no input string. |
| start_pos | optional Specifies the position in s to begin the search. See Common Arguments. |
| end_pos | optional Specifies the position in s to end the search. See Common Arguments. |
The number of non-overlapping occurrences of sub in s.
assert( text::count( "a primary library", "ary" ) == 2 ); assert( text::count( "a primary library", "ary", .start_pos( 3 ) ) == 2 ); // |----------->| // 3 assert( text::count( "a primary library", "ary", .end_pos( 7 ) ) == 0 ); // |----->| // 7 assert( text::count( "a primary library", "ary", .end_pos( -9 ) ) == 1 ); // |------>| // -9
contains, contains_str, ends_with, find_any, index, only, rfind_any, rindex, starts_with
static function string delete( string s, string sub, int count = -1 )
static Returns a copy of the given string with the specified string removed.
| s | An input string. |
| sub | A substring to remove. An empty substring (“”) matches no input string. |
| count | optional The number of substrings to remove. If specified, only the first count occurrences are removed. By default, all occurrences are removed. |
A copy of s with the first count occurrences of sub removed.
assert( text::delete( "abcabc", "abc" ) == "" ); assert( text::delete( "abcabc", "abc", 1 ) == "abc" );
static function bit ends_with( string s, string_q suffixes, int start_pos = 0, int end_pos = -1 )
static Returns 1 if the given string ends with one of the specified suffixes.
| s | An input string. |
| suffixes | A queue of suffix strings. The suffixes can be specified using an array literal. An empty string (“”) matches no input string. |
| start_pos | optional Specifies the position in s to begin the search. See Common Arguments. |
| end_pos | optional Specifies the position in s to end the search. See Common Arguments. |
If s ends with one of the specified suffixes, 1 is returned. Otherwise, 0 is returned.
assert( text::ends_with( "a primary library", { "primary", "library" } ) == 1 );
assert( text::ends_with( "a primary library", { "primary", "library" }, .start_pos( 3 ) ) == 1 );
// |----------->|
// 3
assert( text::ends_with( "a primary library", { "primary", "library" }, .end_pos( 7 ) ) == 0 );
// |----->|
// 7
assert( text::ends_with( "a primary library", { "primary", "library" }, .end_pos( -9 ) ) == 1 );
// |------>|
// -9contains, contains_str, count, find_any, index, only, rfind_any, rindex, starts_with
static function int find_any( string s, string_q subs, int start_pos = 0, int end_pos = -1 )
static Returns the lowest index in the given string where each specified substring is found.
| s | An input string. |
| subs | A queue of substrings. The substrings can be specified using an array literal. An empty substring (“”) matches no input string. |
| start_pos | optional Specifies the position in s to begin the search. See Common Arguments. |
| end_pos | optional Specifies the position in s to end the search. See Common Arguments. |
The lowest index in s where each substring in subs is found. If no substring is found, -1 is returned.
assert( text::find_any( "a primary library", { "primary", "library" } ) == 2 );
assert( text::find_any( "a primary library", { "primary", "library" }, .start_pos( 3 ) ) == 10 );
// |----------->|
// 3
assert( text::find_any( "a primary library", { "primary", "library" }, .end_pos( 7 ) ) == -1 );
// |----->|
// 7
assert( text::find_any( "a primary library", { "primary", "library" }, .end_pos( -9 ) ) == 2 );
// |------>|
// -9contains, contains_str, count, ends_with, index, only, rfind_any, rindex, starts_with
static function int index( string s, string sub, int start_pos = 0, int end_pos = -1 )
static Returns the index of the first occurrence of the specified substring in the given string within the optionally specified range.
| s | An input string. |
| sub | A substring to search. An empty substring (“”) matches no input string. |
| start_pos | optional Specifies the position in s to begin the search. See Common Arguments. |
| end_pos | optional Specifies the position in s to end the search. See Common Arguments. |
The index of the first occurrence of sub in s. If sub is not found, -1 is returned.
assert( text::index( "a primary library", "ary" ) == 6 ); assert( text::index( "a primary library", "ary", .start_pos( 3 ) ) == 6 ); // |----------->| // 3 assert( text::index( "a primary library", "ary", .end_pos( 7 ) ) == -1 ); // |----->| // 7 assert( text::index( "a primary library", "ary", .end_pos( -9 ) ) == 6 ); // |------>| // -9
contains, contains_str, count, ends_with, find_any, only, rfind_any, rindex, starts_with
static function string insert( string s, string sub, int start_pos = 0 )
static Returns a copy of the given string with the specified substring inserted at the specified position.
| s | An input string. |
| sub | A substring to insert. |
| start_pos | optional Specifies the position where sub is inserted. See Common Arguments. The default is 0 (inserting sub before s). |
A copy of s with sub inserted at start_pos.
assert( text::insert( "abc", "XYZ" ) == "XYZabc" ); // insert "XYZ" before the first character ("a")
assert( text::insert( "abc", "XYZ", 1 ) == "aXYZbc" ); // insert "XYZ" before the character index 1 ("b")
assert( text::insert( "abc", "XYZ", -1 ) == "abXYZc" ); // insert "XYZ" before the last character ("c")
static function bit is_alpha( string s )
static Returns 1 if all characters in the given string are alphabetic. Alphabetic characters are [a-zA-Z].
| s | An input string. |
If all characters in s are alphabetic, 1 is returned. Otherwise, 0 is returned. If s is an empty string, 0 is returned.
assert( text::is_alpha( "abc" ) == 1 ); assert( text::is_alpha( "abc_" ) == 0 );
static function bit is_digit( string s )
static Returns 1 if all characters in the given string are digits. Digits are [0-9].
| s | An input string. |
If all characters in s are digits, 1 is returned. Otherwise, 0 is returned. If s is an empty string, 0 is returned.
assert( text::is_digit( "123" ) == 1 ); assert( text::is_digit( "123X" ) == 0 );
static function bit is_lower( string s )
static Returns 1 if all cased characters in the given string are lowercase. Lowercase characters are [a-z].
| s | An input string. |
If all cased characters in s are lowercase, 1 is returned. Otherwise, 0 is returned. If s is an empty string, 0 is returned.
assert( text::is_lower( "abc" ) == 1 ); assert( text::is_lower( "abcX" ) == 0 ); assert( text::is_lower( "abc!?" ) == 1 ); // all cased characters are lowercase
static function bit is_printable( string s )
static Returns 1 if all characters in the given string are printable. Printable characters are the ones whose ASCII code are between ‘h20 (“ “) and ‘h7F (“~”).
| s | An input string. |
If all characters in s are printable, 1 is returned. Otherwise, 0 is returned. If s is an empty string, 0 is returned.
assert( text::is_printable( "!@#$" ) == 1 ); assert( text::is_printable( "\200" ) == 0 ); // ASCII 'h80 is not printable
static function bit is_single_bit_type( string s )
static Returns 1 if the given string is bit, logic, or reg.
| s | An input string. |
If s is bit, logic, or reg, 1 is returned. Othewise, 0 is returned.
assert( text::is_single_bit_type( "bit" ) == 1 ); assert( text::is_single_bit_type( "int" ) == 0 );
static function bit is_space( string s )
static Returns 1 if all characters in the given string are whitespace characters: a space (“ “), a tab (\t), or a newline (\n).
| s | An input string. |
If all characters in s are whitespace characters, 1 is returned. Otherwise, 0 is returned. If s is an empty string, 0 is returned.
assert( text::is_space( " \t\n" ) == 1 ); assert( text::is_space( "X\t\n" ) == 0 );
static function bit is_upper( string s )
static Returns 1 if all cased characters in the given string are uppercase. Uppercase characters are [A-Z].
| s | An input string. |
If all cased characters in s are uppercase, 1 is returned. Otherwise, 0 is returned. If s is an empty string, 0 is returned.
assert( text::is_upper( "ABC" ) == 1 ); assert( text::is_upper( "ABCx" ) == 0 ); assert( text::is_upper( "ABC!?" ) == 1 ); // all cased characters are uppercase
static function string join_str( string_q strings, string separator = "" )
static Returns a string by concatenating the strings in the given string queue, separated by the specified separator.
| strings | A queue of strings. The strings can be specified using an array literal. |
| separator | optional A separator between strings. The default is an empty string (“”). |
A string by concatenating the strings in strings, separated by separator.
assert( text::join_str( { "abc", "XYZ" } ) == "abcXYZ" );
assert( text::join_str( { "abc", "XYZ" }, "---" ) == "abc---XYZ" );
static function string lc_first( string s )
static Returns a copy of the given string with the first character lowercased and the remainder unchanged.
| s | An input string. |
A copy of s with the first character lowercased and the remainder unchanged.
assert( text::lc_first( "Lower CASE first" ) == "lower CASE first" );
static function string ljust( string s, int width, byte fill_char = " ", bit trim_right = 0 )
static Returns a string of the specified width with the given string left justified and padded with the specified character.
| s | A string to left-justify. |
| width | The width of the returned string. |
| fill_char | optional A character used for padding if width is wider than the length of s. The default is a space character (“ “). |
| trim_right | optional If width is narrower than the length of s and trim_right is 1, then the tail of s is trimmed to fit within width. If trim_right is 0, then width is widened to the length of s. If width is wider than or equal to the length of s, trim_right is ignored. The default is 0. |
A string with s left-justified and padded with fill_char.
assert( text::ljust( "ljust me", 15 ) == "ljust me " ); assert( text::ljust( "ljust me", 15, "-" ) == "ljust me-------" ); assert( text::ljust( "ljust me", 7 ) == "ljust me" ); // widened to fit assert( text::ljust( "ljust me", 7, .trim_right( 1 ) ) == "ljust m" ); // trimmed
static function string lstrip( string s, string chars = " \t\n" )
static Returns a copy of the given string with leading characters removed.
| s | A string to be stripped. |
| chars | optional A string specifying the set of characters to be removed. The default is whitespace characters: a space (“ “), a tab (\t), or a newline (\n). Note that the chars string is not a prefix. All combinations of its characters are stripped. |
A copy of s with leading characters removed.
assert( text::lstrip( " abc" ) == "abc" ); assert( text::lstrip( " \t\nabc" ) == "abc" ); assert( text::lstrip( "aabbcc", "a" ) == "bbcc" ); assert( text::lstrip( "aabbcc", "ab" ) == "cc" ); assert( text::lstrip( "aabbcc", "ba" ) == "cc" ); // "b"s and "a"s are stripped
static function bit only( string s, string chars )
static Returns 1 if the given string consists of only the specified set of characters.
| s | An input string. |
| chars | A string specifying the set of characters to be checked. An empty string (“”) matches no input string. |
If s consists of only the characters in chars, 1 is returned. Othewise, 0 is returned.
assert( text::only( "abc", "abcXYZ" ) == 1 ); assert( text::only( "abcXYZ", "abc" ) == 0 );
contains, contains_str, count, ends_with, find_any, index, rfind_any, rindex, starts_with
static function three_strings partition( string s, string sep )
static Searches the first occurrence of the specified separator in the given string and returns an array of three strings. The returned array consists of: the string before the separator, the separator itself, and the string after the separator. If the separator is not found, the given string and two empty strings are returned.
| s | An input string. |
| sep | A separator. |
An array that consists of the part before sep, the sep, and the part after sep. If sep is not found, returns s and two empty strings.
three_strings s, t1, t2, t3, t4;
s = '{ "abc", "-", "XYZ" };
assert( text::partition( "abc-XYZ", "-" ) == s );
t1 = '{ "", "a", "bcabc" };
t2 = '{ "a", "b", "cabc" };
t3 = '{ "ab", "c", "abc" };
t4 = '{ "abcabc", "", "" };
assert( text::partition( "abcabc", "a" ) == t1 );
assert( text::partition( "abcabc", "b" ) == t2 );
assert( text::partition( "abcabc", "c" ) == t3 );
assert( text::partition( "abcabc", "X" ) == t4 );
static function string replace( string s, string old_str, string new_str, int count = -1 )
static Returns a copy of the given string with the specified string replaced with a new string.
| s | An input string. |
| old_str | An old string. An empty string (“”) matches no input string. |
| new_str | A new string. |
| count | optional The number of strings to replace. If specified, only the first count occurrences are replaced. By default, all occurrences are replaced. |
A copy of s with the first count occurrences of old_str replaced with new_str.
assert( text::replace( "abcabc", "abc", "XYZ" ) == "XYZXYZ" ); assert( text::replace( "abcabc", "abc", "XYZ", 1 ) == "XYZabc" );
static function string reverse( string s )
static Returns a copy of the given string with the characters in reverse order.
| s | An input string. |
A copy of s with the characters in reverse order.
assert( text::reverse( "reverse me!" ) == "!em esrever" ); assert( text::reverse( "wonton? not now" ) == "won ton ?notnow" );
static function int rfind_any( string s, string_q subs, int start_pos = 0, int end_pos = -1 )
static Returns the highest index in the given string where each specified substring is found.
| s | An input string. |
| subs | A queue of substrings. The substrings can be specified using an array literal. An empty substring (“”) matches no input string. |
| start_pos | optional Specifies the position in s to begin the search. See Common Arguments. |
| end_pos | optional Specifies the position in s to end the search. See Common Arguments. |
The highest index in s where each substring in subs is found. If no substring is found, -1 is returned.
assert( text::rfind_any( "a primary library", { "primary", "library" } ) == 10 );
assert( text::rfind_any( "a primary library", { "primary", "library" }, .start_pos( 3 ) ) == 10 );
// |----------->|
// 3
assert( text::rfind_any( "a primary library", { "primary", "library" }, .end_pos( 7 ) ) == -1 );
// |----->|
// 7
assert( text::rfind_any( "a primary library", { "primary", "library" }, .end_pos( -9 ) ) == 2 );
// |------>|
// -9contains, contains_str, count, ends_with, find_any, index, only, rindex, starts_with
static function int rindex( string s, string sub, int start_pos = 0, int end_pos = -1 )
static Returns the index of the last occurrence of the specified substring in the given string within the optionally specified range.
| s | An input string. |
| sub | A substring to search. An empty substring (“”) matches no input string. |
| start_pos | optional Specifies the position in s to begin the search. See Common Arguments. |
| end_pos | optional Specifies the position in s to end the search. See Common Arguments. |
The index of the last occurrence of sub in s. If sub is not found, -1 is returned.
assert( text::rindex( "a primary library", "ary" ) == 14 ); assert( text::rindex( "a primary library", "ary", .start_pos( 3 ) ) == 14 ); // |----------->| // 3 assert( text::rindex( "a primary library", "ary", .end_pos( 7 ) ) == -1 ); // |----->| // 7 assert( text::rindex( "a primary library", "ary", .end_pos( -9 ) ) == 6 ); // |------>| // -9
contains, contains_str, count, ends_with, find_any, index, only, rfind_any, starts_with
static function string rjust( string s, int width, byte fill_char = " ", bit trim_left = 0 )
static Returns a string of the specified width with the given string right justified and padded with the specified character.
| s | A string to right-justify. |
| width | The width of the returned string. |
| fill_char | optional A character used for padding if width is wider than the length of s. The default is a space character (“ “). |
| trim_left | optional If width is narrower than the length of s and trim_left is 1, then the head of s is trimmed to fit within width. If trim_left is 0, then width is widened to the length of s. If width is wider than or equal to the length of s, trim_left is ignored. The default is 0. |
A string with s right justified and padded with fill_char.
assert( text::rjust( "rjust me", 15 ) == " rjust me" ); assert( text::rjust( "rjust me", 15, "-" ) == "-------rjust me" ); assert( text::rjust( "rjust me", 7 ) == "rjust me" ); // widened to fit assert( text::rjust( "rjust me", 7, .trim_left( 1 ) ) == "just me" ); // trimmed
static function three_strings rpartition( string s, string sep )
static Searches the last occurrence of the specified separator in the given string and returns an array of three strings. The returned array consists of: the string before the separator, the separator itself, and the string after the separator. If the separator is not found, the given string and two empty strings are returned.
| s | An input string. |
| sep | A separator. |
An array that consists of the part before sep, the sep, and the part after sep. If sep is not found, returns s and two empty strings.
three_strings s, t1, t2, t3, t4;
s = '{ "abc", "-", "XYZ" };
assert( text::rpartition( "abc-XYZ", "-" ) == s );
t1 = '{ "abc", "a", "bc" };
t2 = '{ "abca", "b", "c" };
t3 = '{ "abcab", "c", "" };
t4 = '{ "abcabc", "", "" };
assert( text::rpartition( "abcabc", "a" ) == t1 );
assert( text::rpartition( "abcabc", "b" ) == t2 );
assert( text::rpartition( "abcabc", "c" ) == t3 );
assert( text::rpartition( "abcabc", "X" ) == t4 );
static function string_q rsplit( string s, string sep = "", int max_split = -1 )
static Returns a queue of substrings by dividing the given string by the specified separator from the right.
| s | An input string. If s is empty, an empty queue is returned. |
| sep | optional A separator. If specified, sep is used as the delimiter. The sep itself is not returned as an element of the queue. If not specified, whitespace characters (a space (“ “), a tab (\t), or a newline (\n)) are used. If sep is not specified, the contiguous whitespaces and the trailing whitespaces are ignored. |
| max_split | optional If specified, at most max_split splits are done from the right and the remaining substring is returned as the first element of the queue. If not specified or -1, there is no limit to the number of splits. |
A queue of substrings (string_q).
string_q s1, s2, s3, s4, t1, t2, t3, t4, t5;
s1 = '{ "abc", "pqr", "xyz" };
s2 = '{ " abc pqr", "xyz" };
s3 = '{ " abc", "pqr", "xyz" };
s4 = '{ "abc", "pqr", "xyz" };
assert( text::rsplit( " abc pqr xyz " ) == s1 );
assert( text::rsplit( " abc pqr xyz ", .max_split( 1 ) ) == s2 );
assert( text::rsplit( " abc pqr xyz ", .max_split( 2 ) ) == s3 );
assert( text::rsplit( " abc pqr xyz ", .max_split( 3 ) ) == s4 );
t1 = '{ "", "abc", "pqr", "xyz", "" };
t2 = '{ "--abc--pqr--xyz", "" };
t3 = '{ "--abc--pqr", "xyz", "" };
t4 = '{ "--abc", "pqr", "xyz", "" };
t5 = '{ "", "abc", "pqr", "xyz", "" };
assert( text::rsplit( "--abc--pqr--xyz--", "--" ) == t1 );
assert( text::rsplit( "--abc--pqr--xyz--", "--", .max_split( 1 ) ) == t2 );
assert( text::rsplit( "--abc--pqr--xyz--", "--", .max_split( 2 ) ) == t3 );
assert( text::rsplit( "--abc--pqr--xyz--", "--", .max_split( 3 ) ) == t4 );
assert( text::rsplit( "--abc--pqr--xyz--", "--", .max_split( 4 ) ) == t5 );
static function string rstrip( string s, string chars = " \t\n" )
static Returns a copy of the given string with trailing characters removed.
| s | A string to be stripped. |
| chars | optional A string specifying the set of characters to be removed. The default is whitespace characters: a space (“ “), a tab (\t), or a newline (\n). Note that the chars string is not a suffix. All combinations of its characters are stripped. |
A copy of s with trailing characters removed.
assert( text::rstrip( "abc " ) == "abc" ); assert( text::rstrip( "abc \t\n" ) == "abc" ); assert( text::rstrip( "aabbcc", "c" ) == "aabb" ); assert( text::rstrip( "aabbcc", "bc" ) == "aa" ); assert( text::rstrip( "aabbcc", "cb" ) == "aa" ); // "c"s and "b"s are stripped
static function string slice( string s, int start_pos = 0, int end_pos = - 1 )
static Returns a substring in the specified range. This function is similar to the substr function in native SystemVerilog, but one can specify negative numbers to specify the range with this function.
| s | An input string. |
| start_pos | optional Specifies the position in s to begin the substring. See Common Arguments. |
| end_pos | optional Specifies the position in s to end the substring. See Common Arguments. |
Returns a substring in the specified range.
assert( text::slice( "slice me", 2, 6 ) == "ice m" ); assert( text::slice( "slice me", -6, -2 ) == "ice m" );
static function string slice_len( string s, int start_pos = 0, int unsigned length = s.len() )
static Returns a substring in the specified range. Unlike slice, this function takes the length to extract instead of the end position.
| s | An input string. |
| start_pos | optional Specifies the position in s to begin the substring. See Common Arguments. |
| length | optional The number of characters to extract. The default is the length of s. |
Returns a substring in the specified range.
assert( text::slice_len( "slice me", 2, 5 ) == "ice m" ); assert( text::slice_len( "slice me", -6, 5 ) == "ice m" );
static function string_q split( string s, string sep = "", int max_split = -1 )
static Returns a queue of substrings by dividing the given string by the specified separator.
| s | An input string. If s is empty, an empty queue is returned. |
| sep | optional A separator. If specified, sep is used as the delimiter. The sep itself is not returned as an element of the queue. If not specified, whitespace characters (a space (“ “), a tab (\t), or a newline (\n)) are used. If sep is not specified, the leading whitespaces and the contiguous whitespaces are ignored. |
| max_split | optional If specified, at most max_split splits are done and the remaining substring is returned as the last element of the queue. If not specified or -1, there is no limit to the number of splits. |
A queue of substrings (string_q).
string_q s1, s2, s3, s4, t1, t2, t3, t4, t5;
s1 = '{ "abc", "pqr", "xyz" };
s2 = '{ "abc", "pqr xyz " };
s3 = '{ "abc", "pqr", "xyz " };
s4 = '{ "abc", "pqr", "xyz" };
assert( text::split( " abc pqr xyz " ) == s1 );
assert( text::split( " abc pqr xyz ", .max_split( 1 ) ) == s2 );
assert( text::split( " abc pqr xyz ", .max_split( 2 ) ) == s3 );
assert( text::split( " abc pqr xyz ", .max_split( 3 ) ) == s4 );
t1 = '{ "", "abc", "pqr", "xyz", "" };
t2 = '{ "", "abc--pqr--xyz--" };
t3 = '{ "", "abc", "pqr--xyz--" };
t4 = '{ "", "abc", "pqr", "xyz--" };
t5 = '{ "", "abc", "pqr", "xyz", "" };
assert( text::split( "--abc--pqr--xyz--", "--" ) == t1 );
assert( text::split( "--abc--pqr--xyz--", "--", .max_split( 1 ) ) == t2 );
assert( text::split( "--abc--pqr--xyz--", "--", .max_split( 2 ) ) == t3 );
assert( text::split( "--abc--pqr--xyz--", "--", .max_split( 3 ) ) == t4 );
assert( text::split( "--abc--pqr--xyz--", "--", .max_split( 4 ) ) == t5 );
static function bit starts_with( string s, string_q prefixes, int start_pos = 0, int end_pos = -1 )
static Returns 1 if the given string starts with one of the specified prefixes.
| s | An input string. |
| prefixes | A queue of prefix strings. The prefixes can be specified using an array literal. An empty string (“”) matches no input string. |
| start_pos | optional Specifies the position in s to begin the search. See Common Arguments. |
| end_pos | optional Specifies the position in s to end the search. See Common Arguments. |
If s starts with one of the specified prefixes, 1 is returned. Otherwise, 0 is returned.
assert( text::starts_with( "a primary library", { "a primary", "library" } ) == 1 );
assert( text::starts_with( "a primary library", { "a primary", "library" }, .start_pos( 10 ) ) == 1 );
// |---->|
// 10
assert( text::starts_with( "a primary library", { "a primary", "library" }, .end_pos( 7 ) ) == 0 );
// |----->|
// 7
assert( text::starts_with( "a primary library", { "a primary", "library" }, .end_pos( -9 ) ) == 1 );
// |------>|
// -9contains, contains_str, count, ends_with, find_any, index, only, rfind_any, rindex
static function string strip( string s, string chars = " \t\n" )
static Returns a copy of the given string with leading and trailing characters removed.
| s | A string to be stripped. |
| chars | optional A string specifying the set of characters to be removed. The default is whitespace characters: a space (“ “), a tab (\t), or a newline (\n). Note that the chars string is not a prefix or suffix. All combinations of its characters are stripped. |
A copy of s with leading and trailing characters removed.
assert( text::strip( " abc " ) == "abc" ); assert( text::strip( " \t\nabc\n" ) == "abc" ); assert( text::strip( "aabbcc", "a" ) == "bbcc" ); assert( text::strip( "aabbcc", "ab" ) == "cc" ); assert( text::strip( "aabbcc", "ac" ) == "bb" );
static function string swap_case( string s )
static Returns a copy of the given string with uppercase characters converted to lowercase, and lowercase characters converted to uppercase.
| s | A string to be swap-cased. |
A copy of s with uppercase characters converted to lowercase, and lowercase characters converted to uppercase.
assert( text::swap_case( "Swap Case Me!" ) == "sWAP cASE mE!" );
static function string title_case( string s )
static Returns a copy of the given string with the first character of words uppercased and the remainder lowercased.
| s | A string to be title-cased. |
A copy of s with the first character of words uppercased and the remainder lowercased.
assert( text::title_case( "title case me!" ) == "Title Case Me!" );
static function string trim( string s, int unsigned left = 0, int unsigned right = 0 )
static Returns a copy of the given string with the specified numbers of leading and trailing characters removed.
| s | A string to be trimmed. |
| left | optional The number of leading characters to remove. The default is 0. |
| right | optional The number of trailing characters to remove. The default is 0. |
A copy of s with leading left characters and trailing right characters removed.
assert( text::trim( "trim me!" ) == "trim me!" ); assert( text::trim( "trim me!", 1, 2 ) == "rim m" ); assert( text::trim( "trim me!", 3, 4 ) == "m" );
static function string uc_first( string s )
static Returns a copy of the given string with the first character uppercased and the remainder unchanged.
| s | An input string. |
A copy of s with the first character uppercased and the remainder unchanged.
assert( text::uc_first( "upper CASE first" ) == "Upper CASE first" );
static function string untabify( string s, int unsigned tab_size = 8 )
static Returns a copy of the given string where all tab characters (\t) are replaced by one or more spaces, depending on the tab positions. If a newline (\n) is found, it is copied and the tab position is reset.
| s | A string to untabify. |
| tab_size | optional Tab positions occur every tab_size characters. The default is 8. |
A copy of s where all tab characters are replaced by one or more spaces, depending on the tab positions.
assert( text::untabify( "AB\tCDE\tFGHI\tJKLMN" ) == "AB CDE FGHI JKLMN" );
// tab positions: ^ ^ ^ ^
assert( text::untabify( "AB\tCDE\tFGHI\tJKLMN", 4 ) == "AB CDE FGHI JKLMN" );
// tab positions: ^ ^ ^ ^ ^ ^
assert( text::untabify( "AB\nCDE\tFGHI\tJKLMN", 4 ) == "AB\nCDE FGHI JKLMN" );
// tab positions: ^ ^ ^ ^ ^ ^Provides utility functions for text processing.
virtual class text
static Returns a copy of the given string with the first character uppercased and the remainder lowercased.
static function string capitalize( string s )
static Returns a string of the specified width with the given string centerted and padded with the specified character.
static function string center( string s, int width, byte fill_char = " ", bit trim_ends = 0 )
static Returns a copy of the given string with the characters in the specified range replaced with the specified substring.
static function string change( string s, string sub, int start_pos = 0, int end_pos = -1 )
static Returns a copy of the given string with the last newline character removed (if present).
static function string chomp( string s )
static Returns the last character of the given string.
static function byte chop( string s )
static Returns a copy of the given string with ANSI escape codes.
static function string colorize( string s, fg_color_e fg = FG_BLACK, bg_color_e bg = BG_WHITE, bit bold = 0, bit underline = 0, bit blink = 0, bit reverse = 0 )
static Returns 1 if the given string contains the specified substring.
static function bit contains( string s, string sub, int start_pos = 0, int end_pos = -1 )
static If the given string contains the specified substring, returns that substring.
static function string contains_str( string s, string sub, int start_pos = 0, int end_pos = -1 )
static Returns the number of non-overlapping occurrences of the specified substring in the given string.
static function int unsigned count( string s, string sub, int start_pos = 0, int end_pos = -1 )
static Returns a copy of the given string with the specified string removed.
static function string delete( string s, string sub, int count = -1 )
static Returns 1 if the given string ends with one of the specified suffixes.
static function bit ends_with( string s, string_q suffixes, int start_pos = 0, int end_pos = -1 )
static Returns the lowest index in the given string where each specified substring is found.
static function int find_any( string s, string_q subs, int start_pos = 0, int end_pos = -1 )
static Returns the hash value of the given string.
static function int hash( string s )
static Returns the index of the first occurrence of the specified substring in the given string within the optionally specified range.
static function int index( string s, string sub, int start_pos = 0, int end_pos = -1 )
static Returns a copy of the given string with the specified substring inserted at the specified position.
static function string insert( string s, string sub, int start_pos = 0 )
static Returns 1 if all characters in the given string are alphabetic.
static function bit is_alpha( string s )
static Returns 1 if all characters in the given string are digits.
static function bit is_digit( string s )
static Returns 1 if all cased characters in the given string are lowercase.
static function bit is_lower( string s )
static Returns 1 if all characters in the given string are printable.
static function bit is_printable( string s )
static Returns 1 if the given string is bit, logic, or reg.
static function bit is_single_bit_type( string s )
static Returns 1 if all characters in the given string are whitespace characters: a space (“ “), a tab (\t), or a newline (\n).
static function bit is_space( string s )
static Returns 1 if all cased characters in the given string are uppercase.
static function bit is_upper( string s )
static Returns a string by concatenating the strings in the given string queue, separated by the specified separator.
static function string join_str( string_q strings, string separator = "" )
static Returns a copy of the given string with the first character lowercased and the remainder unchanged.
static function string lc_first( string s )
static Returns a string of the specified width with the given string left justified and padded with the specified character.
static function string ljust( string s, int width, byte fill_char = " ", bit trim_right = 0 )
static Returns a copy of the given string with leading characters removed.
static function string lstrip( string s, string chars = " \t\n" )
static Returns 1 if the given string consists of only the specified set of characters.
static function bit only( string s, string chars )
static Searches the first occurrence of the specified separator in the given string and returns an array of three strings.
static function three_strings partition( string s, string sep )
static Returns a copy of the given string with the specified string replaced with a new string.
static function string replace( string s, string old_str, string new_str, int count = -1 )
static Returns a copy of the given string with the characters in reverse order.
static function string reverse( string s )
static Returns the highest index in the given string where each specified substring is found.
static function int rfind_any( string s, string_q subs, int start_pos = 0, int end_pos = -1 )
static Returns the index of the last occurrence of the specified substring in the given string within the optionally specified range.
static function int rindex( string s, string sub, int start_pos = 0, int end_pos = -1 )
static Returns a string of the specified width with the given string right justified and padded with the specified character.
static function string rjust( string s, int width, byte fill_char = " ", bit trim_left = 0 )
static Searches the last occurrence of the specified separator in the given string and returns an array of three strings.
static function three_strings rpartition( string s, string sep )
static Returns a queue of substrings by dividing the given string by the specified separator from the right.
static function string_q rsplit( string s, string sep = "", int max_split = -1 )
static Returns a copy of the given string with trailing characters removed.
static function string rstrip( string s, string chars = " \t\n" )
static Returns a substring in the specified range.
static function string slice( string s, int start_pos = 0, int end_pos = - 1 )
static Returns a substring in the specified range.
static function string slice_len( string s, int start_pos = 0, int unsigned length = s.len() )
static Returns a queue of substrings by dividing the given string by the specified separator.
static function string_q split( string s, string sep = "", int max_split = -1 )
static Returns 1 if the given string starts with one of the specified prefixes.
static function bit starts_with( string s, string_q prefixes, int start_pos = 0, int end_pos = -1 )
static Returns a copy of the given string with leading and trailing characters removed.
static function string strip( string s, string chars = " \t\n" )
static Returns a copy of the given string with uppercase characters converted to lowercase, and lowercase characters converted to uppercase.
static function string swap_case( string s )
static Returns a copy of the given string with the first character of words uppercased and the remainder lowercased.
static function string title_case( string s )
static Returns a copy of the given string with the specified numbers of leading and trailing characters removed.
static function string trim( string s, int unsigned left = 0, int unsigned right = 0 )
static Returns a copy of the given string with the first character uppercased and the remainder unchanged.
static function string uc_first( string s )
static Returns a copy of the given string where all tab characters (\t) are replaced by one or more spaces, depending on the tab positions.
static function string untabify( string s, int unsigned tab_size = 8 )
The enumerated type of foreground colors.
typedef enum { FG_BLACK = 30, FG_RED = 31, FG_GREEN = 32, FG_YELLOW = 33, FG_BLUE = 34, FG_MAGENTA = 35, FG_CYAN = 36, FG_WHITE = 37 } fg_color_e
The enumerated type of background colors.
typedef enum { BG_BLACK = 40, BG_RED = 41, BG_GREEN = 42, BG_YELLOW = 43, BG_BLUE = 44, BG_MAGENTA = 45, BG_CYAN = 46, BG_WHITE = 47 } bg_color_e
The queue of strings.
typedef string string_q[$]