14297a3b0SGarrett D'Amore /* 2*2d08521bSGarrett D'Amore * Copyright 2013 Garrett D'Amore <garrett@damore.org> 36b5e5868SGarrett D'Amore * Copyright 2010 Nexenta Systems, Inc. All rights reserved. 44297a3b0SGarrett D'Amore * Copyright (c) 1989, 1993 54297a3b0SGarrett D'Amore * The Regents of the University of California. All rights reserved. 64297a3b0SGarrett D'Amore * (c) UNIX System Laboratories, Inc. 74297a3b0SGarrett D'Amore * All or some portions of this file are derived from material licensed 84297a3b0SGarrett D'Amore * to the University of California by American Telephone and Telegraph 94297a3b0SGarrett D'Amore * Co. or Unix System Laboratories, Inc. and are reproduced herein with 104297a3b0SGarrett D'Amore * the permission of UNIX System Laboratories, Inc. 114297a3b0SGarrett D'Amore * 124297a3b0SGarrett D'Amore * This code is derived from software contributed to Berkeley by 134297a3b0SGarrett D'Amore * Paul Borman at Krystal Technologies. 144297a3b0SGarrett D'Amore * 154297a3b0SGarrett D'Amore * Redistribution and use in source and binary forms, with or without 164297a3b0SGarrett D'Amore * modification, are permitted provided that the following conditions 174297a3b0SGarrett D'Amore * are met: 184297a3b0SGarrett D'Amore * 1. Redistributions of source code must retain the above copyright 194297a3b0SGarrett D'Amore * notice, this list of conditions and the following disclaimer. 204297a3b0SGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright 214297a3b0SGarrett D'Amore * notice, this list of conditions and the following disclaimer in the 224297a3b0SGarrett D'Amore * documentation and/or other materials provided with the distribution. 234297a3b0SGarrett D'Amore * 4. Neither the name of the University nor the names of its contributors 244297a3b0SGarrett D'Amore * may be used to endorse or promote products derived from this software 254297a3b0SGarrett D'Amore * without specific prior written permission. 264297a3b0SGarrett D'Amore * 274297a3b0SGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 284297a3b0SGarrett D'Amore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 294297a3b0SGarrett D'Amore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 304297a3b0SGarrett D'Amore * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 314297a3b0SGarrett D'Amore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 324297a3b0SGarrett D'Amore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 334297a3b0SGarrett D'Amore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 344297a3b0SGarrett D'Amore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 354297a3b0SGarrett D'Amore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 364297a3b0SGarrett D'Amore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 374297a3b0SGarrett D'Amore * SUCH DAMAGE. 384297a3b0SGarrett D'Amore */ 394297a3b0SGarrett D'Amore 404297a3b0SGarrett D'Amore #include "lint.h" 414297a3b0SGarrett D'Amore #include <wctype.h> 424297a3b0SGarrett D'Amore #include <stdio.h> 43*2d08521bSGarrett D'Amore #include "localeimpl.h" 44*2d08521bSGarrett D'Amore #include "lctype.h" 454297a3b0SGarrett D'Amore #include "runetype.h" 464297a3b0SGarrett D'Amore 474297a3b0SGarrett D'Amore static wint_t 48*2d08521bSGarrett D'Amore change_case_ext(locale_t loc, wint_t c, int lower) 494297a3b0SGarrett D'Amore { 50*2d08521bSGarrett D'Amore const _RuneLocale *rl; 51*2d08521bSGarrett D'Amore const _RuneRange *rr; 52*2d08521bSGarrett D'Amore const _RuneEntry *base, *re; 534297a3b0SGarrett D'Amore size_t lim; 544297a3b0SGarrett D'Amore 554297a3b0SGarrett D'Amore if (c < 0 || c == EOF) 564297a3b0SGarrett D'Amore return (c); 574297a3b0SGarrett D'Amore 58*2d08521bSGarrett D'Amore rl = loc->runelocale; 59*2d08521bSGarrett D'Amore rr = lower ? &rl->__maplower_ext : &rl->__mapupper_ext; 604297a3b0SGarrett D'Amore /* Binary search -- see bsearch.c for explanation. */ 614297a3b0SGarrett D'Amore base = rr->__ranges; 624297a3b0SGarrett D'Amore for (lim = rr->__nranges; lim != 0; lim >>= 1) { 634297a3b0SGarrett D'Amore re = base + (lim >> 1); 644297a3b0SGarrett D'Amore if (re->__min <= c && c <= re->__max) { 654297a3b0SGarrett D'Amore return (re->__map + c - re->__min); 664297a3b0SGarrett D'Amore } else if (c > re->__max) { 674297a3b0SGarrett D'Amore base = re + 1; 684297a3b0SGarrett D'Amore lim--; 694297a3b0SGarrett D'Amore } 704297a3b0SGarrett D'Amore } 714297a3b0SGarrett D'Amore 724297a3b0SGarrett D'Amore return (c); 734297a3b0SGarrett D'Amore } 744297a3b0SGarrett D'Amore 75*2d08521bSGarrett D'Amore wint_t 76*2d08521bSGarrett D'Amore towlower_l(wint_t wc, locale_t loc) 77*2d08521bSGarrett D'Amore { 78*2d08521bSGarrett D'Amore return (iswascii(wc) ? __trans_lower[wc] : 79*2d08521bSGarrett D'Amore (wc < 0 || wc >= _CACHED_RUNES) ? 80*2d08521bSGarrett D'Amore change_case_ext(loc, wc, 1) : 81*2d08521bSGarrett D'Amore loc->runelocale->__maplower[wc]); 82*2d08521bSGarrett D'Amore } 83*2d08521bSGarrett D'Amore 844297a3b0SGarrett D'Amore #undef towlower 854297a3b0SGarrett D'Amore wint_t 864297a3b0SGarrett D'Amore towlower(wint_t wc) 874297a3b0SGarrett D'Amore { 88*2d08521bSGarrett D'Amore return (iswascii(wc) ? __trans_lower[wc] : 89*2d08521bSGarrett D'Amore (wc < 0 || wc >= _CACHED_RUNES) ? 90*2d08521bSGarrett D'Amore change_case_ext(uselocale(NULL), wc, 1) : 91*2d08521bSGarrett D'Amore uselocale(NULL)->runelocale->__maplower[wc]); 92*2d08521bSGarrett D'Amore } 93*2d08521bSGarrett D'Amore 94*2d08521bSGarrett D'Amore wint_t 95*2d08521bSGarrett D'Amore towupper_l(wint_t wc, locale_t loc) 96*2d08521bSGarrett D'Amore { 97*2d08521bSGarrett D'Amore return (iswascii(wc) ? __trans_upper[wc] : 98*2d08521bSGarrett D'Amore (wc < 0 || wc >= _CACHED_RUNES) ? 99*2d08521bSGarrett D'Amore change_case_ext(loc, wc, 0) : 100*2d08521bSGarrett D'Amore loc->runelocale->__mapupper[wc]); 1014297a3b0SGarrett D'Amore } 1024297a3b0SGarrett D'Amore 1034297a3b0SGarrett D'Amore #undef towupper 1044297a3b0SGarrett D'Amore wint_t 1054297a3b0SGarrett D'Amore towupper(wint_t wc) 1064297a3b0SGarrett D'Amore { 107*2d08521bSGarrett D'Amore return (iswascii(wc) ? __trans_upper[wc] : 108*2d08521bSGarrett D'Amore (wc < 0 || wc >= _CACHED_RUNES) ? 109*2d08521bSGarrett D'Amore change_case_ext(uselocale(NULL), wc, 0) : 110*2d08521bSGarrett D'Amore uselocale(NULL)->runelocale->__mapupper[wc]); 1114297a3b0SGarrett D'Amore } 112