Description

utf8rewind is a system library written in C designed to extend the default string handling functions with support for UTF-8 encoded text.

Download

utf8rewind-1.3.0.zip (7.59 MB)

Clone in Mercurial

hg clone https://bitbucket.org/knight666/utf8rewind utf8rewind

Summary

In this release, we have implemented correct case mapping for Turkish, Azeri (Latin), Lithuanian and Greek text. These languages change the behavior of several code points when case mapping. The system locale is now used to determine if this exceptional behavior should be applied.

Besides this change in behavior, we have identified and resolved several performance bottlenecks, particularly when applying case mapping and when seeking in text.

Unfortunately, the performance issues with the utf8seek could only be resolved by modifying the function signature. We've outlined below how best to upgrade.

Upgrading from previous versions

Although the minor version number has changed, only utf8seek 's signature has been modified. Because of serious performance bottlenecks, the text length must now be specified as an explicit parameter. If your code currently relies on utf8seek , you must modify the calls to include this new parameter when upgrading.

Example:

\\ utf8rewind 1.2.1 const char* myTextOffset = utf8seek(myText, myText, 2, SEEK_CUR); \\ utf8rewind 1.3.0 size_t myTextLength = strlen(myText); const char* myTextOffset = utf8seek(myText, myTextLength, myText, 2, SEEK_CUR);

The new function signature allows you to cache the length of the text yourself, resulting in an astronomical increase in performance when seeking through a large body of text incrementally.

Bug fixes

Fix a major performance issue in utf8seek where incremental seeking would always have to determine the full length of the text before seeking.

Fix a bug in utf8seek where seeking backwards past the start of the string could result in erroneous output. If the current sequence consists of multiple bytes and the cursor is at the start of the text and more than one code point should be skipped, the output would not be the start of the string, as expected.

Fix a bug in utf8totitle where grapheme clusters, consisting of multiple code points would not be case mapped correctly. A grapheme cluster is a collection of code points that combine to form a single character. Each cluster starts with a code point that has a canonical combining class of 0. The function would toggle between lower and uppercase mappings on every code point, regardless if they were part of a cluster.

Implement case mapping for Turkish, Azeri (Latin), Lithuanian and Greek locales.

In-depth

Exceptional case mapping

Case mapping now takes the locale into account when processing the input. But what does this mean for a body of text?

Let's take Turkish text as an example. When working with Turkish text, the behavior of U+0069 LATIN SMALL LETTER I changes. When uppercasing, it turns into U+0130 LATIN CAPITAL LETTER I WITH DOT ABOVE instead of U+0049 LATIN CAPITAL LETTER I. When U+0049 LATIN CAPITAL LETTER I is lowercased, it turns into U+0131 LATIN SMALL LETTER DOTLESS I. Unless, of course, it is immediately followed by U+0307 COMBINING DOT ABOVE, in which case the dot above is removed and only U+0069 LATIN SMALL LETTER I is output.

As you can probably imagine, this took a lot of testing to get right. With this feature, we can finally claim that case mapping is fully compliant with the Unicode 7.0 standard. Just in time for the release of the 8.0 standard.

New testing categories

Besides unit and integration tests, utf8rewind now has a full suite of property and performance tests as well. Property testing came up time and again in discussions about the library, which is why we've given it a try for this release.

For performance testing, we've created a new framework based on Google Test. It allows the user to modify which tests to run and how many times a suite of tests should run. By default, the test output is written to the console, but a CSV file can be output as well.

Performance

Because we now have a full suite of performance testing, we were able to address critical performance issues in the library. This is how the seeking issue came to light, because it turned out seeking over a 5 MB text file incrementally took over eight hours! After caching the length parameter, seeking the same file now takes only five seconds.

One of the major performance gains in this release has been achieved by revisiting the way we store and retrieve Unicode properties. When case mapping and normalizing text, additional properties must be retrieved for each code point. These include the canonical combining class, the general category and the quick check properties. We stored these properties in a singe large array and utilized a binary search to retrieve them. Luckily, there is a more efficient to both store and retrieve these properties. Thanks to the book "Unicode Demystified" by Richard Gillam (ISBN13 9780201700527), we've been able to implement a compact array technique. This allows us to, for example, store the general category property for all 1.1 million Unicode code points in a 35,000 element index array and a 13,000 element data array. A compression ratio of 96%. Retrieving a property for a code point no longer requires a complex binary search, but only a bitshift and two array look-ups.

In the specific case of querying the general category, we've observed a speed-up from 38.8 ms to 1.4 ms to retrieve the general category for all possible code points.