11767c5feSopenharmony_ci/* 21767c5feSopenharmony_ci * The authors of this software are Rob Pike and Ken Thompson. 31767c5feSopenharmony_ci * Copyright (c) 1998-2002 by Lucent Technologies. 41767c5feSopenharmony_ci * Portions Copyright (c) 2009 The Go Authors. All rights reserved. 51767c5feSopenharmony_ci * Permission to use, copy, modify, and distribute this software for any 61767c5feSopenharmony_ci * purpose without fee is hereby granted, provided that this entire notice 71767c5feSopenharmony_ci * is included in all copies of any software which is or includes a copy 81767c5feSopenharmony_ci * or modification of this software and in all copies of the supporting 91767c5feSopenharmony_ci * documentation for such software. 101767c5feSopenharmony_ci * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED 111767c5feSopenharmony_ci * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY 121767c5feSopenharmony_ci * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY 131767c5feSopenharmony_ci * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. 141767c5feSopenharmony_ci */ 151767c5feSopenharmony_ci 161767c5feSopenharmony_ci#ifndef _UTFH_ 171767c5feSopenharmony_ci#define _UTFH_ 1 181767c5feSopenharmony_ci 191767c5feSopenharmony_citypedef unsigned int Rune; /* Code-point values in Unicode 4.0 are 21 bits wide.*/ 201767c5feSopenharmony_ci 211767c5feSopenharmony_cienum 221767c5feSopenharmony_ci{ 231767c5feSopenharmony_ci UTFmax = 4, /* maximum bytes per rune */ 241767c5feSopenharmony_ci Runesync = 0x80, /* cannot represent part of a UTF sequence (<) */ 251767c5feSopenharmony_ci Runeself = 0x80, /* rune and UTF sequences are the same (<) */ 261767c5feSopenharmony_ci Runeerror = 0xFFFD, /* decoding error in UTF */ 271767c5feSopenharmony_ci Runemax = 0x10FFFF, /* maximum rune value */ 281767c5feSopenharmony_ci}; 291767c5feSopenharmony_ci 301767c5feSopenharmony_ci#ifdef __cplusplus 311767c5feSopenharmony_ciextern "C" { 321767c5feSopenharmony_ci#endif 331767c5feSopenharmony_ci 341767c5feSopenharmony_ci/* 351767c5feSopenharmony_ci * rune routines 361767c5feSopenharmony_ci */ 371767c5feSopenharmony_ci 381767c5feSopenharmony_ci/* 391767c5feSopenharmony_ci * These routines were written by Rob Pike and Ken Thompson 401767c5feSopenharmony_ci * and first appeared in Plan 9. 411767c5feSopenharmony_ci * SEE ALSO 421767c5feSopenharmony_ci * utf (7) 431767c5feSopenharmony_ci * tcs (1) 441767c5feSopenharmony_ci*/ 451767c5feSopenharmony_ci 461767c5feSopenharmony_ci// runetochar copies (encodes) one rune, pointed to by r, to at most 471767c5feSopenharmony_ci// UTFmax bytes starting at s and returns the number of bytes generated. 481767c5feSopenharmony_ci 491767c5feSopenharmony_ciint runetochar(char* s, const Rune* r); 501767c5feSopenharmony_ci 511767c5feSopenharmony_ci 521767c5feSopenharmony_ci// chartorune copies (decodes) at most UTFmax bytes starting at s to 531767c5feSopenharmony_ci// one rune, pointed to by r, and returns the number of bytes consumed. 541767c5feSopenharmony_ci// If the input is not exactly in UTF format, chartorune will set *r 551767c5feSopenharmony_ci// to Runeerror and return 1. 561767c5feSopenharmony_ci// 571767c5feSopenharmony_ci// Note: There is no special case for a "null-terminated" string. A 581767c5feSopenharmony_ci// string whose first byte has the value 0 is the UTF8 encoding of the 591767c5feSopenharmony_ci// Unicode value 0 (i.e., ASCII NULL). A byte value of 0 is illegal 601767c5feSopenharmony_ci// anywhere else in a UTF sequence. 611767c5feSopenharmony_ci 621767c5feSopenharmony_ciint chartorune(Rune* r, const char* s); 631767c5feSopenharmony_ci 641767c5feSopenharmony_ci 651767c5feSopenharmony_ci// charntorune is like chartorune, except that it will access at most 661767c5feSopenharmony_ci// n bytes of s. If the UTF sequence is incomplete within n bytes, 671767c5feSopenharmony_ci// charntorune will set *r to Runeerror and return 0. If it is complete 681767c5feSopenharmony_ci// but not in UTF format, it will set *r to Runeerror and return 1. 691767c5feSopenharmony_ci// 701767c5feSopenharmony_ci// Added 2004-09-24 by Wei-Hwa Huang 711767c5feSopenharmony_ci 721767c5feSopenharmony_ciint charntorune(Rune* r, const char* s, int n); 731767c5feSopenharmony_ci 741767c5feSopenharmony_ci// isvalidcharntorune(str, n, r, consumed) 751767c5feSopenharmony_ci// is a convenience function that calls "*consumed = charntorune(r, str, n)" 761767c5feSopenharmony_ci// and returns an int (logically boolean) indicating whether the first 771767c5feSopenharmony_ci// n bytes of str was a valid and complete UTF sequence. 781767c5feSopenharmony_ci 791767c5feSopenharmony_ciint isvalidcharntorune(const char* str, int n, Rune* r, int* consumed); 801767c5feSopenharmony_ci 811767c5feSopenharmony_ci// runelen returns the number of bytes required to convert r into UTF. 821767c5feSopenharmony_ci 831767c5feSopenharmony_ciint runelen(Rune r); 841767c5feSopenharmony_ci 851767c5feSopenharmony_ci 861767c5feSopenharmony_ci// runenlen returns the number of bytes required to convert the n 871767c5feSopenharmony_ci// runes pointed to by r into UTF. 881767c5feSopenharmony_ci 891767c5feSopenharmony_ciint runenlen(const Rune* r, int n); 901767c5feSopenharmony_ci 911767c5feSopenharmony_ci 921767c5feSopenharmony_ci// fullrune returns 1 if the string s of length n is long enough to be 931767c5feSopenharmony_ci// decoded by chartorune, and 0 otherwise. This does not guarantee 941767c5feSopenharmony_ci// that the string contains a legal UTF encoding. This routine is used 951767c5feSopenharmony_ci// by programs that obtain input one byte at a time and need to know 961767c5feSopenharmony_ci// when a full rune has arrived. 971767c5feSopenharmony_ci 981767c5feSopenharmony_ciint fullrune(const char* s, int n); 991767c5feSopenharmony_ci 1001767c5feSopenharmony_ci// The following routines are analogous to the corresponding string 1011767c5feSopenharmony_ci// routines with "utf" substituted for "str", and "rune" substituted 1021767c5feSopenharmony_ci// for "chr". 1031767c5feSopenharmony_ci 1041767c5feSopenharmony_ci// utflen returns the number of runes that are represented by the UTF 1051767c5feSopenharmony_ci// string s. (cf. strlen) 1061767c5feSopenharmony_ci 1071767c5feSopenharmony_ciint utflen(const char* s); 1081767c5feSopenharmony_ci 1091767c5feSopenharmony_ci 1101767c5feSopenharmony_ci// utfnlen returns the number of complete runes that are represented 1111767c5feSopenharmony_ci// by the first n bytes of the UTF string s. If the last few bytes of 1121767c5feSopenharmony_ci// the string contain an incompletely coded rune, utfnlen will not 1131767c5feSopenharmony_ci// count them; in this way, it differs from utflen, which includes 1141767c5feSopenharmony_ci// every byte of the string. (cf. strnlen) 1151767c5feSopenharmony_ci 1161767c5feSopenharmony_ciint utfnlen(const char* s, long n); 1171767c5feSopenharmony_ci 1181767c5feSopenharmony_ci 1191767c5feSopenharmony_ci// utfrune returns a pointer to the first occurrence of rune r in the 1201767c5feSopenharmony_ci// UTF string s, or 0 if r does not occur in the string. The NULL 1211767c5feSopenharmony_ci// byte terminating a string is considered to be part of the string s. 1221767c5feSopenharmony_ci// (cf. strchr) 1231767c5feSopenharmony_ci 1241767c5feSopenharmony_ci/*const*/ char* utfrune(const char* s, Rune r); 1251767c5feSopenharmony_ci 1261767c5feSopenharmony_ci 1271767c5feSopenharmony_ci// utfrrune returns a pointer to the last occurrence of rune r in the 1281767c5feSopenharmony_ci// UTF string s, or 0 if r does not occur in the string. The NULL 1291767c5feSopenharmony_ci// byte terminating a string is considered to be part of the string s. 1301767c5feSopenharmony_ci// (cf. strrchr) 1311767c5feSopenharmony_ci 1321767c5feSopenharmony_ci/*const*/ char* utfrrune(const char* s, Rune r); 1331767c5feSopenharmony_ci 1341767c5feSopenharmony_ci 1351767c5feSopenharmony_ci// utfutf returns a pointer to the first occurrence of the UTF string 1361767c5feSopenharmony_ci// s2 as a UTF substring of s1, or 0 if there is none. If s2 is the 1371767c5feSopenharmony_ci// null string, utfutf returns s1. (cf. strstr) 1381767c5feSopenharmony_ci 1391767c5feSopenharmony_ciconst char* utfutf(const char* s1, const char* s2); 1401767c5feSopenharmony_ci 1411767c5feSopenharmony_ci 1421767c5feSopenharmony_ci// utfecpy copies UTF sequences until a null sequence has been copied, 1431767c5feSopenharmony_ci// but writes no sequences beyond es1. If any sequences are copied, 1441767c5feSopenharmony_ci// s1 is terminated by a null sequence, and a pointer to that sequence 1451767c5feSopenharmony_ci// is returned. Otherwise, the original s1 is returned. (cf. strecpy) 1461767c5feSopenharmony_ci 1471767c5feSopenharmony_cichar* utfecpy(char *s1, char *es1, const char *s2); 1481767c5feSopenharmony_ci 1491767c5feSopenharmony_ci 1501767c5feSopenharmony_ci 1511767c5feSopenharmony_ci// These functions are rune-string analogues of the corresponding 1521767c5feSopenharmony_ci// functions in strcat (3). 1531767c5feSopenharmony_ci// 1541767c5feSopenharmony_ci// These routines first appeared in Plan 9. 1551767c5feSopenharmony_ci// SEE ALSO 1561767c5feSopenharmony_ci// memmove (3) 1571767c5feSopenharmony_ci// rune (3) 1581767c5feSopenharmony_ci// strcat (2) 1591767c5feSopenharmony_ci// 1601767c5feSopenharmony_ci// BUGS: The outcome of overlapping moves varies among implementations. 1611767c5feSopenharmony_ci 1621767c5feSopenharmony_ciRune* runestrcat(Rune* s1, const Rune* s2); 1631767c5feSopenharmony_ciRune* runestrncat(Rune* s1, const Rune* s2, long n); 1641767c5feSopenharmony_ci 1651767c5feSopenharmony_ciconst Rune* runestrchr(const Rune* s, Rune c); 1661767c5feSopenharmony_ci 1671767c5feSopenharmony_ciint runestrcmp(const Rune* s1, const Rune* s2); 1681767c5feSopenharmony_ciint runestrncmp(const Rune* s1, const Rune* s2, long n); 1691767c5feSopenharmony_ci 1701767c5feSopenharmony_ciRune* runestrcpy(Rune* s1, const Rune* s2); 1711767c5feSopenharmony_ciRune* runestrncpy(Rune* s1, const Rune* s2, long n); 1721767c5feSopenharmony_ciRune* runestrecpy(Rune* s1, Rune* es1, const Rune* s2); 1731767c5feSopenharmony_ci 1741767c5feSopenharmony_ciRune* runestrdup(const Rune* s); 1751767c5feSopenharmony_ci 1761767c5feSopenharmony_ciconst Rune* runestrrchr(const Rune* s, Rune c); 1771767c5feSopenharmony_cilong runestrlen(const Rune* s); 1781767c5feSopenharmony_ciconst Rune* runestrstr(const Rune* s1, const Rune* s2); 1791767c5feSopenharmony_ci 1801767c5feSopenharmony_ci 1811767c5feSopenharmony_ci 1821767c5feSopenharmony_ci// The following routines test types and modify cases for Unicode 1831767c5feSopenharmony_ci// characters. Unicode defines some characters as letters and 1841767c5feSopenharmony_ci// specifies three cases: upper, lower, and title. Mappings among the 1851767c5feSopenharmony_ci// cases are also defined, although they are not exhaustive: some 1861767c5feSopenharmony_ci// upper case letters have no lower case mapping, and so on. Unicode 1871767c5feSopenharmony_ci// also defines several character properties, a subset of which are 1881767c5feSopenharmony_ci// checked by these routines. These routines are based on Unicode 1891767c5feSopenharmony_ci// version 3.0.0. 1901767c5feSopenharmony_ci// 1911767c5feSopenharmony_ci// NOTE: The routines are implemented in C, so the boolean functions 1921767c5feSopenharmony_ci// (e.g., isupperrune) return 0 for false and 1 for true. 1931767c5feSopenharmony_ci// 1941767c5feSopenharmony_ci// 1951767c5feSopenharmony_ci// toupperrune, tolowerrune, and totitlerune are the Unicode case 1961767c5feSopenharmony_ci// mappings. These routines return the character unchanged if it has 1971767c5feSopenharmony_ci// no defined mapping. 1981767c5feSopenharmony_ci 1991767c5feSopenharmony_ciRune toupperrune(Rune r); 2001767c5feSopenharmony_ciRune tolowerrune(Rune r); 2011767c5feSopenharmony_ciRune totitlerune(Rune r); 2021767c5feSopenharmony_ci 2031767c5feSopenharmony_ci 2041767c5feSopenharmony_ci// isupperrune tests for upper case characters, including Unicode 2051767c5feSopenharmony_ci// upper case letters and targets of the toupper mapping. islowerrune 2061767c5feSopenharmony_ci// and istitlerune are defined analogously. 2071767c5feSopenharmony_ci 2081767c5feSopenharmony_ciint isupperrune(Rune r); 2091767c5feSopenharmony_ciint islowerrune(Rune r); 2101767c5feSopenharmony_ciint istitlerune(Rune r); 2111767c5feSopenharmony_ci 2121767c5feSopenharmony_ci 2131767c5feSopenharmony_ci// isalpharune tests for Unicode letters; this includes ideographs in 2141767c5feSopenharmony_ci// addition to alphabetic characters. 2151767c5feSopenharmony_ci 2161767c5feSopenharmony_ciint isalpharune(Rune r); 2171767c5feSopenharmony_ci 2181767c5feSopenharmony_ci 2191767c5feSopenharmony_ci// isdigitrune tests for digits. Non-digit numbers, such as Roman 2201767c5feSopenharmony_ci// numerals, are not included. 2211767c5feSopenharmony_ci 2221767c5feSopenharmony_ciint isdigitrune(Rune r); 2231767c5feSopenharmony_ci 2241767c5feSopenharmony_ci 2251767c5feSopenharmony_ci// isspacerune tests for whitespace characters, including "C" locale 2261767c5feSopenharmony_ci// whitespace, Unicode defined whitespace, and the "zero-width 2271767c5feSopenharmony_ci// non-break space" character. 2281767c5feSopenharmony_ci 2291767c5feSopenharmony_ciint isspacerune(Rune r); 2301767c5feSopenharmony_ci 2311767c5feSopenharmony_ci 2321767c5feSopenharmony_ci// (The comments in this file were copied from the manpage files rune.3, 2331767c5feSopenharmony_ci// isalpharune.3, and runestrcat.3. Some formatting changes were also made 2341767c5feSopenharmony_ci// to conform to Google style. /JRM 11/11/05) 2351767c5feSopenharmony_ci 2361767c5feSopenharmony_ci#ifdef __cplusplus 2371767c5feSopenharmony_ci} 2381767c5feSopenharmony_ci#endif 2391767c5feSopenharmony_ci 2401767c5feSopenharmony_ci#endif 241