fixed packaging
[platform/upstream/libunistring.git] / doc / libunistring.texi
1 \input texinfo          @c -*-texinfo-*-
2 @comment %**start of header
3 @setfilename libunistring.info
4 @documentencoding UTF-8
5 @settitle GNU libunistring
6 @finalout
7 @c Indices:
8 @c   am = autoconf macro  @amindex
9 @c   cp = concept         @cindex
10 @c   fn = function        @findex
11 @c   tp = type            @tindex
12 @c Unused predefined indices:
13 @c   ky = keystroke       @kindex
14 @c   pg = program         @pindex
15 @c   vr = variable        @vindex
16 @defcodeindex am
17 @syncodeindex am cp
18 @syncodeindex fn cp
19 @syncodeindex tp cp
20 @ifclear texi2html
21 @firstparagraphindent insert
22 @end ifclear
23 @c texi2html-1.76 does not support @arrow{}.
24 @ifset texi2html
25 @macro arrow{}
26
27 @end macro
28 @end ifset
29 @comment %**end of header
30
31 @include version.texi
32
33 @c Location of the POSIX specification on the web.
34 @set POSIXURL http://www.opengroup.org/onlinepubs/9699919799
35
36 @c Macro for referencing a POSIX function.
37 @c We don't write it as func(), see section "GNU Manuals" of the
38 @c GNU coding standards.
39 @ifinfo
40 @macro posixfunc{func}
41 @code{\func\}
42 @end macro
43 @end ifinfo
44 @ifnotinfo
45 @macro posixfunc{func}
46 @uref{@value{POSIXURL}/functions/\func\.html,,@code{\func\}}
47 @end macro
48 @end ifnotinfo
49
50 @c Macro for referencing a normal function.
51 @c We don't write it as func(), see section "GNU Manuals" of the
52 @c GNU coding standards.
53 @macro func{func}
54 @code{\func\}
55 @end macro
56
57 @c Macro for an advisory ragged line break in TeX mode.
58 @c Needed because there are long unbreakable pieces of text (such as URLs or
59 @c formulas), TeX is too shy to move them to a new line. TeX considers only
60 @c two choices: a line break in aligned mode (which it rejects due to aesthetic
61 @c reasons) and writing into the margin. What we want in many cases is a line
62 @c break without filling the first line. Like what @* delivers. But we want it
63 @c only when needed, so that it disappears when unrelated changes in the same
64 @c paragraph cause a line break in a nearby position. And we need it only in
65 @c TeX mode. info and HTML modes are fine.
66 @c This trick is from Karl Berry.
67 @iftex
68 @macro texnl
69 @hfil@penalty9000@hfilneg
70 @end macro
71 @end iftex
72 @ifnottex
73 @macro texnl
74 @end macro
75 @end ifnottex
76
77 @ifinfo
78 @dircategory Software development
79 @direntry
80 * GNU libunistring: (libunistring).     Unicode string library.
81 @end direntry
82 @end ifinfo
83
84 @ifinfo
85 This manual is for GNU libunistring.
86
87 @ignore
88 @c This was: @copying but it triggers a makeinfo 4.13 bug
89 Copyright (C) 2001-2010 Free Software Foundation, Inc.
90
91 This manual is free documentation.  It is dually licensed under the
92 GNU FDL and the GNU GPL.  This means that you can redistribute this
93 manual under either of these two licenses, at your choice.
94
95 This manual is covered by the GNU FDL.  Permission is granted to copy,
96 distribute and/or modify this document under the terms of the
97 GNU Free Documentation License (FDL), either version 1.2 of the
98 License, or (at your option) any later version published by the
99 Free Software Foundation (FSF); with no Invariant Sections, with no
100 Front-Cover Text, and with no Back-Cover Texts.
101 A copy of the license is included in @ref{GNU FDL}.
102
103 This manual is covered by the GNU GPL.  You can redistribute it and/or
104 modify it under the terms of the GNU General Public License (GPL), either
105 version 3 of the License, or (at your option) any later version published
106 by the Free Software Foundation (FSF).
107 A copy of the license is included in @ref{GNU GPL}.
108 @end ignore
109 @end ifinfo
110
111 @titlepage
112 @title GNU libunistring, version @value{VERSION}
113 @subtitle updated @value{UPDATED}
114 @subtitle Edition @value{EDITION}, @value{UPDATED}
115 @author Bruno Haible
116
117 @ifnothtml
118 @page
119 @vskip 0pt plus 1filll
120 @c @insertcopying
121 Copyright (C) 2001-2010 Free Software Foundation, Inc.
122
123 This manual is free documentation.  It is dually licensed under the
124 GNU FDL and the GNU GPL.  This means that you can redistribute this
125 manual under either of these two licenses, at your choice.
126
127 This manual is covered by the GNU FDL.  Permission is granted to copy,
128 distribute and/or modify this document under the terms of the
129 GNU Free Documentation License (FDL), either version 1.2 of the
130 License, or (at your option) any later version published by the
131 Free Software Foundation (FSF); with no Invariant Sections, with no
132 Front-Cover Text, and with no Back-Cover Texts.
133 A copy of the license is included in @ref{GNU FDL}.
134
135 This manual is covered by the GNU GPL.  You can redistribute it and/or
136 modify it under the terms of the GNU General Public License (GPL), either
137 version 3 of the License, or (at your option) any later version published
138 by the Free Software Foundation (FSF).
139 A copy of the license is included in @ref{GNU GPL}.
140 @end ifnothtml
141 @end titlepage
142
143 @c Table of Contents
144 @contents
145
146 @ifnottex
147 @node Top
148 @top GNU libunistring
149 @end ifnottex
150
151 @menu
152 * Introduction::                Who may need Unicode strings?
153 * Conventions::                 Conventions used in this manual
154 * unitypes.h::                  Elementary types
155 * unistr.h::                    Elementary Unicode string functions
156 * uniconv.h::                   Conversions between Unicode and encodings
157 * unistdio.h::                  Output with Unicode strings
158 * uniname.h::                   Names of Unicode characters
159 * unictype.h::                  Unicode character classification and properties
160 * uniwidth.h::                  Display width
161 * unigbrk.h::                   Grapheme cluster breaking
162 * uniwbrk.h::                   Word breaks in strings
163 * unilbrk.h::                   Line breaking
164 * uninorm.h::                   Normalization forms
165 * unicase.h::                   Case mappings
166 * uniregex.h::                  Regular expressions
167 * Using the library::           How to link with the library and use it?
168 * More functionality::          More advanced functionality
169 * Licenses::                    Licenses
170
171 * Index::                       General Index
172
173 @detailmenu
174  --- The Detailed Node Listing ---
175
176 Introduction
177
178 * Unicode::                     What is Unicode?
179 * Unicode and i18n::            Unicode and internationalization
180 * Locale encodings::            What is a locale encoding?
181 * In-memory representation::    How to represent strings in memory?
182 * char * strings::              What to keep in mind with @code{char *} strings
183 * The wchar_t mess::            Why @code{wchar_t *} strings are useless
184 * Unicode strings::             How are Unicode strings represented?
185
186 unistr.h
187
188 * Elementary string checks::
189 * Elementary string conversions::
190 * Elementary string functions::
191 * Elementary string functions with memory allocation::
192 * Elementary string functions on NUL terminated strings::
193
194 unictype.h
195
196 * General category::
197 * Canonical combining class::
198 * Bidi class::
199 * Decimal digit value::
200 * Digit value::
201 * Numeric value::
202 * Mirrored character::
203 * Arabic shaping::
204 * Properties::
205 * Scripts::
206 * Blocks::
207 * ISO C and Java syntax::
208 * Classifications like in ISO C::
209
210 General category
211
212 * Object oriented API::
213 * Bit mask API::
214
215 Properties
216
217 * Properties as objects::
218 * Properties as functions::
219
220 unigbrk.h
221
222 * Grapheme cluster breaks in a string::
223 * Grapheme cluster break property::
224
225 uniwbrk.h
226
227 * Word breaks in a string::
228 * Word break property::
229
230 uninorm.h
231
232 * Decomposition of characters::
233 * Composition of characters::
234 * Normalization of strings::
235 * Normalizing comparisons::
236 * Normalization of streams::
237
238 unicase,h
239
240 * Case mappings of characters::
241 * Case mappings of strings::
242 * Case mappings of substrings::
243 * Case insensitive comparison::
244 * Case detection::
245
246 Using the library
247
248 * Installation::
249 * Compiler options::
250 * Include files::
251 * Autoconf macro::
252 * Reporting problems::
253
254 Licenses
255
256 * GNU GPL::                     GNU General Public License
257 * GNU LGPL::                    GNU Lesser General Public License
258 * GNU FDL::                     GNU Free Documentation License
259
260 @end detailmenu
261 @end menu
262
263 @node Introduction
264 @chapter Introduction
265
266 This library provides functions for manipulating Unicode strings and
267 for manipulating C strings according to the Unicode standard.
268
269 It consists of the following parts:
270
271 @table @code
272 @item <unistr.h>
273 elementary string functions
274 @item <uniconv.h>
275 conversion from/to legacy encodings
276 @item <unistdio.h>
277 formatted output to strings
278 @item <uniname.h>
279 character names
280 @item <unictype.h>
281 character classification and properties
282 @item <uniwidth.h>
283 string width when using nonproportional fonts
284 @item <unigbrk.h>
285 grapheme cluster breaks
286 @item <uniwbrk.h>
287 word breaks
288 @item <unilbrk.h>
289 line breaking algorithm
290 @item <uninorm.h>
291 normalization (composition and decomposition)
292 @item <unicase.h>
293 case folding
294 @item <uniregex.h>
295 regular expressions (not yet implemented)
296 @end table
297
298 @cindex use cases
299 @cindex value, of libunistring
300 libunistring is for you if your application involves non-trivial text
301 processing, such as upper/lower case conversions, line breaking, operations
302 on words, or more advanced analysis of text.  Text provided by the user can,
303 in general, contain characters of all kinds of scripts.  The text processing
304 functions provided by this library handle all scripts and all languages.
305
306 libunistring is for you if your application already uses the ISO C / POSIX
307 @code{<ctype.h>}, @code{<wctype.h>} functions and the text it operates on is
308 provided by the user and can be in any language.
309
310 libunistring is also for you if your application uses Unicode strings as
311 internal in-memory representation.
312
313 @menu
314 * Unicode::                     What is Unicode?
315 * Unicode and i18n::            Unicode and internationalization
316 * Locale encodings::            What is a locale encoding?
317 * In-memory representation::    How to represent strings in memory?
318 * char * strings::              What to keep in mind with @code{char *} strings
319 * The wchar_t mess::            Why @code{wchar_t *} strings are useless
320 * Unicode strings::             How are Unicode strings represented?
321 @end menu
322
323 @node Unicode
324 @section Unicode
325
326 @cindex Unicode
327 Unicode is a standardized repertoire of characters that contains characters
328 from all scripts of the world, from Latin letters to Chinese ideographs
329 and Babylonian cuneiform glyphs.  It also specifies how these characters
330 are to be rendered on a screen or on paper, and how common text processing
331 (word selection, line breaking, uppercasing of page titles etc.) is supposed
332 to behave on Unicode text.
333
334 Unicode also specifies three ways of storing sequences of Unicode
335 characters in a computer whose basic unit of data is an 8-bit byte:
336 @cindex UTF-8
337 @cindex UTF-16
338 @cindex UTF-32
339 @cindex UCS-4
340 @table @asis
341 @item UTF-8
342 Every character is represented as 1 to 4 bytes.
343 @item UTF-16
344 Every character is represented as 1 to 2 units of 16 bits.
345 @item UTF-32, a.k.a@. UCS-4
346 Every character is represented as 1 unit of 32 bits.
347 @end table
348
349 For encoding Unicode text in a file, UTF-8 is usually used.  For encoding
350 Unicode strings in memory for a program, either of the three encoding forms
351 can be reasonably used.
352
353 Unicode is widely used on the web.  Prior to the use of Unicode, web pages
354 were in many different encodings (ISO-8859-1 for English, French, Spanish,
355 ISO-8859-2 for Polish, ISO-8859-7 for Greek, KOI8-R for Russian, GB2312 or
356 BIG5 for Chinese, ISO-2022-JP-2 or EUC-JP or Shift_JIS for Japanese, and many
357 many others).  It was next to impossible to create a document that contained
358 Chinese and Polish text in the same document.  Due to the many encodings for
359 Japanese, even the processing of pure Japanese text was error prone.
360
361 References:
362 @itemize @bullet
363 @item
364 The Unicode standard:@texnl{} @url{http://www.unicode.org/}
365 @item
366 Definition of UTF-8:@texnl{} @url{http://www.rfc-editor.org/rfc/rfc3629.txt}
367 @item
368 Definition of UTF-16:@texnl{} @url{http://www.rfc-editor.org/rfc/rfc2781.txt}
369 @item
370 Markus Kuhn's UTF-8 and Unicode FAQ:@texnl{}
371 @url{http://www.cl.cam.ac.uk/~mgk25/unicode.html}
372 @end itemize
373
374 @node Unicode and i18n
375 @section Unicode and Internationalization
376
377 @cindex internationalization
378 Internationalization is the process of changing the source code of a program
379 so that it can meet the expectations of users in any culture, if culture
380 specific data (translations, images etc.) are provided.
381
382 Use of Unicode is not strictly required for internationalization, but it
383 makes internationalization much easier, because operations that need to
384 look at specific characters (like hyphenation, spell checking, or the
385 automatic conversion of double-quotes to opening and closing double-quote
386 characters) don't need to consider multiple possible encodings of the text.
387
388 Use of Unicode also enables multilingualization: the ability of having text
389 in multiple languages present in the same document or even in the same line
390 of text.
391
392 But use of Unicode is not everything.  Internationalization usually consists
393 of three features:
394 @itemize @bullet
395 @item
396 Use of Unicode where needed for text processing.  This is what this library
397 is for.
398 @item
399 Use of message catalogs for messages shown to the user, This is what
400 GNU gettext is about.
401 @item
402 Use of locale specific conventions for date and time formats, for numeric
403 formatting, or for sorting of text.  This can be done adequately with the
404 POSIX APIs and the implementation of locales in the GNU C library.
405 @end itemize
406
407 @node Locale encodings
408 @section Locale encodings
409
410 @cindex locale
411 A locale is a set of cultural conventions.  According to POSIX, for a program,
412 at any moment, there is one locale being designated as the ``current locale''.
413 (Actually, POSIX supports also one locale per thread, but this feature is not
414 yet universally implemented and not widely used.)
415 @cindex locale categories
416 The locale is partitioned into several aspects, called the ``categories''
417 of the locale.  The main various aspects are:
418 @itemize
419 @item
420 The character encoding and the character properties.  This is the
421 @code{LC_CTYPE} category.
422 @item
423 The sorting rules for text.  This is the @code{LC_COLLATE} category.
424 @item
425 The language specific translations of messages.  This is the
426 @code{LC_MESSAGES} category.
427 @item
428 The formatting rules for numbers, such as the decimal separator.  This is
429 the @code{LC_NUMERIC} category.
430 @item
431 The formatting rules for amounts of money.  This is the @code{LC_MONETARY}
432 category.
433 @item
434 The formatting of date and time.  This is the @code{LC_TIME} category.
435 @end itemize
436
437 @cindex locale encoding
438 In particular, the @code{LC_CTYPE} category of the current locale determines
439 the character encoding.  This is the encoding of @samp{char *} strings.
440 We also call it the ``locale encoding''.  GNU libunistring has a function,
441 @func{locale_charset}, that returns a standardized (platform independent)
442 name for this encoding.
443
444 All locale encodings used on glibc systems are essentially ASCII compatible:
445 Most graphic ASCII characters have the same representation, as a single byte,
446 in that encoding as in ASCII.
447
448 Among the possible locale encodings are UTF-8 and GB18030.  Both allow
449 to represent any Unicode character as a sequence of bytes.  UTF-8 is used in
450 most of the world, whereas GB18030 is used in the People's Republic of China,
451 because it is backward compatible with the GB2312 encoding that was used in
452 this country earlier.
453
454 The legacy locale encodings, ISO-8859-15 (which supplanted ISO-8859-1 in
455 most of Europe), ISO-8859-2, KOI8-R, EUC-JP, etc., are still in use in
456 many places, though.
457
458 UTF-16 and UTF-32 are not used as locale encodings, because they are not
459 ASCII compatible.
460
461 @node In-memory representation
462 @section Choice of in-memory representation of strings
463
464 There are three ways of representing strings in memory of a running
465 program.
466 @itemize
467 @item
468 As @samp{char *} strings.  Such strings are represented in locale encoding.
469 This approach is employed when not much text processing is done by the
470 program.  When some Unicode aware processing is to be done, a string is
471 converted to Unicode on the fly and back to locale encoding afterwards.
472 @item
473 As UTF-8 or UTF-16 or UTF-32 strings.  This implies that conversion from
474 locale encoding to Unicode is performed on input, and in the opposite
475 direction on output.  This approach is employed when the program does
476 a significant amount of text processing, or when the program has multiple
477 threads operating on the same data but in different locales.
478 @item
479 As @samp{wchar_t *}, a.k.a@. ``wide strings''.  This approach is misguided,
480 see @ref{The wchar_t mess}.
481 @end itemize
482
483 @node char * strings
484 @section @samp{char *} strings
485
486 @cindex C string functions
487 The classical C strings, with its C library support standardized by
488 ISO C and POSIX, can be used in internationalized programs with some
489 precautions.  The problem with this API is that many of the C library
490 functions for strings don't work correctly on strings in locale
491 encodings, leading to bugs that only people in some cultures of the
492 world will experience.
493
494 @cindex locale, multibyte
495 The first problem with the C library API is the support of multibyte
496 locales.  According to the locale encoding, in general, every character
497 is represented by one or more bytes (up to 4 bytes in practice --- but
498 use @code{MB_LEN_MAX} instead of the number 4 in the code).
499 When every character is represented by only 1 byte, we speak of an
500 ``unibyte locale'', otherwise of a ``multibyte locale''.  It is important
501 to realize that the majority of Unix installations nowadays use UTF-8
502 or GB18030 as locale encoding; therefore, the majority of users are
503 using multibyte locales.
504
505 @cindex char, type
506 The important fact to remember is:
507 @cartouche
508 @emph{A @samp{char} is a byte, not a character.}
509 @end cartouche
510
511 As a consequence:
512 @itemize
513 @item
514 The @code{<ctype.h>} API is useless in this context; it does not work in
515 multibyte locales.
516 @item
517 The @posixfunc{strlen} function does not return the number of characters
518 in a string.  Nor does it return the number of screen columns occupied
519 by a string after it is output.  It merely returns the number of
520 @emph{bytes} occupied by a string.
521 @item
522 Truncating a string, for example, with @posixfunc{strncpy}, can have the
523 effect of truncating it in the middle of a multibyte character.  Such
524 a string will, when output, have a garbled character at its end, often
525 represented by a hollow box.
526 @item
527 @posixfunc{strchr} and @posixfunc{strrchr} do not work with multibyte strings
528 if the locale encoding is GB18030 and the character to be searched is
529 a digit.
530 @item
531 @posixfunc{strstr} does not work with multibyte strings if the locale encoding
532 is different from UTF-8.
533 @item
534 @posixfunc{strcspn}, @posixfunc{strpbrk}, @posixfunc{strspn} cannot work
535 correctly in multibyte locales: they assume the second argument is a list of
536 single-byte characters.  Even in this simple case, they do not work with
537 multibyte strings if the locale encoding is GB18030 and one of the
538 characters to be searched is a digit.
539 @item
540 @posixfunc{strsep} and @posixfunc{strtok_r} do not work with multibyte strings
541 unless all of the delimiter characters are ASCII characters < 0x30.
542 @item
543 The @posixfunc{strcasecmp}, @posixfunc{strncasecmp}, and @posixfunc{strcasestr}
544 functions do not work with multibyte strings.
545 @end itemize
546
547 The workarounds can be found in GNU gnulib
548 @url{http://www.gnu.org/software/gnulib/}.
549 @itemize
550 @item
551 gnulib has modules @samp{mbchar}, @samp{mbiter}, @samp{mbuiter} that
552 represent multibyte characters and allow to iterate across a multibyte
553 string with the same ease as through a unibyte string.
554 @item
555 gnulib has functions @func{mbslen} and @func{mbswidth} that can be
556 used instead of @posixfunc{strlen} when the number of characters or the
557 number of screen columns of a string is requested.
558 @item
559 gnulib has functions @func{mbschr} and @func{mbsrrchr} that are
560 like @posixfunc{strchr} and @posixfunc{strrchr}, but work in multibyte locales.
561 @item
562 gnulib has a function @func{mbsstr}, like @posixfunc{strstr}, but works
563 in multibyte locales.
564 @item
565 gnulib has functions @func{mbscspn}, @func{mbspbrk}, @func{mbsspn}
566 that are like @posixfunc{strcspn}, @posixfunc{strpbrk}, @posixfunc{strspn}, but
567 work in multibyte locales.
568 @item
569 gnulib has functions @func{mbssep} and @func{mbstok_r} that are
570 like @posixfunc{strsep} and @posixfunc{strtok_r} but work in multibyte locales.
571 @item
572 gnulib has functions @func{mbscasecmp}, @func{mbsncasecmp},
573 @func{mbspcasecmp}, and @func{mbscasestr} that are like @posixfunc{strcasecmp},
574 @posixfunc{strncasecmp}, and @posixfunc{strcasestr}, but
575 work in multibyte locales.  Still, the function @code{ulc_casecmp} is
576 preferable to these functions; see below.
577 @end itemize
578
579 The second problem with the C library API is that it has some assumptions built-in that are not valid in some languages:
580 @itemize
581 @item
582 It assumes that there are only two forms of every character: uppercase
583 and lowercase.  This is not true for Croatian, where the character
584 @sc{LETTER DZ WITH CARON} comes in three forms:
585 @sc{LATIN CAPITAL LETTER DZ WITH CARON} (DZ),
586 @sc{LATIN CAPITAL LETTER D WITH SMALL LETTER Z WITH CARON} (Dz),
587 @sc{LATIN SMALL LETTER DZ WITH CARON} (dz).
588 @item
589 It assumes that uppercasing of 1 character leads to 1 character.  This
590 is not true for German, where the @sc{LATIN SMALL LETTER SHARP S}, when
591 uppercased, becomes @samp{SS}.
592 @item
593 It assumes that there is 1:1 mapping between uppercase and lowercase forms.
594 This is not true for the Greek sigma: @sc{GREEK CAPITAL LETTER SIGMA} is
595 the uppercase of both @sc{GREEK SMALL LETTER SIGMA} and
596 @sc{GREEK SMALL LETTER FINAL SIGMA}.
597 @item
598 It assumes that the upper/lowercase mappings are position independent.
599 This is not true for the Greek sigma and the Lithuanian i.
600 @end itemize
601
602 The correct way to deal with this problem is
603 @enumerate
604 @item
605 to provide functions for titlecasing, as well as for upper- and
606 lowercasing,
607 @item
608 to view case transformations as functions that operates on strings,
609 rather than on characters.
610 @end enumerate
611
612 This is implemented in this library, through the functions declared in @code{<unicase.h>}, see @ref{unicase.h}.
613
614 @node The wchar_t mess
615 @section The @code{wchar_t} mess
616
617 @cindex wchar_t, type
618 The ISO C and POSIX standard creators made an attempt to fix the first
619 problem mentioned in the previous section.  They introduced
620 @itemize
621 @item
622 a type @samp{wchar_t}, designed to encapsulate an entire character,
623 @item
624 a ``wide string'' type @samp{wchar_t *}, and
625 @item
626 functions declared in @code{<wctype.h>} that were meant to supplant the
627 ones in @code{<ctype.h>}.
628 @end itemize
629
630 Unfortunately, this API and its implementation has numerous problems:
631
632 @itemize
633 @item
634 On AIX and Windows platforms, @code{wchar_t} is a 16-bit type.  This
635 means that it can never accommodate an entire Unicode character.  Either
636 the @code{wchar_t *} strings are limited to characters in UCS-2 (the
637 ``Basic Multilingual Plane'' of Unicode), or --- if @code{wchar_t *}
638 strings are encoded in UTF-16 --- a @code{wchar_t} represents only half
639 of a character in the worst case, making the @code{<wctype.h>} functions
640 pointless.
641
642 @item
643 On Solaris and FreeBSD, the @code{wchar_t} encoding is locale dependent
644 and undocumented.  This means, if you want to know any property of a
645 @code{wchar_t} character, other than the properties defined by
646 @code{<wctype.h>} --- such as whether it's a dash, currency symbol,
647 paragraph separator, or similar ---, you have to convert it to
648 @code{char *} encoding first, by use of the function @posixfunc{wctomb}.
649
650 @item
651 When you read a stream of wide characters, through the functions
652 @posixfunc{fgetwc} and @posixfunc{fgetws}, and when the input stream/file is
653 not in the expected encoding, you have no way to determine the invalid
654 byte sequence and do some corrective action.  If you use these
655 functions, your program becomes ``garbage in - more garbage out'' or
656 ``garbage in - abort''.
657 @end itemize
658
659 As a consequence, it is better to use multibyte strings, as explained in
660 the previous section.  Such multibyte strings can bypass limitations
661 of the @code{wchar_t} type, if you use functions defined in gnulib and
662 libunistring for text processing.  They can also faithfully transport
663 malformed characters that were present in the input, without requiring
664 the program to produce garbage or abort.
665
666 @node Unicode strings
667 @section Unicode strings
668
669 libunistring supports Unicode strings in three representations:
670 @cindex UTF-8, strings
671 @cindex UTF-16, strings
672 @cindex UTF-32, strings
673 @itemize
674 @item
675 UTF-8 strings, through the type @samp{uint8_t *}.  The units are bytes
676 (@code{uint8_t}).
677 @item
678 UTF-16 strings, through the type @samp{uint16_t *},  The units are 16-bit
679 memory words (@code{uint16_t}).
680 @item
681 UTF-32 strings, through the type @samp{uint32_t *}.  The units are 32-bit
682 memory words (@code{uint32_t}).
683 @end itemize
684
685 As with C strings, there are two variants:
686 @itemize
687 @item
688 Unicode strings with a terminating NUL character are represented as
689 a pointer to the first unit of the string.  There is a unit containing
690 a 0 value at the end.  It is considered part of the string for all
691 memory allocation purposes, but is not considered part of the string
692 for all other logical purposes.
693 @item
694 Unicode strings where embedded NUL characters are allowed.  These
695 are represented by a pointer to the first unit and the number of units
696 (not bytes!) of the string.  In this setting, there is no trailing
697 zero-valued unit used as ``end marker''.
698 @end itemize
699
700 @node Conventions
701 @chapter Conventions
702
703 This chapter explains conventions valid throughout the libunistring library.
704
705 @cindex argument conventions
706 Variables of type @code{char *} denote C strings in locale encoding.
707 See @ref{Locale encodings}.
708
709 Variables of type @code{uint8_t *} denote UTF-8 strings.  Their units
710 are bytes.
711
712 Variables of type @code{uint16_t *} denote UTF-16 strings, without byte
713 order mark.  Their units are 2-byte words.
714
715 Variables of type @code{uint32_t *} denote UTF-32 strings, without byte
716 order mark.  Their units are 4-byte words.
717
718 Argument pairs @code{(@var{s}, @var{n})} denote a string
719 @code{@var{s}[0..@var{n}-1]} with exactly @var{n} units.
720
721 All functions with prefix @samp{ulc_} operate on C strings in locale
722 encoding.
723
724 All functions with prefix @samp{u8_} operate on UTF-8 strings.
725
726 All functions with prefix @samp{u16_} operate on UTF-16 strings.
727
728 All functions with prefix @samp{u32_} operate on UTF-32 strings.
729
730 For every function with prefix @samp{u8_}, operating on UTF-8 strings,
731 there is also a corresponding function with prefix @samp{u16_},
732 operating on UTF-16 strings, and a corresponding function with prefix
733 @samp{u32_}, operating on UTF-32 strings.  Their description is
734 analogous; in this documentation we describe only the function that
735 operates on UTF-8 strings, for brevity.
736
737 A declaration with a variable @var{n} denotes the three concrete
738 declarations with @var{n} = 8, @var{n} = 16, @var{n} = 32.
739
740 All parameters starting with @samp{str} and the parameters of
741 functions starting with @code{u8_str}/@code{u16_str}/@code{u32_str}
742 denote a NUL terminated string.
743
744 @cindex return value conventions
745 Error values are always returned through the @code{errno} variable,
746 usually with a return value that indicates the presence of an error
747 (NULL for functions that return an pointer, or -1 for functions that
748 return an @code{int}).
749
750 Functions returning a string result take a
751 @code{(@var{resultbuf}, @var{lengthp})}
752 argument pair.  If @var{resultbuf} is not NULL and the result fits
753 into @code{*@var{lengthp}} units, it is put in @var{resultbuf}, and
754 @var{resultbuf} is returned.  Otherwise, a freshly allocated string
755 is returned.  In both cases, @code{*@var{lengthp}} is set to the
756 length (number of units) of the returned string.  In case of error,
757 NULL is returned and @code{errno} is set.
758
759 @include unitypes.texi
760 @include unistr.texi
761 @include uniconv.texi
762 @include unistdio.texi
763 @include uniname.texi
764 @include unictype.texi
765 @include uniwidth.texi
766 @include unigbrk.texi
767 @include uniwbrk.texi
768 @include unilbrk.texi
769 @include uninorm.texi
770 @include unicase.texi
771 @include uniregex.texi
772
773 @node Using the library
774 @chapter Using the library
775
776 This chapter explains some practical considerations, regarding the
777 installation and compiler options that are needed in order to use this
778 library.
779
780 @menu
781 * Installation::
782 * Compiler options::
783 * Include files::
784 * Autoconf macro::
785 * Reporting problems::
786 @end menu
787
788 @node Installation
789 @section Installation
790
791 @cindex dependencies
792 Before you can use the library, it must be installed.  First, you have to
793 make sure all dependencies are installed.  They are listed in the file
794 @file{DEPENDENCIES}.
795
796 @cindex installation
797 Then you can proceed to build and install the library, as described in the
798 file @file{INSTALL}.  For installation on Windows systems, please refer to
799 the file @file{README.woe32}.
800
801 @node Compiler options
802 @section Compiler options
803
804 Let's denote as @code{LIBUNISTRING_PREFIX} the value of the @samp{--prefix}
805 option that you passed to @code{configure} while installing this package.
806 If you didn't pass any @samp{--prefix} option, then the package is installed
807 in @file{/usr/local}.
808
809 Let's denote as @code{LIBUNISTRING_INCLUDEDIR} the directory where the
810 include files were installed.  This is usually the same as
811 @code{$@{LIBUNISTRING_PREFIX@}/include}.  Except that if you passed an
812 @samp{--includedir} option to @code{configure}, it is the value of that
813 option.
814
815 Let's further denote as @code{LIBUNISTRING_LIBDIR} the directory where
816 the library itself was installed.  This is the value that you passed
817 with the @samp{--libdir} option to @code{configure}, or otherwise the
818 same as @code{$@{LIBUNISTRING_PREFIX@}/lib}.  Recall that when building
819 in 64-bit mode on a 64-bit GNU/Linux system that supports executables
820 in either 64-bit mode or 32-bit mode, you should have used the option
821 @code{--libdir=$@{LIBUNISTRING_PREFIX@}/lib64}.
822
823 @cindex compiler options
824 So that the compiler finds the include files, you have to pass it the
825 option @code{-I$@{LIBUNISTRING_INCLUDEDIR@}}.
826
827 So that the compiler finds the library during its linking pass, you have
828 to pass it the options @code{-L$@{LIBUNISTRING_LIBDIR@} -lunistring}.
829 On some systems, in some configurations, you also have to pass options
830 needed for linking with @code{libiconv}.  The autoconf macro
831 @code{gl_LIBUNISTRING} (see @ref{Autoconf macro}) deals with this
832 particularity.
833
834 @node Include files
835 @section Include files
836
837 Most of the include files have been presented in the introduction, see
838 @ref{Introduction}, and subsequent detailed chapters.
839
840 Another include file is @code{<unistring/version.h>}. It contains the
841 version number of the libunistring library.
842
843 @deftypevr Macro int _LIBUNISTRING_VERSION
844 This constant contains the version of libunistring that is being used
845 at compile time.  It encodes the major and minor parts of the version
846 number only.  These parts are encoded in the form @code{(major<<8) + minor}.
847 @end deftypevr
848
849 @deftypevr Constant int _libunistring_version
850 This constant contains the version of libunistring that is being used
851 at run time.  It encodes the major and minor parts of the version
852 number only.  These parts are encoded in the form @code{(major<<8) + minor}.
853 @end deftypevr
854
855 It is possible that @code{_libunistring_version} is greater than
856 @code{_LIBUNISTRING_VERSION}.  This can happen when you use
857 @code{libunistring} as a shared library, and a newer, binary
858 backward-compatible version has been installed after your program
859 that uses @code{libunistring} was installed.
860
861 @node Autoconf macro
862 @section Autoconf macro
863
864 @cindex autoconf macro
865 GNU Gnulib provides an autoconf macro that tests for the availability
866 of @code{libunistring}.  It is contained in the Gnulib module
867 @samp{libunistring}, see@texnl{}
868 @url{http://www.gnu.org/software/gnulib/MODULES.html#module=libunistring}.
869
870 @amindex gl_LIBUNISTRING
871 The macro is called @code{gl_LIBUNISTRING}.  It searches for an installed
872 libunistring.  If found, it sets and AC_SUBSTs @code{HAVE_LIBUNISTRING=yes}
873 and the @code{LIBUNISTRING} and @code{LTLIBUNISTRING} variables and augments
874 the @code{CPPFLAGS} variable, and defines the C macro
875 @code{HAVE_LIBUNISTRING} to 1.  Otherwise, it sets and AC_SUBSTs
876 @code{HAVE_LIBUNISTRING=no} and @code{LIBUNISTRING} and @code{LTLIBUNISTRING}
877 to empty.
878
879 The complexities that @code{gl_LIBUNISTRING} deals with are the following:
880
881 @itemize @bullet
882 @item
883 On some operating systems, in some configurations, libunistring depends
884 on @code{libiconv}, and the options for linking with libiconv must be
885 mentioned explicitly on the link command line.
886
887 @item
888 GNU @code{libunistring}, if installed, is not necessarily already in the
889 search path (@code{CPPFLAGS} for the include file search path,
890 @code{LDFLAGS} for the library search path).
891
892 @item
893 GNU @code{libunistring}, if installed, is not necessarily already in the
894 run time library search path.  To avoid the need for setting an environment
895 variable like @code{LD_LIBRARY_PATH}, the macro adds the appropriate
896 run time search path options to the @code{LIBUNISTRING} variable.  This works
897 on most systems.
898 @end itemize
899
900 @node Reporting problems
901 @section Reporting problems
902
903 @cindex bug reports
904 @cindex bug tracker
905 @cindex mailing list
906 If you encounter any problem, please don't hesitate to send a detailed
907 bug report to the @code{bug-libunistring@@gnu.org} mailing list.  You can
908 alternatively also use the bug tracker at the project page
909 @url{https://savannah.gnu.org/projects/libunistring}.
910
911 Please always include the version number of this library, and a short
912 description of your operating system and compilation environment with
913 corresponding version numbers.
914
915 For problems that appear while building and installing @code{libunistring},
916 for which you don't find the remedy in the @file{INSTALL} file, please include
917 a description of the options that you passed to the @samp{configure} script.
918
919 @node More functionality
920 @chapter More advanced functionality
921
922 @cindex bidirectional reordering
923 For bidirectional reordering of strings, we recommend the GNU FriBidi library:
924 @url{http://www.fribidi.org/}.
925
926 @cindex rendering
927 For the rendering of Unicode strings outside of the context of a given toolkit
928 (KDE/Qt or GNOME/Gtk), we recommend the Pango library:
929 @url{http://www.pango.org/}.
930
931 @node Licenses
932 @appendix Licenses
933 @cindex Licenses
934
935 The files of this package are covered by the licenses indicated in each
936 particular file or directory.  Here is a summary:
937
938 @itemize @bullet
939 @item
940 The @code{libunistring} library is covered by the
941 GNU Lesser General Public License (LGPL).
942 A copy of the license is included in @ref{GNU LGPL}.
943
944 @item
945 This manual is free documentation.  It is dually licensed under the
946 GNU FDL and the GNU GPL.  This means that you can redistribute this
947 manual under either of these two licenses, at your choice.
948 @*
949 This manual is covered by the GNU FDL.  Permission is granted to copy,
950 distribute and/or modify this document under the terms of the
951 GNU Free Documentation License (FDL), either version 1.2 of the
952 License, or (at your option) any later version published by the
953 Free Software Foundation (FSF); with no Invariant Sections, with no
954 Front-Cover Text, and with no Back-Cover Texts.
955 A copy of the license is included in @ref{GNU FDL}.
956 @*
957 This manual is covered by the GNU GPL.  You can redistribute it and/or
958 modify it under the terms of the GNU General Public License (GPL), either
959 version 3 of the License, or (at your option) any later version published
960 by the Free Software Foundation (FSF).
961 A copy of the license is included in @ref{GNU GPL}.
962 @end itemize
963
964 @menu
965 * GNU GPL::                     GNU General Public License
966 * GNU LGPL::                    GNU Lesser General Public License
967 * GNU FDL::                     GNU Free Documentation License
968 @end menu
969
970 @page
971 @node GNU GPL
972 @appendixsec GNU GENERAL PUBLIC LICENSE
973 @cindex GPL, GNU General Public License
974 @cindex License, GNU GPL
975 @include gpl.texi
976 @page
977 @node GNU LGPL
978 @appendixsec GNU LESSER GENERAL PUBLIC LICENSE
979 @cindex LGPL, GNU Lesser General Public License
980 @cindex License, GNU LGPL
981 @include lgpl.texi
982 @page
983 @node GNU FDL
984 @appendixsec GNU Free Documentation License
985 @cindex FDL, GNU Free Documentation License
986 @cindex License, GNU FDL
987 @include fdl.texi
988
989 @node Index
990 @unnumbered Index
991
992 @printindex cp
993
994 @bye
995
996 @c Local Variables:
997 @c indent-tabs-mode: nil
998 @c whitespace-check-buffer-indent: nil
999 @c End: