Home | History | Annotate | Line # | Download | only in locale
_wctrans.c revision 1.8
      1 /*	$NetBSD: _wctrans.c,v 1.8 2009/01/11 02:46:28 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c)2003 Citrus Project,
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 /*-
     30  * Copyright (c) 1993
     31  *	The Regents of the University of California.  All rights reserved.
     32  *
     33  * This code is derived from software contributed to Berkeley by
     34  * Paul Borman at Krystal Technologies.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  */
     60 
     61 #include <sys/cdefs.h>
     62 #if defined(LIBC_SCCS) && !defined(lint)
     63 __RCSID("$NetBSD: _wctrans.c,v 1.8 2009/01/11 02:46:28 christos Exp $");
     64 #endif /* LIBC_SCCS and not lint */
     65 
     66 #include "namespace.h"
     67 
     68 #include <wctype.h>
     69 #include <stdlib.h>
     70 #include <string.h>
     71 #include "rune.h"
     72 #include "rune_local.h"
     73 #include "_wctrans_local.h"
     74 
     75 /*
     76  * _wctrans_init:
     77  */
     78 
     79 void
     80 _wctrans_init(_RuneLocale *rl)
     81 {
     82 	rl->rl_wctrans[_WCTRANS_INDEX_LOWER].te_name = "tolower";
     83 	rl->rl_wctrans[_WCTRANS_INDEX_LOWER].te_cached = rl->rl_maplower;
     84 	rl->rl_wctrans[_WCTRANS_INDEX_LOWER].te_extmap = &rl->rl_maplower_ext;
     85 	rl->rl_wctrans[_WCTRANS_INDEX_UPPER].te_name = "toupper";
     86 	rl->rl_wctrans[_WCTRANS_INDEX_UPPER].te_cached = rl->rl_mapupper;
     87 	rl->rl_wctrans[_WCTRANS_INDEX_UPPER].te_extmap = &rl->rl_mapupper_ext;
     88 }
     89 
     90 /*
     91  * _towctrans_ext:
     92  *	translate a character (extended part)
     93  */
     94 wint_t
     95 _towctrans_ext(wint_t c, struct _WCTransEntry *te)
     96 {
     97 	__nbrune_t c0;
     98 	uint32_t x;
     99 	_RuneRange *rr;
    100 	_RuneEntry *base, *re;
    101 
    102 	if (c == WEOF)
    103 		return (c);
    104 
    105 	c0 = (__nbrune_t)c; /* XXX assumes wchar_t = int */
    106 	rr = te->te_extmap;
    107 	base = rr->rr_rune_ranges;
    108 	for (x = rr->rr_nranges; x != 0; x >>= 1) {
    109 		re = base + (x >> 1);
    110 		if (re->re_min <= c0 && re->re_max >= c0)
    111 			return (re->re_map + c0 - re->re_min);
    112 		else if (c0 >= re->re_max) {
    113 			base = re + 1;
    114 			x--;
    115 		}
    116 	}
    117 
    118 	return (c);
    119 }
    120