1*2a7af30cStnozaki /* $NetBSD: _wctrans.c,v 1.17 2010/06/13 04:14:57 tnozaki Exp $ */
231e2cbf0Stshiozak
331e2cbf0Stshiozak /*-
431e2cbf0Stshiozak * Copyright (c)2003 Citrus Project,
531e2cbf0Stshiozak * All rights reserved.
631e2cbf0Stshiozak *
731e2cbf0Stshiozak * Redistribution and use in source and binary forms, with or without
831e2cbf0Stshiozak * modification, are permitted provided that the following conditions
931e2cbf0Stshiozak * are met:
1031e2cbf0Stshiozak * 1. Redistributions of source code must retain the above copyright
1131e2cbf0Stshiozak * notice, this list of conditions and the following disclaimer.
1231e2cbf0Stshiozak * 2. Redistributions in binary form must reproduce the above copyright
1331e2cbf0Stshiozak * notice, this list of conditions and the following disclaimer in the
1431e2cbf0Stshiozak * documentation and/or other materials provided with the distribution.
1531e2cbf0Stshiozak *
1631e2cbf0Stshiozak * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1731e2cbf0Stshiozak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1831e2cbf0Stshiozak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1931e2cbf0Stshiozak * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2031e2cbf0Stshiozak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2131e2cbf0Stshiozak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2231e2cbf0Stshiozak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2331e2cbf0Stshiozak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2431e2cbf0Stshiozak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2531e2cbf0Stshiozak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2631e2cbf0Stshiozak * SUCH DAMAGE.
2731e2cbf0Stshiozak */
2831e2cbf0Stshiozak
2931e2cbf0Stshiozak /*-
3031e2cbf0Stshiozak * Copyright (c) 1993
3131e2cbf0Stshiozak * The Regents of the University of California. All rights reserved.
3231e2cbf0Stshiozak *
3331e2cbf0Stshiozak * This code is derived from software contributed to Berkeley by
3431e2cbf0Stshiozak * Paul Borman at Krystal Technologies.
3531e2cbf0Stshiozak *
3631e2cbf0Stshiozak * Redistribution and use in source and binary forms, with or without
3731e2cbf0Stshiozak * modification, are permitted provided that the following conditions
3831e2cbf0Stshiozak * are met:
3931e2cbf0Stshiozak * 1. Redistributions of source code must retain the above copyright
4031e2cbf0Stshiozak * notice, this list of conditions and the following disclaimer.
4131e2cbf0Stshiozak * 2. Redistributions in binary form must reproduce the above copyright
4231e2cbf0Stshiozak * notice, this list of conditions and the following disclaimer in the
4331e2cbf0Stshiozak * documentation and/or other materials provided with the distribution.
44eb7c1594Sagc * 3. Neither the name of the University nor the names of its contributors
4531e2cbf0Stshiozak * may be used to endorse or promote products derived from this software
4631e2cbf0Stshiozak * without specific prior written permission.
4731e2cbf0Stshiozak *
4831e2cbf0Stshiozak * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4931e2cbf0Stshiozak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5031e2cbf0Stshiozak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5131e2cbf0Stshiozak * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5231e2cbf0Stshiozak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5331e2cbf0Stshiozak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5431e2cbf0Stshiozak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5531e2cbf0Stshiozak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5631e2cbf0Stshiozak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5731e2cbf0Stshiozak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5831e2cbf0Stshiozak * SUCH DAMAGE.
5931e2cbf0Stshiozak */
6031e2cbf0Stshiozak
6131e2cbf0Stshiozak #include <sys/cdefs.h>
6231e2cbf0Stshiozak #if defined(LIBC_SCCS) && !defined(lint)
63*2a7af30cStnozaki __RCSID("$NetBSD: _wctrans.c,v 1.17 2010/06/13 04:14:57 tnozaki Exp $");
6431e2cbf0Stshiozak #endif /* LIBC_SCCS and not lint */
6531e2cbf0Stshiozak
6631e2cbf0Stshiozak #include "namespace.h"
6731e2cbf0Stshiozak
689a35d797Stnozaki #include <sys/types.h>
69db741099Stnozaki #include <assert.h>
7031e2cbf0Stshiozak #include <wctype.h>
719a35d797Stnozaki
72*2a7af30cStnozaki #include "runetype_local.h"
7331e2cbf0Stshiozak #include "_wctrans_local.h"
7431e2cbf0Stshiozak
7531e2cbf0Stshiozak /*
7652ed7b03Stnozaki * _towctrans_ext:
7731e2cbf0Stshiozak * translate a character (extended part)
7831e2cbf0Stshiozak */
7931e2cbf0Stshiozak wint_t
_towctrans_ext(wint_t c,struct _WCTransEntry const * te)80bf280d27Stnozaki _towctrans_ext(wint_t c, struct _WCTransEntry const *te)
8131e2cbf0Stshiozak {
827aab8c37Stnozaki __nbrune_t c0;
8331e2cbf0Stshiozak uint32_t x;
847aab8c37Stnozaki _RuneRange *rr;
857aab8c37Stnozaki _RuneEntry *base, *re;
8631e2cbf0Stshiozak
879a35d797Stnozaki _DIAGASSERT(te != NULL);
889a35d797Stnozaki
896ca22529Stshiozak if (c == WEOF)
9031e2cbf0Stshiozak return (c);
9131e2cbf0Stshiozak
927aab8c37Stnozaki c0 = (__nbrune_t)c; /* XXX assumes wchar_t = int */
937aab8c37Stnozaki rr = te->te_extmap;
947aab8c37Stnozaki base = rr->rr_rune_ranges;
957aab8c37Stnozaki for (x = rr->rr_nranges; x != 0; x >>= 1) {
967aab8c37Stnozaki re = base + (x >> 1);
977aab8c37Stnozaki if (re->re_min <= c0 && re->re_max >= c0)
987aab8c37Stnozaki return (re->re_map + c0 - re->re_min);
997aab8c37Stnozaki else if (c0 >= re->re_max) {
1007aab8c37Stnozaki base = re + 1;
1017aab8c37Stnozaki x--;
10231e2cbf0Stshiozak }
1037aab8c37Stnozaki }
1047aab8c37Stnozaki
10531e2cbf0Stshiozak return (c);
10631e2cbf0Stshiozak }
107