xref: /freebsd/lib/libc/locale/toupper.c (revision 3c87aa1d)
1350a3d3eSAndrey A. Chernov /*-
2350a3d3eSAndrey A. Chernov  * Copyright (c) 1993
3350a3d3eSAndrey A. Chernov  *	The Regents of the University of California.  All rights reserved.
4350a3d3eSAndrey A. Chernov  *
5350a3d3eSAndrey A. Chernov  * This code is derived from software contributed to Berkeley by
6350a3d3eSAndrey A. Chernov  * Paul Borman at Krystal Technologies.
7350a3d3eSAndrey A. Chernov  *
83c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
93c87aa1dSDavid Chisnall  * All rights reserved.
103c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
113c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
123c87aa1dSDavid Chisnall  *
13350a3d3eSAndrey A. Chernov  * Redistribution and use in source and binary forms, with or without
14350a3d3eSAndrey A. Chernov  * modification, are permitted provided that the following conditions
15350a3d3eSAndrey A. Chernov  * are met:
16350a3d3eSAndrey A. Chernov  * 1. Redistributions of source code must retain the above copyright
17350a3d3eSAndrey A. Chernov  *    notice, this list of conditions and the following disclaimer.
18350a3d3eSAndrey A. Chernov  * 2. Redistributions in binary form must reproduce the above copyright
19350a3d3eSAndrey A. Chernov  *    notice, this list of conditions and the following disclaimer in the
20350a3d3eSAndrey A. Chernov  *    documentation and/or other materials provided with the distribution.
21350a3d3eSAndrey A. Chernov  * 4. Neither the name of the University nor the names of its contributors
22350a3d3eSAndrey A. Chernov  *    may be used to endorse or promote products derived from this software
23350a3d3eSAndrey A. Chernov  *    without specific prior written permission.
24350a3d3eSAndrey A. Chernov  *
25350a3d3eSAndrey A. Chernov  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26350a3d3eSAndrey A. Chernov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27350a3d3eSAndrey A. Chernov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28350a3d3eSAndrey A. Chernov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29350a3d3eSAndrey A. Chernov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30350a3d3eSAndrey A. Chernov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31350a3d3eSAndrey A. Chernov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32350a3d3eSAndrey A. Chernov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33350a3d3eSAndrey A. Chernov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34350a3d3eSAndrey A. Chernov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35350a3d3eSAndrey A. Chernov  * SUCH DAMAGE.
36350a3d3eSAndrey A. Chernov  */
37350a3d3eSAndrey A. Chernov 
38333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
39333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
40333fc21eSDavid E. O'Brien 
4161310091SStefan Farfeleder #include <ctype.h>
422f04ec53SAndrey A. Chernov #include <stdio.h>
43a0998ce6STim J. Robbins #include <runetype.h>
443c87aa1dSDavid Chisnall #include <wchar.h>
453c87aa1dSDavid Chisnall #include "mblocal.h"
462f04ec53SAndrey A. Chernov 
47abbd8902SMike Barcroft __ct_rune_t
483c87aa1dSDavid Chisnall ___toupper_l(c, l)
49abbd8902SMike Barcroft 	__ct_rune_t c;
503c87aa1dSDavid Chisnall 	locale_t l;
512f04ec53SAndrey A. Chernov {
5245a11576STim J. Robbins 	size_t lim;
533c87aa1dSDavid Chisnall 	FIX_LOCALE(l);
543c87aa1dSDavid Chisnall 	_RuneRange *rr = &XLOCALE_CTYPE(l)->runes->__maplower_ext;
5545a11576STim J. Robbins 	_RuneEntry *base, *re;
562f04ec53SAndrey A. Chernov 
577871e368SAndrey A. Chernov 	if (c < 0 || c == EOF)
58f9cbdf41SAndrey A. Chernov 		return(c);
597871e368SAndrey A. Chernov 
6045a11576STim J. Robbins 	/* Binary search -- see bsearch.c for explanation. */
61ddc1ededSTim J. Robbins 	base = rr->__ranges;
62ddc1ededSTim J. Robbins 	for (lim = rr->__nranges; lim != 0; lim >>= 1) {
6345a11576STim J. Robbins 		re = base + (lim >> 1);
64ddc1ededSTim J. Robbins 		if (re->__min <= c && c <= re->__max)
653c87aa1dSDavid Chisnall 		{
66ddc1ededSTim J. Robbins 			return (re->__map + c - re->__min);
673c87aa1dSDavid Chisnall 		}
68ddc1ededSTim J. Robbins 		else if (c > re->__max) {
6945a11576STim J. Robbins 			base = re + 1;
7045a11576STim J. Robbins 			lim--;
7145a11576STim J. Robbins 		}
722f04ec53SAndrey A. Chernov 	}
738b96e6c9SAndrey A. Chernov 
742f04ec53SAndrey A. Chernov 	return(c);
752f04ec53SAndrey A. Chernov }
763c87aa1dSDavid Chisnall __ct_rune_t
773c87aa1dSDavid Chisnall ___toupper(c)
783c87aa1dSDavid Chisnall 	__ct_rune_t c;
793c87aa1dSDavid Chisnall {
803c87aa1dSDavid Chisnall 	return ___toupper_l(c, __get_locale());
813c87aa1dSDavid Chisnall }
82