(1.1K lines)
4 Converts a `Text` value to a C-style string.8 A C-style string (`CString`) representing the text.13 The text to be converted to a C-style string.15 assert "Hello".as_c_string() == CString("Hello")20 Get the graphical cluster at a given index. This is similar to `str[i]` with21 ASCII text, but has more correct behavior for unicode text.25 A `Text` with the single graphical cluster at the given index.27 Negative indices are counted from the back of the text, so `-1` means the28 last cluster, `-2` means the second-to-last, and so on.33 The text from which to get a cluster.37 The index of the graphical cluster (1-indexed).39 assert "Amélie".at(3) == "é"44 Returns an iterator function that can be used to iterate over the lines in a45 text.49 An iterator function that returns one line at a time, until it runs out and50 returns `none`.52 This function ignores a trailing newline if there is one. If you don't want53 this behavior, use `text.by_split($/{1 nl}/)` instead.58 The text to be iterated over, line by line.60 text := "61 line one62 line two63 "64 lines := [line for line in text.by_line()]65 assert lines == ["line one", "line two"]70 Returns an iterator function that can be used to iterate over text separated by71 a delimiter.75 An iterator function that returns one chunk of text at a time, separated by the76 given delimiter, until it runs out and returns `none`.81 The text to be iterated over in delimited chunks.86 An exact delimiter to use for splitting the text.88 To split based on a set of delimiters, use Text.by_split_any().90 If an empty text is given as the delimiter, then each split will be the91 graphical clusters of the text.93 text := "one,two,three"94 chunks := [chunk for chunk in text.by_split(",")]95 assert chunks == ["one", "two", "three"]100 Returns an iterator function that can be used to iterate over text separated by101 one or more characters (grapheme clusters) from a given text of delimiters.105 An iterator function that returns one chunk of text at a time, separated by the106 given delimiter characters, until it runs out and returns `none`.111 The text to be iterated over in delimited chunks.116 Grapheme clusters to use for splitting the text.118 Splitting will occur on every place where one or more of the grapheme119 clusters in `delimiters` occurs.121 To split based on an exact delimiter, use Text.by_split().123 text := "one,two,;,three"124 chunks := [chunk for chunk in text.by_split_any(",;")]125 assert chunks == ["one", "two", "three"]130 Converts a `Text` value to a list of bytes representing a UTF8 encoding of131 the text.135 A list of bytes (`[Byte]`) representing the text in UTF8 encoding.140 The text to be converted to UTF8 bytes.142 assert "Amélie".utf8() == [65, 109, 195, 169, 108, 105, 101]147 Checks whether two texts are equal, ignoring the casing of the letters (i.e.148 case-insensitive comparison).152 `yes` if `a` and `b` are equal to each other, ignoring casing, otherwise `no`.157 The first text to compare case-insensitively.161 The second text to compare case-insensitively.166 The ISO 639 language code for which casing rules to use.168 assert "A".caseless_equals("a") == yes170 # Turkish lowercase "I" is "ı" (dotless I), not "i"171 assert "I".caseless_equals("i", language="tr_TR") == no176 Returns a list of the names of each codepoint in the text.180 A list of codepoint names (`[Text]`).185 The text from which to extract codepoint names.187 assert "Amélie".codepoint_names() == [188 "LATIN CAPITAL LETTER A",189 "LATIN SMALL LETTER M",190 "LATIN SMALL LETTER E WITH ACUTE",191 "LATIN SMALL LETTER L",192 "LATIN SMALL LETTER I",193 "LATIN SMALL LETTER E",194 ]199 Checks if the `Text` ends with a literal suffix text.203 `yes` if the text has the target, `no` otherwise.208 The text to be searched.212 The literal suffix text to check for.217 If non-none, this value will be set to the rest of the text up to the trailing suffix.218 If the suffix is not found, this value will be set to the original text.220 assert "hello world".ends_with("world") == yes221 remainder : Text222 assert "hello world".ends_with("world", &remainder) == yes223 assert remainder == "hello "228 Get an approximate distance between two texts, such that when the distance229 is small, the texts are similar and when the distance is large, the texts230 are dissimilar.232 The exact distance algorithm is not specified and may be subject to change233 over time.237 The distance between the two texts (larger means more dissimilar).242 The first text to compare.246 The second text to compare.251 The ISO 639 language code for which character width to use.253 assert "hello".distance("hello") == 0254 texts := &["goodbye", "hello", "hallo"]255 texts.sort(func(a,b:&Text) a.distance("hello") <> b.distance("hello"))256 assert texts == ["hello", "hallo", "goodbye"]261 Find a substring within a text and return its index, if found.265 The index where the first occurrence of `target` appears, or `none` if it is not found.270 The text to be searched.274 The target text to find.279 The index at which to begin searching.281 assert "one two".find("one") == 1282 assert "one two".find("two") == 5283 assert "one two".find("three") == none284 assert "one two".find("o", start=2) == 7289 Get a slice of the text, starting at the given position.293 The text from the given grapheme cluster to the end of the text.295 A negative index counts backwards from the end of the text, so `-1` refers296 to the last cluster, `-2` the second-to-last, etc. Slice ranges will be297 truncated to the length of the text.302 The text to be sliced.306 The index to begin the slice.308 assert "hello".from(2) == "ello"309 assert "hello".from(-2) == "lo"314 Returns text that has been constructed from the given UTF8 bytes.316 The text will be normalized, so the resulting text's UTF8 bytes may not317 exactly match the input.321 A new text based on the input UTF8 bytes after normalization has been applied.326 The UTF-8 bytes of the desired text.328 assert Text.from_utf8([195, 133, 107, 101]) == "Åke"333 Returns text that has been constructed from the given UTF16 sequence.335 The text will be normalized, so the resulting text's UTF16 sequence may not336 exactly match the input.340 A new text based on the input UTF16 sequence after normalization has been applied.345 The UTF-16 integers of the desired text.347 assert Text.from_utf16([197, 107, 101]) == "Åke"348 assert Text.from_utf16([12371, 12435, 12395, 12385, 12399, 19990, 30028]) == "こんにちは世界"353 Converts a C-style string to a `Text` value.357 A `Text` value representing the C-style string.362 The C-style string to be converted.364 assert Text.from_c_string(CString("Hello")) == "Hello"369 Returns text that has the given codepoint names (according to the Unicode370 specification) as its codepoints.372 The text will be normalized, so the resulting text's codepoints may not373 exactly match the input codepoints.377 A new text with the specified codepoints after normalization has been applied.378 Any invalid names are ignored.383 The names of each codepoint in the desired text (case-insentive).385 text := Text.from_codepoint_names([386 "LATIN CAPITAL LETTER A WITH RING ABOVE",387 "LATIN SMALL LETTER K",388 "LATIN SMALL LETTER E",389 ])390 assert text == "Åke"395 Returns text that has been constructed from the given UTF32 codepoints.397 The text will be normalized, so the resulting text's codepoints may not398 exactly match the input codepoints.402 A new text with the specified codepoints after normalization has been applied.407 The UTF32 codepoints in the desired text.409 assert Text.from_utf32([197, 107, 101]) == "Åke"414 Checks if the `Text` contains some target text.418 `yes` if the target text is found, `no` otherwise.423 The text to be searched.427 The text to search for.429 assert "hello world".has("wo") == yes430 assert "hello world".has("xxx") == no435 Joins a list of text pieces with a specified glue.439 A single `Text` value with the pieces joined by the glue.444 The text used to join the pieces.448 The list of text pieces to be joined.450 assert ", ".join(["one", "two", "three"]) == "one, two, three"455 Return whether or not the text matches the given glob.459 Whether or not the text matches the given glob.464 The text to check.468 The glob pattern to check.470 assert "hello world".matches_glob("h* *d")475 Pad some text on the left and right side so it reaches a target width.479 Text with length at least `width`, with extra padding on the left and right as480 needed. If `pad` has length greater than 1, it may be partially repeated to481 reach the exact desired length.486 The text to pad.490 The target width.495 The padding text.500 The ISO 639 language code for which character width to use.502 assert "x".middle_pad(6) == " x "503 assert "x".middle_pad(10, "ABC") == "ABCAxABCAB"508 Pad some text on the left side so it reaches a target width.512 Text with length at least `width`, with extra padding on the left as needed. If513 `pad` has length greater than 1, it may be partially repeated to reach the514 exact desired length.519 The text to pad.523 The target width.528 The padding text.533 The ISO 639 language code for which character width to use.535 assert "x".left_pad(5) == " x"536 assert "x".left_pad(5, "ABC") == "ABCAx"541 Splits the text into a list of lines of text, preserving blank lines,542 ignoring trailing newlines, and handling `\r\n` the same as `\n`.546 A list of substrings resulting from the split.551 The text to be split into lines.553 assert "one\ntwo\nthree".lines() == ["one", "two", "three"]554 assert "one\ntwo\nthree\n".lines() == ["one", "two", "three"]555 assert "one\ntwo\nthree\n\n".lines() == ["one", "two", "three", ""]556 assert "one\r\ntwo\r\nthree\r\n".lines() == ["one", "two", "three"]557 assert "".lines() == []562 Converts all characters in the text to lowercase.566 The lowercase version of the text.571 The text to be converted to lowercase.576 The ISO 639 language code for which casing rules to use.578 assert "AMÉLIE".lower() == "amélie"579 assert "I".lower(language="tr_TR") == "ı"584 Formats the text with quotation marks and escapes.588 The text formatted as a quoted text.593 The text to be quoted.598 Whether to add color formatting.603 The quotation mark to use.605 assert "one\ntwo".quoted() == "\"one\\ntwo\""610 Repeat some text multiple times.614 The text repeated the given number of times.619 The text to repeat.623 The number of times to repeat it. (Negative numbers are equivalent to zero).625 assert "Abc".repeat(3) == "AbcAbcAbc"630 Replaces occurrences of a target text with a replacement text.634 The text with occurrences of the target replaced.639 The text in which to perform replacements.643 The target text to be replaced.647 The text to replace the target with.649 assert "Hello world".replace("world", "there") == "Hello there"654 Return a text that has the grapheme clusters in reverse order.658 A reversed version of the text.663 The text to reverse.665 assert "Abc".reversed() == "cbA"670 Pad some text on the right side so it reaches a target width.674 Text with length at least `width`, with extra padding on the right as needed. If675 `pad` has length greater than 1, it may be partially repeated to reach the676 exact desired length.681 The text to pad.685 The target width.690 The padding text.695 The ISO 639 language code for which character width to use.697 assert "x".right_pad(5) == "x "698 assert "x".right_pad(5, "ABC") == "xABCA"703 Get a slice of the text.707 The text that spans the given grapheme cluster indices.709 A negative index counts backwards from the end of the text, so `-1` refers710 to the last cluster, `-2` the second-to-last, etc. Slice ranges will be711 truncated to the length of the text.716 The text to be sliced.721 The index of the first grapheme cluster to include (1-indexed).726 The index of the last grapheme cluster to include (1-indexed).728 assert "hello".slice(2, 3) == "el"729 assert "hello".slice(to=-2) == "hell"730 assert "hello".slice(from=2) == "ello"735 Splits the text into a list of substrings based on exact matches of a delimiter.739 A list of subtexts resulting from the split.744 The text to be split.749 The delimiter used to split the text.751 To split based on a set of delimiters, use Text.split_any().753 If an empty text is given as the delimiter, then each split will be the754 graphical clusters of the text.757 assert "one,two,,three".split(",") == ["one", "two", "", "three"]758 assert "abc".split() == ["a", "b", "c"]763 Splits the text into a list of substrings at one or more occurrences of a set764 of delimiter characters (grapheme clusters).768 A list of subtexts resulting from the split.773 The text to be split.778 A text containing delimiters to use for splitting the text.780 Splitting will occur on every place where one or more of the grapheme781 clusters in `delimiters` occurs.783 To split based on an exact delimiter, use Text.split().785 assert "one, two,,three".split_any(", ") == ["one", "two", "three"]790 Checks if the `Text` starts with a literal prefix text.794 `yes` if the text has the given prefix, `no` otherwise.799 The text to be searched.803 The literal prefix text to check for.808 If non-none, this value will be set to the rest of the text after the prefix.809 If the prefix is not found, this value will be set to the original text.811 assert "hello world".starts_with("hello") == yes812 remainder : Text813 assert "hello world".starts_with("hello", &remainder) == yes814 assert remainder == " world"819 Converts the text to title case (capitalizing the first letter of each word).823 The text in title case.828 The text to be converted to title case.833 The ISO 639 language code for which casing rules to use.835 assert "amélie".title() == "Amélie"837 # In Turkish, uppercase "i" is "İ"838 assert "i".title(language="tr_TR") == "İ"843 Get a slice of the text, ending at the given position.847 The text up to and including the given grapheme cluster.849 A negative index counts backwards from the end of the text, so `-1` refers850 to the last cluster, `-2` the second-to-last, etc. Slice ranges will be851 truncated to the length of the text.856 The text to be sliced.860 The index of the last grapheme cluster to include (1-indexed).862 assert "goodbye".to(3) == "goo"863 assert "goodbye".to(-2) == "goodby"868 Takes a table mapping target texts to their replacements and performs all the869 replacements in the table on the whole text. At each position, the first870 matching replacement is applied and the matching moves on to *after* the871 replacement text, so replacement text is not recursively modified. See872 Text.replace() for more information about replacement behavior.876 The text with all occurrences of the targets replaced with their corresponding877 replacement text.882 The text to be translated.886 A table mapping from target text to its replacement.888 text := "A <tag> & an ampersand".translate({889 "&": "&",890 "<": "<",891 ">": ">",892 '"': """,893 "'": "'",894 })895 assert text == "A <tag> & an ampersand"900 Trims the given characters (grapheme clusters) from the left and/or right side of the text.904 The text without the trim characters at either end.909 The text to be trimmed.914 The characters to remove from the left/right of the text.919 Whether or not to trim from the front of the text.924 Whether or not to trim from the back of the text.926 assert " x y z \n".trim() == "x y z"927 assert "one,".trim(",") == "one"928 assert " xyz ".trim(right=no) == "xyz "933 Converts all characters in the text to uppercase.937 The uppercase version of the text.942 The text to be converted to uppercase.947 The ISO 639 language code for which casing rules to use.949 assert "amélie".upper() == "AMÉLIE"951 # In Turkish, uppercase "i" is "İ"952 assert "i".upper(language="tr_TR") == "İ"957 Returns a list of Unicode code points for UTF16 encoding of the text.961 A list of 16-bit integer Unicode code points (`[Int16]`).966 The text from which to extract Unicode code points.968 assert "Åke".utf16() == [197, 107, 101]969 assert "こんにちは世界".utf16() == [12371, 12435, 12395, 12385, 12399, 19990, 30028]974 Returns a list of Unicode code points for UTF32 encoding of the text.978 A list of 32-bit integer Unicode code points (`[Int32]`).983 The text from which to extract Unicode code points.985 assert "Amélie".utf32() == [65, 109, 233, 108, 105, 101]990 Returns the display width of the text as seen in a terminal with appropriate991 font rendering. This is usually the same as the text's `.length`, but there are992 some characters like emojis that render wider than 1 cell.994 This will not always be exactly accurate when your terminal's font995 rendering can't handle some unicode displaying correctly.999 An integer representing the display width of the text.1004 The text whose length you want.1006 assert "Amélie".width() == 61007 assert "🤠".width() == 21012 Returns the text with a given prefix removed (if present).1016 A text without the given prefix (if present) or the unmodified text if the1017 prefix is not present.1022 The text to remove the prefix from.1026 The prefix to remove.1028 assert "foo:baz".without_prefix("foo:") == "baz"1029 assert "qux".without_prefix("foo:") == "qux"1034 Returns the text with a given suffix removed (if present).1038 A text without the given suffix (if present) or the unmodified text if the1039 suffix is not present.1044 The text to remove the suffix from.1048 The suffix to remove.1050 assert "baz.foo".without_suffix(".foo") == "baz"1051 assert "qux".without_suffix(".foo") == "qux"