Jump to content

C string handling: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
→‎Functions: add missing multibyte functions
Line 74: Line 74:
| transforms a string according to the current locale
| transforms a string according to the current locale
|-
|-
! rowspan=11 | String examination
! rowspan=11 | String<br> examination
| {{anchor|strlen}}<code>[http://en.cppreference.com/w/cpp/string/byte/strlen strlen]</code>
| {{anchor|strlen}}<code>[http://en.cppreference.com/w/cpp/string/byte/strlen strlen]</code>
| {{anchor|wcslen}}<code>[http://en.cppreference.com/w/cpp/string/wide/wcslen wcslen]</code>
| {{anchor|wcslen}}<code>[http://en.cppreference.com/w/cpp/string/wide/wcslen wcslen]</code>
Line 148: Line 148:
|}
|}


; Multibyte Functions
====Multibyte functions====

* {{anchor|mbtowc}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/mbtowc mbtowc]</code> - converts the first multibyte character in a string to the matching wide character
{| class="wikitable" style="font-size:0.85em"
* {{anchor|wctomb}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/wctomb wctomb]</code> - converts a wide character to the matching multibyte character
|-
* {{anchor|mblen}}<code>[http://c-p-p.net/c/stdlib.h/mblen mblen]</code> - Determines how many bytes are used for a character.
! Name
! Description
|-
| {{anchor|mblen}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/mblen mblen]</code>
| returns the number of bytes in the next multibyte character
|-
| {{anchor|mbtowc}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/mbtowc mbtowc]</code>
| converts the first multibyte character in a string to the matching wide character
|-
| {{anchor|wctomb}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/wctomb wctomb]</code>
| converts a wide character to the matching multibyte character
|-
| {{anchor|mbstowcs}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/mbstowcs mbstowcs]</code>
| converts a narrow multibyte string to a wide string
|-
| {{anchor|wcstombs}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/wcstombs wcstombs]</code>
| converts a wide character string to a narrow multibyte string
|-
| {{anchor|mbsinit}}<code>[http://en.cppreference.com/w/cpp/string/mbsinit/ mbsinit]</code>
| checks if a mbstate_t object represents initial shift state
|-
| {{anchor|btowc}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/btowc btowc]</code>
| widens a single-byte character to wide character, if possible
|-
| {{anchor|wctob}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/wctob wctob]</code>
| narrows a wide character to a single-byte character, if possible
|-
| {{anchor|mbrlen}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/mbrlen mbrlen]</code>
| returns the number of bytes in the next multibyte character, given state
|-
| {{anchor|mbrtowc}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/mbrtowc mbrtowc]</code>
| converts the next multibyte character to wide character, given state
|-
| {{anchor|wcrtomb}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/wcrtomb wcrtomb]</code>
| converts a wide character to its multibyte representation, given state
|-
| {{anchor|mbsrtowcs}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/mbsrtowcs mbsrtowcs]</code>
| converts a narrow multibyte character string to wide string, given state
|-
| {{anchor|wcsrtombs}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/wcsrtombs wcsrtombs]</code>
| converts a wide string to narrow multibyte character string, given state
|-
| {{anchor|mbrtoc16}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/mbrtoc16 mbrtoc16]</code> ([[C99]])
| generate the next 16-bit wide character from a narrow multibyte string
|-
| {{anchor|c16rtomb}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/c16rtomb c16rtomb]</code> ([[C99]])
| convert a 16-bit wide character to narrow multibyte string
|-
| {{anchor|mbrtoc32}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/mbrtoc32 mbrtoc32]</code> ([[C99]])
| generate the next 32-bit wide character from a narrow multibyte string
|-
| {{anchor|c32rtomb}}<code>[http://en.cppreference.com/w/cpp/string/multibyte/c32rtomb c32rtomb]</code> ([[C99]])
| convert a 32-bit wide character to narrow multibyte string
|}


===Numeric conversions===
===Numeric conversions===

Revision as of 13:18, 31 March 2012

C string handling refers to a group of functions implementing operations on strings in the C standard library. Various operations, such as copying, concatenation, tokenization and searching are supported.

The only support in the C programming language itself for strings is that the compiler will translate a quoted string constant in the source into a null-terminated string stored in static memory.

However the standard C library provides a large number of functions designed to manipulate these null-terminated strings. These functions are so popular and used so often that they are usually considered part of the definition of C.

Definitions

A string is a contiguous sequence of characters terminated by and including the first null character (written '\0' and corresponding to the ASCII character NUL). In C, there are two types of strings: string, which is sometimes called byte string, and wide string.[1] A byte string contains the type chars as code units (one char is one byte), whereas a wide string contains the type wchar_t as code units.

A common misconception is that all char arrays are strings, because string literals are converted to arrays during the compilation (or translation) phase.[2] It is important to remember that a string ends at the first null character. An array or string literal that contains a null character before the last byte therefore contains a string, or possibly several strings, but is not itself a string.[3] Conversely, it is possible to create a char array that is not null-terminated and is thus not a string. char is often used as integer when needing to save memory, for example when having an array of booleans.

The term pointer to a string is used in C to describe a pointer to the initial (lowest-addressed) byte of a string.[1] In C, pointers are used to pass strings to functions. Documentation (including this page) will often use the term string to mean pointer to a string.

The term length of a string is used in C to describe the number of bytes preceding the null character.[1] strlen is a standardised function commonly used to determine the length of a string.

Character encodings

Each string ends at the first occurrence of the null character of the appropriate kind (char or wchar_t). A null character is a character represented as a zero. Consequently, a byte string can contain non-NUL characters in ASCII or any ASCII extension, but not characters in encodings such as UTF-16 (even though a 16-bit code unit might be nonzero, its high or low byte might be zero). The encodings that can be stored in wide strings are defined by the width of wchar_t. In most implementations, wchar_t is at least 16 bits, and so all 16-bit encodings, such as UCS-2, can be stored. If wchar_t is 32-bits, then 32-bit encodings, such as UTF-32, can be stored.

Variable-width encodings can be used in both byte strings and wide strings. String length and offsets are measured in bytes or wchar_t, not in "characters", which can be confusing to beginning programmers. UTF-8 and Shift JIS are often used in C byte strings, while UTF-16 is often used in C wide strings when wchar_t is 16 bits. Truncating strings with variable length characters using functions like strncpy can produce invalid sequences at the end of the string. This can be unsafe if the truncated parts are interpreted by code that assumes the input is valid.

Support for Unicode literals such as char foo[512] = "φωωβαρ";(UTF-8) or wchar_t foo[512] = L"φωωβαρ"; (UTF-16 or UTF-32) is implementation defined,[4] and may require that the source code be in the same encoding. Some compilers or editors will require entering all non-ASCII characters as \xNN sequences for each byte of UTF-8, and/or \uNNNN for each word of UTF-16.

Overview of functions

Most of the functions that operate on C strings are defined in the string.h (cstring header in C++). Functions that operate on C wide strings are defined in the wchar.h (cwchar header in C++). These headers also contain declarations of functions used for handling memory buffers; the name is thus something of a misnomer.

Functions declared in string.h are extremely popular since, as a part of the C standard library, they are guaranteed to work on any platform which supports C. However, some security issues exist with these functions, such as buffer overflows, leading programmers to prefer safer, possibly less portable variants, of which some popular ones are listed here.

In historical documentation the term "character" was often used instead of "byte" for C strings, which leads many to believe that these functions somehow don't work for UTF-8. In fact all lengths are defined as being in bytes and this is true in all implementations, and these functions work as well with UTF-8 as with any other byte encoding. The BSD documentation has been fixed to make this clear, but POSIX, Linux, and Windows documentation still uses "character" in many places where "byte" or "wchar_t" is the correct term.

Constants and types

Name Notes
NULL macro expanding to the null pointer constant; that is, a constant representing a pointer value which is guaranteed not to be a valid address of an object in memory.
wchar_t type used for a code unit in a wide strings, usually either 16 or 32 bits.
wint_t integer type that can hold any value of a wchar_t as well as the value of the macro WEOF. This type is unchanged by integral promotions. Usually a 32 bit signed value.
mbstate_t contains all the information about the conversion state required from one call to a function to the other.

Functions

Byte
string
Wide
string
Description[note 1]
String
manipulation
strcpy wcscpy copies one string to another
strncpy wcsncpy writes exactly n bytes/wchar_t, copying from source or adding nulls
strcat wcscat appends one string to another
strncat wcsncat appends no more than n bytes/wchar_t from one string to another
strxfrm wcsxfrm transforms a string according to the current locale
String
examination
strlen wcslen returns the length of the string
strcmp wcscmp compares two strings
strncmp wcsncmp compares a specific number of bytes/wchar_t in two strings
strcoll wcscoll compares two strings according to the current locale
strchr wcschr finds the first occurrence of a byte/wchar_t in a string
strrchr wcsrchr finds the last occurrence of a byte/wchar_t in a string
strspn wcsspn finds in a string the first occurrence of a byte/wchar_t not in a set
strcspn wcscspn finds in a string the last occurrence of a byte/wchar_t not in a set
strpbrk wcspbrk finds in a string the first occurrence of a byte/wchar_t in a set
strstr wcsstr finds the first occurrence of a substring in a string
strtok wcstok splits string into tokens
Miscellaneous strerror returns a string containing a message derived from an error code
Memory
manipulation
memset wmemset fills a buffer with a repeated byte/wchar_t
memcpy wmemcpy copies one buffer to another
memmove wmemmove copies one buffer to another, possibly overlapping, buffer
memcmp wmemcmp compares two buffers
memchr wmemchr finds the first occurrence of a byte/wchar_t in a buffer
  1. ^ Here string refers either to byte string or wide string

Multibyte functions

Name Description
mblen returns the number of bytes in the next multibyte character
mbtowc converts the first multibyte character in a string to the matching wide character
wctomb converts a wide character to the matching multibyte character
mbstowcs converts a narrow multibyte string to a wide string
wcstombs converts a wide character string to a narrow multibyte string
mbsinit checks if a mbstate_t object represents initial shift state
btowc widens a single-byte character to wide character, if possible
wctob narrows a wide character to a single-byte character, if possible
mbrlen returns the number of bytes in the next multibyte character, given state
mbrtowc converts the next multibyte character to wide character, given state
wcrtomb converts a wide character to its multibyte representation, given state
mbsrtowcs converts a narrow multibyte character string to wide string, given state
wcsrtombs converts a wide string to narrow multibyte character string, given state
mbrtoc16 (C99) generate the next 16-bit wide character from a narrow multibyte string
c16rtomb (C99) convert a 16-bit wide character to narrow multibyte string
mbrtoc32 (C99) generate the next 32-bit wide character from a narrow multibyte string
c32rtomb (C99) convert a 32-bit wide character to narrow multibyte string

Numeric conversions

The C standard library contains several functions for numeric conversions. The functions that deal with byte strings are defined in the stdlib.h header (cstdlib header in C++). The functions that deal with wide strings are defined in the wchar.h header (cwchar header in C++).

Byte
string
Wide
string
Description[note 1]
atof converts a string to a floating-point value
atoi
atol
atoll
converts a string to an integer (C99/C++11)
strtof(C99/C++11)
strtod
strtold(C99/C++11)
wcstof(C99/C++11)
wcstod
wcstold(C99/C++11)
converts a string to a floating-point value
strtol
strtoll
wcstol
wcstoll
converts a string to a signed integer
strtoul
strtoull
wcstoul
wcstoull
converts a string to an unsigned integer
  1. ^ Here string refers either to byte string or wide string

Popular extensions

  • memccpy - SVID, POSIX - copies up to specified number of bytes between two memory areas, which must not overlap, stopping when a given byte is found.
  • mempcpy - GNU - a variant of memcpy returning a pointer to the byte following the last written byte
  • strcat_s - ISO/IEC WDTR 24731 - a variant of strcat that checks the destination buffer size before copying
  • strcpy_s - ISO/IEC WDTR 24731 - a variant of strcpy that checks the destination buffer size before copying
  • strdup - POSIX - allocates and duplicates a string
  • strerror_r - POSIX 1, GNU - a variant of strerror that is thread-safe. GNU version is incompatible with POSIX one.
  • strlcpy - BSD - a variant of strcpy that truncates the result to fit in the destination buffer[5]
  • strlcat - BSD - a variant of strcat that truncates the result to fit in the destination buffer[5]
  • strsignal - POSIX:2008 - returns string representation of a signal code. Not thread safe.
  • strtok_r - POSIX - a variant of strtok that is thread-safe

Criticism

Despite the well-established need to replace strcat and strcpy with functions that do not overflow buffers, no accepted standard has arisen. Partly this is due to the mistaken belief by many C programmers that strncat and strncpy have the desired behavior (neither function was designed for this and the behavior and arguments are non-intuitive and often written wrong even by expert programmers[5]). Replacement functions which take the buffer length as an argument have been proposed, but have failed to become standards for political rather than technical reasons:

strcat_s and strcpy_s attracted considerable criticism because even though they are defined in the ISO/IEC WDTR 24731 standard, they are currently supported only by Microsoft Visual C++. Warning messages produced by Microsoft's compilers suggesting programmers use these functions instead of standard ones have been speculated by some to be a Microsoft attempt to lock developers to its platform.[6][7] The default behavior of these functions on overflow is to terminate the application.

The more popular strlcpy and strlcat have been criticised on the basis that they encourage use of C strings and thus create more problems than they solve[8] and for lacking documentation.[9] Consequently they have not been included in the GNU C library (used by software on Linux), although they are implemented in OpenBSD, FreeBSD, Solaris, Mac OS X, and even internally in the Linux kernel.

See also

References

  1. ^ a b c "The C99 standard draft + TC3" (PDF). §7.1.1p1. Retrieved 7 January 2011.{{cite web}}: CS1 maint: location (link)
  2. ^ "The C99 standard draft + TC3" (PDF). §6.4.5p7. Retrieved 7 January 2011.{{cite web}}: CS1 maint: location (link)
  3. ^ "The C99 standard draft + TC3" (PDF). Section 6.4.5 footnote 66. Retrieved 7 January 2011.{{cite web}}: CS1 maint: location (link)
  4. ^ "The C99 standard draft + TC3" (PDF). §5.1.1.2 Translation phases, p1. Retrieved 23 December 2011.{{cite web}}: CS1 maint: location (link)
  5. ^ a b c Todd C. Miller (1999). "strlcpy and strlcat - consistent, safe, string copy and concatenation". USENIX '99. {{cite web}}: Unknown parameter |coauthors= ignored (|author= suggested) (help)
  6. ^ Danny Kalev. "They're at it again". InformIT. Retrieved 10 November 2011.
  7. ^ "Security Enhanced CRT, Safer Than Standard Library?". Retrieved 10 November 2011.
  8. ^ libc-alpha mailing list, selected messages from 8 August 2000 thread: 53, 60, 61
  9. ^ Antill, James. "Security with string APIs: Security relevant things to look for in a string library API". Retrieved 10 November 2011.