Character classification

Chris Sherlock chris.sherlock79 at
Wed Apr 5 22:02:21 UTC 2017

> On 23 Mar 2017, at 10:47 pm, Stephan Bergmann <sbergman at> wrote:
> C character classification and case mapping functions in <ctype.h> are notoriously hard to use.


> I audited all uses of such functions (outside of external/ source blobs) on recent master:
> In C++ code, I replaced them with calls to corresponding rtl/character.hxx functions (and adding missing casts from char to unsigned char where necessary; also see <> "Prevent calls to rtl/character.hxx functions with (signed) char arguments").  A function corresponding to isspace was missing and has been added with <> "Use rtl::isAscii* instead of ctype.h is* (and fix passing plain char) and add rtl::isAsciiWhiteSpace".  Any calls thus replaced appeared to either never pass in EOF or have been adapted accordingly (<> "Fix some usage of std::istream unformatted input in hwpfilter/source/hwpeq.cxx").  (And there had been no uses of the corresponding std-namespaced functions from <cctype>

Nice work! When I get to the point where I write about the RTL I'll make sure I note this in the part I write about string handling.

> What remains is the source of the five C programs
>  rsc/ (rsc/source/rscpp/cpp{2,3,5,6}.c)
>  shell/ (shell/source/unix/misc/uri-encode.c)
>  solenv/ (solenv/bin/concat-deps.c)
>  soltools/ (soltools/cpp/_{tokens,unix}.c)
>  soltools/ (soltools/mkdepend/{cppsetup,ifparser,parse}.c)
> For one, I have added any casts from char to unsigned char where missing.  (But note that in some cases the input already was of the expected form.)

So the recommendation is to avoid C string functions in LibreOffice code in future?

I realise this may be a silly question, but does this mean we have a portable, cross-culture string handling module that makes things like character case handling consistent across platforms?

If so, I've always wondered what effort it would take to hive off our string handling code to its own project - there is a lot of thought and decades worth of testing for this C and C++ code. Perhaps it might help other projects outside of LibreOffice in the future...

> For another, with a recent set of commits to master I have removed all but one call to setlocale from the LO code base itself.  (The remaining one is in SetSystemLocale in vcl/unx/generic/app/i18n_im.cxx, and smells like it is necessary for proper IME support in VCL-based applications on Linux.  None of those five C programs should be affected by it.)  So barring any calls to setlocale in external code, and ignoring the somewhat fuzzy definition of isprint as called from rsc/source/rscpp/cpp{5,6}.c, those five C programs should not (any longer) be affected by locale issues.

Was this done because of the character casing challenges mentioned above? Or was calling on this causing problems elsewhere?

I'm assuming setting locales is frowned on... 

> Note that there are three remaining calls to toupper/tolower that are currently being addressed by <> "tdf#99589 change tolower/toupper to easytolower/upper."
> Please avoid adding any new uses of any of those functions.

Nice work! Please pardon me if any of my questions are silly, I am relatively inexperienced with topics related to localization.


More information about the LibreOffice mailing list