199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2011 The FreeBSD Foundation
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This software was developed by David Chisnall under sponsorship from
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the FreeBSD Foundation.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * $FreeBSD$
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if (defined(_XLOCALE_WCTYPES) && !defined(_XLOCALE_WCTYPE_H)) || \
199767f8919635c4928607450d9e0abb932109ceToomas Soome (!defined(_XLOCALE_WCTYPES) && !defined(_XLOCALE_CTYPE_H))
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef _XLOCALE_WCTYPES
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define _XLOCALE_WCTYPE_H
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define _XLOCALE_CTYPE_H
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef _LOCALE_T_DEFINED
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define _LOCALE_T_DEFINED
199767f8919635c4928607450d9e0abb932109ceToomas Soometypedef struct _xlocale *locale_t;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef _XLOCALE_RUN_FUNCTIONS_DEFINED
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define _XLOCALE_RUN_FUNCTIONS_DEFINED 1
199767f8919635c4928607450d9e0abb932109ceToomas Soomeunsigned long ___runetype_l(__ct_rune_t, locale_t) __pure;
199767f8919635c4928607450d9e0abb932109ceToomas Soome__ct_rune_t ___tolower_l(__ct_rune_t, locale_t) __pure;
199767f8919635c4928607450d9e0abb932109ceToomas Soome__ct_rune_t ___toupper_l(__ct_rune_t, locale_t) __pure;
199767f8919635c4928607450d9e0abb932109ceToomas Soome_RuneLocale *__runes_for_locale(locale_t, int*);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef _XLOCALE_INLINE
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if defined(__GNUC__) && !defined(__GNUC_STDC_INLINE__)
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* GNU89 inline has nonstandard semantics. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define _XLOCALE_INLINE extern __inline
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Hack to work around people who define inline away */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef inline
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define _XLOCALE_INLINE static __inline
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Define with C++ / C99 compatible semantics */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define _XLOCALE_INLINE inline
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* _XLOCALE_INLINE */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef _XLOCALE_WCTYPES
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int
199767f8919635c4928607450d9e0abb932109ceToomas Soome__maskrune_l(__ct_rune_t __c, unsigned long __f, locale_t __loc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int
199767f8919635c4928607450d9e0abb932109ceToomas Soome__istype_l(__ct_rune_t __c, unsigned long __f, locale_t __loc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int
199767f8919635c4928607450d9e0abb932109ceToomas Soome__maskrune_l(__ct_rune_t __c, unsigned long __f, locale_t __loc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int __limit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome _RuneLocale *runes = __runes_for_locale(__loc, &__limit);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ((__c < 0 || __c >= _CACHED_RUNES) ? ___runetype_l(__c, __loc) :
199767f8919635c4928607450d9e0abb932109ceToomas Soome runes->__runetype[__c]) & __f;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int
199767f8919635c4928607450d9e0abb932109ceToomas Soome__istype_l(__ct_rune_t __c, unsigned long __f, locale_t __loc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (!!__maskrune_l(__c, __f, __loc));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define XLOCALE_ISCTYPE(fname, cat) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome _XLOCALE_INLINE int isw##fname##_l(int, locale_t);\
199767f8919635c4928607450d9e0abb932109ceToomas Soome _XLOCALE_INLINE int isw##fname##_l(int __c, locale_t __l)\
199767f8919635c4928607450d9e0abb932109ceToomas Soome { return __istype_l(__c, cat, __l); }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int
199767f8919635c4928607450d9e0abb932109ceToomas Soome__sbmaskrune_l(__ct_rune_t __c, unsigned long __f, locale_t __loc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int
199767f8919635c4928607450d9e0abb932109ceToomas Soome__sbistype_l(__ct_rune_t __c, unsigned long __f, locale_t __loc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int
199767f8919635c4928607450d9e0abb932109ceToomas Soome__sbmaskrune_l(__ct_rune_t __c, unsigned long __f, locale_t __loc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int __limit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome _RuneLocale *runes = __runes_for_locale(__loc, &__limit);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (__c < 0 || __c >= __limit) ? 0 :
199767f8919635c4928607450d9e0abb932109ceToomas Soome runes->__runetype[__c] & __f;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int
199767f8919635c4928607450d9e0abb932109ceToomas Soome__sbistype_l(__ct_rune_t __c, unsigned long __f, locale_t __loc)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (!!__sbmaskrune_l(__c, __f, __loc));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define XLOCALE_ISCTYPE(__fname, __cat) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome _XLOCALE_INLINE int is##__fname##_l(int, locale_t); \
199767f8919635c4928607450d9e0abb932109ceToomas Soome _XLOCALE_INLINE int is##__fname##_l(int __c, locale_t __l)\
199767f8919635c4928607450d9e0abb932109ceToomas Soome { return __sbistype_l(__c, __cat, __l); }
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(alnum, _CTYPE_A|_CTYPE_D|_CTYPE_N)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(alpha, _CTYPE_A)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(blank, _CTYPE_B)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(cntrl, _CTYPE_C)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(digit, _CTYPE_D)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(graph, _CTYPE_G)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(hexnumber, _CTYPE_X)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(ideogram, _CTYPE_I)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(lower, _CTYPE_L)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(number, _CTYPE_D|_CTYPE_N)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(phonogram, _CTYPE_Q)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(print, _CTYPE_R)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(punct, _CTYPE_P)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(rune, 0xFFFFFF00L)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(space, _CTYPE_S)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(special, _CTYPE_T)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(upper, _CTYPE_U)
199767f8919635c4928607450d9e0abb932109ceToomas SoomeXLOCALE_ISCTYPE(xdigit, _CTYPE_X)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#undef XLOCALE_ISCTYPE
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef _XLOCALE_WCTYPES
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int towlower_l(int, locale_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int __wcwidth_l(__ct_rune_t, locale_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int towupper_l(int, locale_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int towlower_l(int __c, locale_t __l)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int __limit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome _RuneLocale *__runes = __runes_for_locale(__l, &__limit);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (__c < 0 || __c >= _CACHED_RUNES) ? ___tolower_l(__c, __l) :
199767f8919635c4928607450d9e0abb932109ceToomas Soome __runes->__maplower[__c];
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int towupper_l(int __c, locale_t __l)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int __limit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome _RuneLocale *__runes = __runes_for_locale(__l, &__limit);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (__c < 0 || __c >= _CACHED_RUNES) ? ___toupper_l(__c, __l) :
199767f8919635c4928607450d9e0abb932109ceToomas Soome __runes->__mapupper[__c];
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int
199767f8919635c4928607450d9e0abb932109ceToomas Soome__wcwidth_l(__ct_rune_t _c, locale_t __l)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsigned int _x;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (_c == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome _x = (unsigned int)__maskrune_l(_c, _CTYPE_SWM|_CTYPE_R, __l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((_x & _CTYPE_SWM) != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ((_x & _CTYPE_SWM) >> _CTYPE_SWS);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return ((_x & _CTYPE_R) != 0 ? 1 : -1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint iswctype_l(wint_t __wc, wctype_t __charclass, locale_t __l);
199767f8919635c4928607450d9e0abb932109ceToomas Soomewctype_t wctype_l(const char *property, locale_t __l);
199767f8919635c4928607450d9e0abb932109ceToomas Soomewint_t towctrans_l(wint_t __wc, wctrans_t desc, locale_t __l);
199767f8919635c4928607450d9e0abb932109ceToomas Soomewint_t nextwctype_l(wint_t __wc, wctype_t wct, locale_t __l);
199767f8919635c4928607450d9e0abb932109ceToomas Soomewctrans_t wctrans_l(const char *__charclass, locale_t __l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#undef _XLOCALE_WCTYPES
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int digittoint_l(int, locale_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int tolower_l(int, locale_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int toupper_l(int, locale_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int digittoint_l(int __c, locale_t __l)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{ return __sbmaskrune_l((__c), 0xFF, __l); }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int tolower_l(int __c, locale_t __l)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int __limit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome _RuneLocale *__runes = __runes_for_locale(__l, &__limit);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (__c < 0 || __c >= __limit) ? __c :
199767f8919635c4928607450d9e0abb932109ceToomas Soome __runes->__maplower[__c];
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome_XLOCALE_INLINE int toupper_l(int __c, locale_t __l)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int __limit;
199767f8919635c4928607450d9e0abb932109ceToomas Soome _RuneLocale *__runes = __runes_for_locale(__l, &__limit);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (__c < 0 || __c >= __limit) ? __c :
199767f8919635c4928607450d9e0abb932109ceToomas Soome __runes->__mapupper[__c];
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* (defined(_XLOCALE_WCTYPES) && !defined(_XLOCALE_WCTYPE_H)) || \
199767f8919635c4928607450d9e0abb932109ceToomas Soome (!defined(_XLOCALE_WCTYPES) && !defined(_XLOCALE_CTYPE_H)) */