chartype.h revision 1.14
1/* $OpenBSD: chartype.h,v 1.14 2016/04/09 19:31:55 schwarze Exp $ */ 2/* $NetBSD: chartype.h,v 1.5 2010/04/15 00:55:57 christos Exp $ */ 3 4/*- 5 * Copyright (c) 2009 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#ifndef _h_chartype_f 31#define _h_chartype_f 32 33 34#ifndef NARROWCHAR 35 36/* Ideally we should also test the value of the define to see if it 37 * supports non-BMP code points without requiring UTF-16, but nothing 38 * seems to actually advertise this properly, despite Unicode 3.1 having 39 * been around since 2001... */ 40#if !defined(__NetBSD__) && !defined(__OpenBSD__) 41#ifndef __STDC_ISO_10646__ 42/* In many places it is assumed that the first 127 code points are ASCII 43 * compatible, so ensure wchar_t indeed does ISO 10646 and not some other 44 * funky encoding that could break us in weird and wonderful ways. */ 45 #error wchar_t must store ISO 10646 characters 46#endif 47#endif 48 49/* Oh for a <uchar.h> with char32_t and __STDC_UTF_32__ in it... 50 * ref: ISO/IEC DTR 19769 51 */ 52#if WCHAR_MAX < INT32_MAX 53#warning Build environment does not support non-BMP characters 54#endif 55 56#define ct_mbrtowc mbrtowc 57#define ct_wctob wctob 58#define ct_wctomb wctomb 59#define ct_wctomb_reset wctomb(0,0) 60#define ct_wcstombs wcstombs 61#define ct_mbstowcs mbstowcs 62 63#define Char wchar_t 64#define FUN(prefix,rest) prefix ## _w ## rest 65#define FUNW(type) type ## _w 66#define TYPE(type) type ## W 67#define FSTR "%ls" 68#define FSTARSTR "%.*ls" 69#define STR(x) L ## x 70#define UC(c) c 71#define Isalpha(x) iswalpha(x) 72#define Isalnum(x) iswalnum(x) 73#define Isgraph(x) iswgraph(x) 74#define Isspace(x) iswspace(x) 75#define Isdigit(x) iswdigit(x) 76#define Iscntrl(x) iswcntrl(x) 77#define Isprint(x) iswprint(x) 78 79#define Isupper(x) iswupper(x) 80#define Islower(x) iswlower(x) 81#define Toupper(x) towupper(x) 82#define Tolower(x) towlower(x) 83 84#define IsASCII(x) (x < 0x100) 85 86#define Strlen(x) wcslen(x) 87#define Strchr(s,c) wcschr(s,c) 88#define Strrchr(s,c) wcsrchr(s,c) 89#define Strstr(s,v) wcsstr(s,v) 90#define Strdup(x) wcsdup(x) 91/* #define Strcpy(d,s) wcscpy(d,s) */ 92#define Strncpy(d,s,n) wcsncpy(d,s,n) 93#define Strncat(d,s,n) wcsncat(d,s,n) 94 95#define Strcmp(s,v) wcscmp(s,v) 96#define Strncmp(s,v,n) wcsncmp(s,v,n) 97#define Strcspn(s,r) wcscspn(s,r) 98 99#define Strtol(p,e,b) wcstol(p,e,b) 100 101#define Width(c) (wcwidth(c) == -1 ? 0 : wcwidth(c)) 102 103#else /* NARROW */ 104 105size_t ct_mbrtowc(wchar_t *, const char *, size_t, void *); 106 107#define Char char 108#define FUN(prefix,rest) prefix ## _ ## rest 109#define FUNW(type) type 110#define TYPE(type) type 111#define STR(x) x 112 113#define Strlen(x) strlen(x) 114#define Strchr(s,c) strchr(s,c) 115#define Strdup(x) strdup(x) 116#define Strncpy(d,s,n) strncpy(d,s,n) 117#define Strncat(d,s,n) strncat(d,s,n) 118 119#define Strcmp(s,v) strcmp(s,v) 120#define Strncmp(s,v,n) strncmp(s,v,n) 121#endif 122 123 124#ifndef NARROWCHAR 125/* 126 * Conversion buffer 127 */ 128typedef struct ct_buffer_t { 129 char *cbuff; 130 size_t csize; 131 Char *wbuff; 132 size_t wsize; 133} ct_buffer_t; 134 135#define ct_encode_string __ct_encode_string 136/* Encode a wide-character string and return the UTF-8 encoded result. */ 137public char *ct_encode_string(const Char *, ct_buffer_t *); 138 139#define ct_decode_string __ct_decode_string 140/* Decode a (multi)?byte string and return the wide-character string result. */ 141public Char *ct_decode_string(const char *, ct_buffer_t *); 142 143/* Decode a (multi)?byte argv string array. 144 * The pointer returned must be free()d when done. */ 145protected Char **ct_decode_argv(int, const char *[], ct_buffer_t *); 146 147/* Resizes the conversion buffer(s) if needed. */ 148protected void ct_conv_buff_resize(ct_buffer_t *, size_t, size_t); 149protected ssize_t ct_encode_char(char *, size_t, Char); 150protected size_t ct_enc_width(Char); 151 152#define ct_free_argv(s) free(s) 153 154#else 155#define ct_encode_string(s, b) (s) 156#define ct_decode_string(s, b) (s) 157#endif 158 159#ifndef NARROWCHAR 160/* Encode a characted into the destination buffer, provided there is sufficent 161 * buffer space available. Returns the number of bytes used up (zero if the 162 * character cannot be encoded, -1 if there was not enough space available). */ 163 164/* The maximum buffer size to hold the most unwieldly visual representation, 165 * in this case \U+nnnnn. */ 166#define VISUAL_WIDTH_MAX 8 167 168/* The terminal is thought of in terms of X columns by Y lines. In the cases 169 * where a wide character takes up more than one column, the adjacent 170 * occupied column entries will contain this faux character. */ 171#define MB_FILL_CHAR ((Char)-1) 172 173/* Visual width of character c, taking into account ^? , \0177 and \U+nnnnn 174 * style visual expansions. */ 175protected int ct_visual_width(Char); 176 177/* Turn the given character into the appropriate visual format, matching 178 * the width given by ct_visual_width(). Returns the number of characters used 179 * up, or -1 if insufficient space. Buffer length is in count of Char's. */ 180protected ssize_t ct_visual_char(Char *, size_t, Char); 181 182/* Convert the given string into visual format, using the ct_visual_char() 183 * function. Uses a static buffer, so not threadsafe. */ 184protected const Char *ct_visual_string(const Char *); 185 186 187/* printable character, use ct_visual_width() to find out display width */ 188#define CHTYPE_PRINT ( 0) 189/* control character found inside the ASCII portion of the charset */ 190#define CHTYPE_ASCIICTL (-1) 191/* a \t */ 192#define CHTYPE_TAB (-2) 193/* a \n */ 194#define CHTYPE_NL (-3) 195/* non-printable character */ 196#define CHTYPE_NONPRINT (-4) 197/* classification of character c, as one of the above defines */ 198protected int ct_chr_class(Char c); 199#endif 200 201 202#endif /* _chartype_f */ 203