GNU TeXmacsUser manualReferenceAboutEnglish
String utilities and pitfalls

1.Byte level utilities

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.

Character tests

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.

Reading

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/.

Searching and replacing

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, …

Quoting

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).

Escaping for other programs

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.

Numbers

roman_nr, Roman_nr, alpha_nr, Alpha_nr, fnsymbol_nr (used for numbering), and the hexadecimal routines of the previous page.

Sets of characters

string_union and string_minus treat a string as a set of bytes.

Completions and differences

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).

Mathematics

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.

Indexed search.
string_searcher (
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.

Sorting.
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.

2.Pitfalls

Mixing encodings.
Nothing in the type system distinguishes a Cork string from a UTF-8 string or a file name. The most common bugs are:
Line breaks and control characters.
In the Cork table the bytes 0x09, 0x0A and 0x0D are glyphs (macron, dot accent and a low quote). cork_to_utf8 therefore turns a tab into U+00AF and a newline into U+02D9 (verified: (cork->utf8 "a\nb") gives an a, a dot accent and a b): it is the converter of document text, whose strings hold no line break. Plain text (widget text, commit messages, the verbatim export in
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).

3.Known bugs

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.

  1. 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.

  2. D with stroke (0x9E) is unaccented to a lower case d (verified;

    universal.cpp:400

    ).

  3. 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.

  4. 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: &#x2013; is left unchanged).

  5. 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).

  6. 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.

  7. 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

    ).

  8. 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.

  9. 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.

  10. 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).

  11. 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

    ).

  12. from_qstring_utf8 builds the result from a C string (

    Plugins/Qt/qt_utilities.cpp:346

    ), so text containing a NUL character is truncated.

  13. The combining tilde is recognized as U+033E (combining vertical tilde) instead of U+0303 (

    Plugins/Qt/QTMKeyboardEvent.cpp:137

    ); probably a typo.

  14. 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.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".