![]() |
String utilities and pitfalls | ![]() |
Besides the routines of the previous pages,
Data/String/analyze.cpp
collects general purpose functions on byte strings. Unless stated otherwise they ignore the universal encoding, so a symbol counts as several characters and a search may match inside a symbol; use the tm_* variants when this matters.
is_alpha, is_digit, is_hex_digit, is_space, is_punctuation, … on ASCII characters (inline in
analyze.hpp
), and is_iso_alpha, is_iso_locase, is_iso_upcase which also accept Cork letters.
test (s, i, what), starts, ends, read (s, i, what), read_line, read_int, read_double, read_word, skip_spaces, skip_whitespace, skip_line, skip_symbol, is_whitespace, and parse for binary integers. These are the building blocks of the hand written parsers in Data/Convert/.
search_forwards, search_backwards, occurs, count_occurrences (which counts overlapping occurrences), overlapping, replace, match_wildcard (only * is special), tokenize and recompose, trim_spaces and its left and right variants (also on trees and arrays), find_non_alpha. Scheme: string-search-forwards, string-replace, string-count-occurrences, cpp-string-tokenize, …
scm_quote and scm_unquote (Scheme string syntax with \\ and \" escapes; string-quote), raw_quote and raw_unquote (only add or remove the double quotes, used to distinguish strings from symbols in trees), unescape_guile (\xhh escapes).
escape_sh (shell; on Windows it only adds double quotes), escape_generic (the escapes of the plug-in protocol, see the plug-in machinery), escape_verbatim (removes control characters and turns tabs and newlines into spaces), escape_spaces, dos_to_better (removes carriage returns), convert_tabs_to_spaces.
roman_nr, Roman_nr, alpha_nr, Alpha_nr, fnsymbol_nr (used for numbering), and the hexadecimal routines of the previous page.
string_union and string_minus treat a string as a set of bytes.
as_completions, close_completions, strip_completions (used by tab-completion,
Edit/Interface/edit_complete.cpp
); differences (s1, s2), which returns the differing ranges as quadruples \((b_1, e_1, b_2, e_2)\) found by recursively matching common substrings, and distance, which sums the lengths of these ranges (an approximation of an edit distance; string-differences).
downgrade_math_letters turns <b-x>, <cal-x> and similar into plain letters; find_left_bracket and find_right_bracket look for matching brackets in the edit tree.
Data/String/fast_search.hpp
) builds, for a fixed string, a table of hash codes of all substrings whose length is a power of two, and uses it to find the occurrences of a pattern quickly (search_next, search_all). get_longest_common (s1, s2, …) uses two such tables to find a longest common substring. Both are used by the conservative LaTeX converters to recognize the parts of a document which did not change.
Data/String/merge_sort.hpp
provides merge_sort (a) for arrays of any type with an <= operator and merge_sort_leq<T,LEQ> (a) with an explicit comparison class. The sort is stable.
Raw UTF-8 in trees: text from the outside (a file, the clipboard, an external program, a Scheme string literal in a UTF-8 source file) inserted without utf8_to_cork shows up as several wrong Cork characters. .tm files must not contain bytes above 127 other than Cork characters.
Converting twice, or converting a string which is already UTF-8: cork_to_utf8 applied to UTF-8 produces mojibake. to_qstring tries to avoid this by guessing, which in turn can misinterpret a Cork string that happens to be valid UTF-8.
Counting bytes instead of characters: N (s), string-length and substring count bytes, so they split symbols; use tm_string_length, tmstring-length and the other tm_* routines.
Entities versus characters: the same character may be present as a Cork byte, as a symbol name and as a Unicode escape (for instance an e with acute accent is the byte 0xE9, not <#E9>). utf8_to_cork always produces the Cork byte when there is one, but strings built by hand may not, and comparisons of such strings fail.
Data/Convert/Verbatim/verbatim.cpp
) is converted by cork_text_to_utf8 (cork-text->utf8, or the source Cork-text of string-convert), which keeps 0x09 and 0x0A as a tab and a newline. In the other direction, utf8_to_cork keeps tabs and newlines as bytes, so the two functions are not inverse to each other on such strings. Code points U+0080–U+009F (the C1 control characters) have no table entry and are left as raw UTF-8 by utf8_to_cork (verified).
The following problems were found while writing this chapter. Items marked “verified” were reproduced with the Scheme glue; the others are from reading the code.
Wrong case of four Cork characters (verified). uni_locase_char lowers the whole range 0x80–0x9F, including the section sign 0x9F, which becomes the pound sign; uni_upcase_char raises 0xA0–0xBF, including the inverted exclamation and question marks and the pound sign, which become an I with dot, a D with stroke and the section sign (
Data/String/universal.cpp:199
, :250). This affects tmstring-upcase-all and friends, and hence capitalization in bibliographies and titles. The byte level routines of
analyze.cpp
exclude these positions correctly.
D with stroke (0x9E) is unaccented to a lower case d (verified;
universal.cpp:400
).
uni_is_letter considers every byte above 127 a letter, including the section, pound and inverted punctuation signs (verified): the test (c & 97) != 31 at
universal.cpp:460
is always true.
html_to_utf8 only decodes hexadecimal entities between 0x80 and 0xFF (
converter.cpp:924
), so it does not invert utf8_to_html, which encodes all non-ASCII characters (verified: – is left unchanged).
An unterminated <# at the end of a string makes cork_to_utf8 (and its variants) output a NUL byte (verified;
converter.cpp:379
and the analogous lines).
decode_from_utf8 on a sequence truncated at the end of the string reads the last byte again (
converter.cpp:880
), so a wrong code point is produced instead of the failsafe byte.
decode_base64 drops a final group without padding (verified: "QUI" decodes to the empty string), indexes its table with a negative value for bytes above 127 (
base64.cpp:88
), and decodes an empty group, reading past the end of an empty array, when = follows a complete group (
base64.cpp:69
).
looks_universal only accepts decimal digits in <#…> (
wencoding.cpp:99
), so western_to_cork escapes ASCII text containing an escape such as <#E9> with tm_encode.
The iso-8859-1 setting for plain text import and export applies tm_encode and tm_decode without converting bytes (
Data/Convert/Verbatim/verbatim.cpp:247
, :282), as if Cork were ISO-8859-1; characters in the range 0x80–0xBF and the four differing positions are mangled, and on export tm_decode drops all symbols. Since export with auto uses the character set of the locale, this also happens on systems with a Latin-1 locale.
replace and tokenize loop forever when the pattern or separator is empty (
analyze.cpp:1263
, :1311); both are reachable from Scheme (string-replace, cpp-string-tokenize).
string_searcher_rep::search_sub indexes its table out of range when the pattern is longer than the indexed string, or when the indexed string is empty (
fast_search.cpp:90
).
from_qstring_utf8 builds the result from a C string (
Plugins/Qt/qt_utilities.cpp:346
), so text containing a NUL character is truncated.
The combining tilde is recognized as U+033E (combining vertical tilde) instead of U+0303 (
Plugins/Qt/QTMKeyboardEvent.cpp:137
); probably a typo.
The iconv wrapper returns the whole input unchanged when a conversion fails, and tm_decode silently drops symbols and Unicode escapes which do not have exactly four digits; both lose information without telling the caller.