xref: /freebsd/lib/libc/locale/toupper.c (revision 559a218c)
1350a3d3eSAndrey A. Chernov /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
4350a3d3eSAndrey A. Chernov  * Copyright (c) 1993
5350a3d3eSAndrey A. Chernov  *	The Regents of the University of California.  All rights reserved.
6350a3d3eSAndrey A. Chernov  *
7350a3d3eSAndrey A. Chernov  * This code is derived from software contributed to Berkeley by
8350a3d3eSAndrey A. Chernov  * Paul Borman at Krystal Technologies.
9350a3d3eSAndrey A. Chernov  *
103c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
115b5fa75aSEd Maste  *
123c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
133c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
143c87aa1dSDavid Chisnall  *
15350a3d3eSAndrey A. Chernov  * Redistribution and use in source and binary forms, with or without
16350a3d3eSAndrey A. Chernov  * modification, are permitted provided that the following conditions
17350a3d3eSAndrey A. Chernov  * are met:
18350a3d3eSAndrey A. Chernov  * 1. Redistributions of source code must retain the above copyright
19350a3d3eSAndrey A. Chernov  *    notice, this list of conditions and the following disclaimer.
20350a3d3eSAndrey A. Chernov  * 2. Redistributions in binary form must reproduce the above copyright
21350a3d3eSAndrey A. Chernov  *    notice, this list of conditions and the following disclaimer in the
22350a3d3eSAndrey A. Chernov  *    documentation and/or other materials provided with the distribution.
23fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
24350a3d3eSAndrey A. Chernov  *    may be used to endorse or promote products derived from this software
25350a3d3eSAndrey A. Chernov  *    without specific prior written permission.
26350a3d3eSAndrey A. Chernov  *
27350a3d3eSAndrey A. Chernov  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28350a3d3eSAndrey A. Chernov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29350a3d3eSAndrey A. Chernov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30350a3d3eSAndrey A. Chernov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31350a3d3eSAndrey A. Chernov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32350a3d3eSAndrey A. Chernov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33350a3d3eSAndrey A. Chernov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34350a3d3eSAndrey A. Chernov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35350a3d3eSAndrey A. Chernov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36350a3d3eSAndrey A. Chernov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37350a3d3eSAndrey A. Chernov  * SUCH DAMAGE.
38350a3d3eSAndrey A. Chernov  */
39350a3d3eSAndrey A. Chernov 
4061310091SStefan Farfeleder #include <ctype.h>
412f04ec53SAndrey A. Chernov #include <stdio.h>
42a0998ce6STim J. Robbins #include <runetype.h>
433c87aa1dSDavid Chisnall #include <wchar.h>
443c87aa1dSDavid Chisnall #include "mblocal.h"
452f04ec53SAndrey A. Chernov 
46abbd8902SMike Barcroft __ct_rune_t
___toupper_l(__ct_rune_t c,locale_t l)47c83f3fc4SCraig Rodrigues ___toupper_l(__ct_rune_t c, locale_t l)
482f04ec53SAndrey A. Chernov {
4945a11576STim J. Robbins 	size_t lim;
503c87aa1dSDavid Chisnall 	FIX_LOCALE(l);
5183c476cdSDimitry Andric 	_RuneRange *rr = &XLOCALE_CTYPE(l)->runes->__mapupper_ext;
5245a11576STim J. Robbins 	_RuneEntry *base, *re;
532f04ec53SAndrey A. Chernov 
547871e368SAndrey A. Chernov 	if (c < 0 || c == EOF)
55f9cbdf41SAndrey A. Chernov 		return(c);
567871e368SAndrey A. Chernov 
5745a11576STim J. Robbins 	/* Binary search -- see bsearch.c for explanation. */
58ddc1ededSTim J. Robbins 	base = rr->__ranges;
59ddc1ededSTim J. Robbins 	for (lim = rr->__nranges; lim != 0; lim >>= 1) {
6045a11576STim J. Robbins 		re = base + (lim >> 1);
61ddc1ededSTim J. Robbins 		if (re->__min <= c && c <= re->__max)
623c87aa1dSDavid Chisnall 		{
63ddc1ededSTim J. Robbins 			return (re->__map + c - re->__min);
643c87aa1dSDavid Chisnall 		}
65ddc1ededSTim J. Robbins 		else if (c > re->__max) {
6645a11576STim J. Robbins 			base = re + 1;
6745a11576STim J. Robbins 			lim--;
6845a11576STim J. Robbins 		}
692f04ec53SAndrey A. Chernov 	}
708b96e6c9SAndrey A. Chernov 
712f04ec53SAndrey A. Chernov 	return(c);
722f04ec53SAndrey A. Chernov }
733c87aa1dSDavid Chisnall __ct_rune_t
___toupper(__ct_rune_t c)74c83f3fc4SCraig Rodrigues ___toupper(__ct_rune_t c)
753c87aa1dSDavid Chisnall {
763c87aa1dSDavid Chisnall 	return ___toupper_l(c, __get_locale());
773c87aa1dSDavid Chisnall }
78