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