Home | History | Annotate | Download | only in utf
      1 /*
      2  * The authors of this software are Rob Pike and Ken Thompson.
      3  * Copyright (c) 1998-2002 by Lucent Technologies.
      4  * Portions Copyright (c) 2009 The Go Authors. All rights reserved.
      5  * Permission to use, copy, modify, and distribute this software for any
      6  * purpose without fee is hereby granted, provided that this entire notice
      7  * is included in all copies of any software which is or includes a copy
      8  * or modification of this software and in all copies of the supporting
      9  * documentation for such software.
     10  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
     11  * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
     12  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
     13  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
     14  */
     15 
     16 #ifndef _UTFH_
     17 #define _UTFH_ 1
     18 
     19 // stdint.h content doesn't seem to be used in this file and doesn't exist on
     20 // Windows, therefore we comment it out here so that the code could be compiled
     21 // on Windows.
     22 //#include <stdint.h>
     23 
     24 typedef signed int Rune;	/* Code-point values in Unicode 4.0 are 21 bits wide.*/
     25 
     26 enum
     27 {
     28   UTFmax	= 4,		/* maximum bytes per rune */
     29   Runesync	= 0x80,		/* cannot represent part of a UTF sequence (<) */
     30   Runeself	= 0x80,		/* rune and UTF sequences are the same (<) */
     31   Runeerror	= 0xFFFD,	/* decoding error in UTF */
     32   Runemax	= 0x10FFFF,	/* maximum rune value */
     33 };
     34 
     35 #ifdef	__cplusplus
     36 extern "C" {
     37 #endif
     38 
     39 /*
     40  * rune routines
     41  */
     42 
     43 /*
     44  * These routines were written by Rob Pike and Ken Thompson
     45  * and first appeared in Plan 9.
     46  * SEE ALSO
     47  * utf (7)
     48  * tcs (1)
     49 */
     50 
     51 // runetochar copies (encodes) one rune, pointed to by r, to at most
     52 // UTFmax bytes starting at s and returns the number of bytes generated.
     53 
     54 int runetochar(char* s, const Rune* r);
     55 
     56 
     57 // chartorune copies (decodes) at most UTFmax bytes starting at s to
     58 // one rune, pointed to by r, and returns the number of bytes consumed.
     59 // If the input is not exactly in UTF format, chartorune will set *r
     60 // to Runeerror and return 1.
     61 //
     62 // Note: There is no special case for a "null-terminated" string. A
     63 // string whose first byte has the value 0 is the UTF8 encoding of the
     64 // Unicode value 0 (i.e., ASCII NULL). A byte value of 0 is illegal
     65 // anywhere else in a UTF sequence.
     66 
     67 int chartorune(Rune* r, const char* s);
     68 
     69 
     70 // charntorune is like chartorune, except that it will access at most
     71 // n bytes of s.  If the UTF sequence is incomplete within n bytes,
     72 // charntorune will set *r to Runeerror and return 0. If it is complete
     73 // but not in UTF format, it will set *r to Runeerror and return 1.
     74 //
     75 // Added 2004-09-24 by Wei-Hwa Huang
     76 
     77 int charntorune(Rune* r, const char* s, int n);
     78 
     79 // isvalidcharntorune(str, n, r, consumed)
     80 // is a convenience function that calls "*consumed = charntorune(r, str, n)"
     81 // and returns an int (logically boolean) indicating whether the first
     82 // n bytes of str was a valid and complete UTF sequence.
     83 
     84 int isvalidcharntorune(const char* str, int n, Rune* r, int* consumed);
     85 
     86 // runelen returns the number of bytes required to convert r into UTF.
     87 
     88 int runelen(Rune r);
     89 
     90 
     91 // runenlen returns the number of bytes required to convert the n
     92 // runes pointed to by r into UTF.
     93 
     94 int runenlen(const Rune* r, int n);
     95 
     96 
     97 // fullrune returns 1 if the string s of length n is long enough to be
     98 // decoded by chartorune, and 0 otherwise. This does not guarantee
     99 // that the string contains a legal UTF encoding. This routine is used
    100 // by programs that obtain input one byte at a time and need to know
    101 // when a full rune has arrived.
    102 
    103 int fullrune(const char* s, int n);
    104 
    105 // The following routines are analogous to the corresponding string
    106 // routines with "utf" substituted for "str", and "rune" substituted
    107 // for "chr".
    108 
    109 // utflen returns the number of runes that are represented by the UTF
    110 // string s. (cf. strlen)
    111 
    112 int utflen(const char* s);
    113 
    114 
    115 // utfnlen returns the number of complete runes that are represented
    116 // by the first n bytes of the UTF string s. If the last few bytes of
    117 // the string contain an incompletely coded rune, utfnlen will not
    118 // count them; in this way, it differs from utflen, which includes
    119 // every byte of the string. (cf. strnlen)
    120 
    121 int utfnlen(const char* s, long n);
    122 
    123 
    124 // utfrune returns a pointer to the first occurrence of rune r in the
    125 // UTF string s, or 0 if r does not occur in the string.  The NULL
    126 // byte terminating a string is considered to be part of the string s.
    127 // (cf. strchr)
    128 
    129 const char* utfrune(const char* s, Rune r);
    130 
    131 
    132 // utfrrune returns a pointer to the last occurrence of rune r in the
    133 // UTF string s, or 0 if r does not occur in the string.  The NULL
    134 // byte terminating a string is considered to be part of the string s.
    135 // (cf. strrchr)
    136 
    137 const char* utfrrune(const char* s, Rune r);
    138 
    139 
    140 // utfutf returns a pointer to the first occurrence of the UTF string
    141 // s2 as a UTF substring of s1, or 0 if there is none. If s2 is the
    142 // null string, utfutf returns s1. (cf. strstr)
    143 
    144 const char* utfutf(const char* s1, const char* s2);
    145 
    146 
    147 // utfecpy copies UTF sequences until a null sequence has been copied,
    148 // but writes no sequences beyond es1.  If any sequences are copied,
    149 // s1 is terminated by a null sequence, and a pointer to that sequence
    150 // is returned.  Otherwise, the original s1 is returned. (cf. strecpy)
    151 
    152 char* utfecpy(char *s1, char *es1, const char *s2);
    153 
    154 
    155 
    156 // These functions are rune-string analogues of the corresponding
    157 // functions in strcat (3).
    158 //
    159 // These routines first appeared in Plan 9.
    160 // SEE ALSO
    161 // memmove (3)
    162 // rune (3)
    163 // strcat (2)
    164 //
    165 // BUGS: The outcome of overlapping moves varies among implementations.
    166 
    167 Rune* runestrcat(Rune* s1, const Rune* s2);
    168 Rune* runestrncat(Rune* s1, const Rune* s2, long n);
    169 
    170 const Rune* runestrchr(const Rune* s, Rune c);
    171 
    172 int runestrcmp(const Rune* s1, const Rune* s2);
    173 int runestrncmp(const Rune* s1, const Rune* s2, long n);
    174 
    175 Rune* runestrcpy(Rune* s1, const Rune* s2);
    176 Rune* runestrncpy(Rune* s1, const Rune* s2, long n);
    177 Rune* runestrecpy(Rune* s1, Rune* es1, const Rune* s2);
    178 
    179 Rune* runestrdup(const Rune* s);
    180 
    181 const Rune* runestrrchr(const Rune* s, Rune c);
    182 long runestrlen(const Rune* s);
    183 const Rune* runestrstr(const Rune* s1, const Rune* s2);
    184 
    185 
    186 
    187 // The following routines test types and modify cases for Unicode
    188 // characters.  Unicode defines some characters as letters and
    189 // specifies three cases: upper, lower, and title.  Mappings among the
    190 // cases are also defined, although they are not exhaustive: some
    191 // upper case letters have no lower case mapping, and so on.  Unicode
    192 // also defines several character properties, a subset of which are
    193 // checked by these routines.  These routines are based on Unicode
    194 // version 3.0.0.
    195 //
    196 // NOTE: The routines are implemented in C, so the boolean functions
    197 // (e.g., isupperrune) return 0 for false and 1 for true.
    198 //
    199 //
    200 // toupperrune, tolowerrune, and totitlerune are the Unicode case
    201 // mappings. These routines return the character unchanged if it has
    202 // no defined mapping.
    203 
    204 Rune toupperrune(Rune r);
    205 Rune tolowerrune(Rune r);
    206 Rune totitlerune(Rune r);
    207 
    208 
    209 // isupperrune tests for upper case characters, including Unicode
    210 // upper case letters and targets of the toupper mapping. islowerrune
    211 // and istitlerune are defined analogously.
    212 
    213 int isupperrune(Rune r);
    214 int islowerrune(Rune r);
    215 int istitlerune(Rune r);
    216 
    217 
    218 // isalpharune tests for Unicode letters; this includes ideographs in
    219 // addition to alphabetic characters.
    220 
    221 int isalpharune(Rune r);
    222 
    223 
    224 // isdigitrune tests for digits. Non-digit numbers, such as Roman
    225 // numerals, are not included.
    226 
    227 int isdigitrune(Rune r);
    228 
    229 
    230 // isideographicrune tests for ideographic characters and numbers, as
    231 // defined by the Unicode standard.
    232 
    233 int isideographicrune(Rune r);
    234 
    235 
    236 // isspacerune tests for whitespace characters, including "C" locale
    237 // whitespace, Unicode defined whitespace, and the "zero-width
    238 // non-break space" character.
    239 
    240 int isspacerune(Rune r);
    241 
    242 
    243 // (The comments in this file were copied from the manpage files rune.3,
    244 // isalpharune.3, and runestrcat.3. Some formatting changes were also made
    245 // to conform to Google style. /JRM 11/11/05)
    246 
    247 #ifdef	__cplusplus
    248 }
    249 #endif
    250 
    251 #endif
    252