Internationalization and localization tools


Locale-Sensitive Length Function

char *strndupa(const char *s, size_t size);

Internationalization (I18n) Function Overview

This function is similar to strndup but like strdupa it allocates the new string using alloca. The same advantages and limitations of strdupa are valid for strndupa.

I18n Issues

Note that while the documentation says strndupa copies size characters it actually copies size bytes. This makes this function locale-sensitive in UTF-8 multibyte environments. For example, consider the simple case of a string which consists of a single multibyte character which contains 2 bytes. If stpncpy is used with a parameter of 1, only half of the multibyte character will be copied, resulting in an ill-formed string.

Additionally, there is no ANSI wide character version of this function.

These two issues combined mean that there is no suitable substitution for strndupa that is readily available that supports either multibyte or wide character strings.

Note that since this function is simply a alloca followed by a strncpy, you may consider doing the alloca yourself and then calling strncpy. Also note that strncpy has its own issues however, and the documentation should be consulted.

Recommended Replacements*

*If you're already using the recommended function, see I18n Issues for other reasons why Globalyzer is detecting the function.

Locale-Sensitive Length Functions

 

Lingoport internationalization and localization services and software