Path: blob/master/waterbox/libc/internals/_PDCLIB_locale.h
2 views
/* PDCLib locale support <_PDCLIB_locale.h>12This file is part of the Public Domain C Library (PDCLib).3Permission is granted to use, modify, and / or redistribute at will.4*/56#ifndef __PDCLIB_LOCALE_H7#define __PDCLIB_LOCALE_H __PDCLIB_LOCALE_H89#include "_PDCLIB_int.h"1011#include <locale.h>12#include <wctype.h>13#include <threads.h>14#include <stdlib.h>1516#define _PDCLIB_LOCALE_METHOD_TSS 't'17#define _PDCLIB_LOCALE_METHOD_THREAD_LOCAL 'T'18#define _PDCLIB_LOCALE_METHOD_FAKE 'F'1920#if !defined(_PDCLIB_LOCALE_METHOD)21/* If undefined, no POSIX per thread locales */22#define _PDCLIB_threadlocale() (&_PDCLIB_global_locale)23#elif _PDCLIB_LOCALE_METHOD == _PDCLIB_LOCALE_METHOD_TSS24extern tss_t _PDCLIB_locale_tss;25static inline locale_t _PDCLIB_threadlocale( void )26{27locale_t l = tss_get(_PDCLIB_locale_tss);28if ( l == NULL )29l = &_PDCLIB_global_locale;30return l;31}3233static inline void _PDCLIB_setthreadlocale( locale_t l )34{35if ( tss_set( _PDCLIB_locale_tss, l ) != thrd_success )36abort();37}38#elif _PDCLIB_LOCALE_METHOD == _PDCLIB_LOCALE_METHOD_THREAD_LOCAL39extern thread_local locale_t _PDCLIB_locale_tls;40#define _PDCLIB_threadlocale() ( _PDCLIB_locale_tls || &_PDCLIB_global_locale )41static inline locale_t _PDCLIB_threadlocale( void )42{43locale_t l = _PDCLIB_locale_tls;44if(l == NULL)45l = &_PDCLIB_global_locale;46return l;47}4849static inline void _PDCLIB_setthreadlocale( locale_t l )50{51_PDCLIB_locale_tls = l;52}53#elif _PDCLIB_LOCALE_METHOD == _PDCLIB_LOCALE_METHOD_FAKE54extern locale_t _PDCLIB_locale_fake;55static inline locale_t _PDCLIB_threadlocale( void )56{57locale_t l = _PDCLIB_locale_fake;58if(l == NULL)59l = &_PDCLIB_global_locale;60return l;61}6263static inline void _PDCLIB_setthreadlocale( locale_t l )64{65_PDCLIB_locale_fake = l;66}67#else68#error Locale TSS method unspecified69#endif7071/* -------------------------------------------------------------------------- */72/* <ctype.h> lookup tables */73/* -------------------------------------------------------------------------- */7475#define _PDCLIB_CTYPE_ALPHA 176#define _PDCLIB_CTYPE_BLANK 277#define _PDCLIB_CTYPE_CNTRL 478#define _PDCLIB_CTYPE_GRAPH 879#define _PDCLIB_CTYPE_PUNCT 1680#define _PDCLIB_CTYPE_SPACE 3281#define _PDCLIB_CTYPE_LOWER 6482#define _PDCLIB_CTYPE_UPPER 12883#define _PDCLIB_CTYPE_DIGIT 25684#define _PDCLIB_CTYPE_XDIGT 5128586#define _PDCLIB_WCTRANS_TOLOWER 187#define _PDCLIB_WCTRANS_TOUPPER 28889typedef struct _PDCLIB_ctype90{91_PDCLIB_uint16_t flags;92unsigned char upper;93unsigned char lower;94unsigned char collation;95} _PDCLIB_ctype_t;9697typedef struct _PDCLIB_wcinfo98{99_PDCLIB_wint_t start;100_PDCLIB_uint16_t length;101_PDCLIB_uint16_t flags;102_PDCLIB_wint_t lower_delta;103_PDCLIB_wint_t upper_delta;104} _PDCLIB_wcinfo_t;105106struct _PDCLIB_locale {107const struct _PDCLIB_charcodec_t * _Codec;108struct lconv _Conv;109110/* ctype / wctype */111/* XXX: Maybe re-evaluate constness of these later on? */112const _PDCLIB_wcinfo_t *_WCType;113_PDCLIB_size_t _WCTypeSize;114const _PDCLIB_ctype_t *_CType;115116/* perror/strerror */117const char * const _ErrnoStr[_PDCLIB_ERRNO_MAX];118};119120extern const _PDCLIB_wcinfo_t _PDCLIB_wcinfo[];121extern const size_t _PDCLIB_wcinfo_size;122123static inline int _PDCLIB_wcinfo_cmp( const void * _key, const void * _obj )124{125_PDCLIB_int32_t * key = (_PDCLIB_int32_t *) _key;126_PDCLIB_wcinfo_t * obj = (_PDCLIB_wcinfo_t *) _obj;127if ( *key < obj->start )128{129return -1;130}131else if ( *key >= obj->start + obj->length )132{133return 1;134}135else136{137return 0;138}139}140141static inline _PDCLIB_wcinfo_t * _PDCLIB_wcgetinfo( locale_t l, _PDCLIB_int32_t num )142{143_PDCLIB_wcinfo_t *info = (_PDCLIB_wcinfo_t*)144bsearch( &num, l->_WCType, l->_WCTypeSize,145sizeof( l->_WCType[0] ), _PDCLIB_wcinfo_cmp );146147return info;148}149150static inline wint_t _PDCLIB_unpackwint( wint_t wc )151{152if( sizeof(_PDCLIB_wchar_t) == 2 && sizeof(_PDCLIB_wint_t) == 4 ) {153/* On UTF-16 platforms, as an extension accept a "packed surrogate"154* encoding. We accept the surrogate pairs either way155*/156157wint_t c = (wc & 0xF800F800);158if(c == (_PDCLIB_wint_t) 0xD800DC00) {159// MSW: Lead, LSW: Trail160wint_t lead = wc >> 16 & 0x3FF;161wint_t trail = wc & 0x3FF;162wc = lead << 10 | trail;163} else if(c == (_PDCLIB_wint_t) 0xDC00D800) {164// MSW: Trail, LSW: Lead165wint_t trail = wc >> 16 & 0x3FF;166wint_t lead = wc & 0x3FF;167wc = lead << 10 | trail;168}169170}171return wc;172}173174/* Internal xlocale-style WCType API */175int _PDCLIB_iswalnum_l( wint_t _Wc, locale_t l );176int _PDCLIB_iswalpha_l( wint_t _Wc, locale_t l );177int _PDCLIB_iswblank_l( wint_t _Wc, locale_t l );178int _PDCLIB_iswcntrl_l( wint_t _Wc, locale_t l );179int _PDCLIB_iswdigit_l( wint_t _Wc, locale_t l );180int _PDCLIB_iswgraph_l( wint_t _Wc, locale_t l );181int _PDCLIB_iswlower_l( wint_t _Wc, locale_t l );182int _PDCLIB_iswprint_l( wint_t _Wc, locale_t l );183int _PDCLIB_iswpunct_l( wint_t _Wc, locale_t l );184int _PDCLIB_iswspace_l( wint_t _Wc, locale_t l );185int _PDCLIB_iswupper_l( wint_t _Wc, locale_t l );186int _PDCLIB_iswxdigit_l( wint_t _Wc, locale_t l );187int _PDCLIB_iswctype_l( wint_t _Wc, wctype_t _Desc, locale_t l );188wint_t _PDCLIB_towlower_l( wint_t _Wc, locale_t l );189wint_t _PDCLIB_towupper_l( wint_t _Wc, locale_t l );190wint_t _PDCLIB_towctrans_l( wint_t _Wc, wctrans_t _Desc, locale_t l );191192#endif193194195