1131787Stjr/*-
2131787Stjr * Copyright (c) 2004 Tim J. Robbins.
3131787Stjr * All rights reserved.
4131787Stjr *
5227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
6227753Stheraven * All rights reserved.
7227753Stheraven * Portions of this software were developed by David Chisnall
8227753Stheraven * under sponsorship from the FreeBSD Foundation.
9227753Stheraven *
10131787Stjr * Redistribution and use in source and binary forms, with or without
11131787Stjr * modification, are permitted provided that the following conditions
12131787Stjr * are met:
13131787Stjr * 1. Redistributions of source code must retain the above copyright
14131787Stjr *    notice, this list of conditions and the following disclaimer.
15131787Stjr * 2. Redistributions in binary form must reproduce the above copyright
16131787Stjr *    notice, this list of conditions and the following disclaimer in the
17131787Stjr *    documentation and/or other materials provided with the distribution.
18131787Stjr *
19131787Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20131787Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21131787Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22131787Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23131787Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24131787Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25131787Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26131787Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27131787Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28131787Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29131787Stjr * SUCH DAMAGE.
30131787Stjr */
31131787Stjr
32131787Stjr#include <sys/cdefs.h>
33131787Stjr__FBSDID("$FreeBSD$");
34131787Stjr
35131787Stjr#include <runetype.h>
36131787Stjr#include <wchar.h>
37131787Stjr#include <wctype.h>
38227753Stheraven#include "mblocal.h"
39131787Stjr
40131787Stjrwint_t
41227753Stheravennextwctype_l(wint_t wc, wctype_t wct, locale_t locale)
42131787Stjr{
43131787Stjr	size_t lim;
44227753Stheraven	FIX_LOCALE(locale);
45227753Stheraven	_RuneLocale *runes = XLOCALE_CTYPE(locale)->runes;
46227753Stheraven	_RuneRange *rr = &runes->__runetype_ext;
47131787Stjr	_RuneEntry *base, *re;
48131787Stjr	int noinc;
49131787Stjr
50131787Stjr	noinc = 0;
51131787Stjr	if (wc < _CACHED_RUNES) {
52131787Stjr		wc++;
53131787Stjr		while (wc < _CACHED_RUNES) {
54227753Stheraven			if (runes->__runetype[wc] & wct)
55131787Stjr				return (wc);
56131787Stjr			wc++;
57131787Stjr		}
58131787Stjr		wc--;
59131787Stjr	}
60131787Stjr	if (rr->__ranges != NULL && wc < rr->__ranges[0].__min) {
61131787Stjr		wc = rr->__ranges[0].__min;
62131787Stjr		noinc = 1;
63131787Stjr	}
64131787Stjr
65131787Stjr	/* Binary search -- see bsearch.c for explanation. */
66131787Stjr	base = rr->__ranges;
67131787Stjr	for (lim = rr->__nranges; lim != 0; lim >>= 1) {
68131787Stjr		re = base + (lim >> 1);
69131787Stjr		if (re->__min <= wc && wc <= re->__max)
70131787Stjr			goto found;
71131787Stjr		else if (wc > re->__max) {
72131787Stjr			base = re + 1;
73131787Stjr			lim--;
74131787Stjr		}
75131787Stjr	}
76131787Stjr	return (-1);
77131787Stjrfound:
78131787Stjr	if (!noinc)
79131787Stjr		wc++;
80131787Stjr	if (re->__min <= wc && wc <= re->__max) {
81131787Stjr		if (re->__types != NULL) {
82131787Stjr			for (; wc <= re->__max; wc++)
83131787Stjr				if (re->__types[wc - re->__min] & wct)
84131787Stjr					return (wc);
85131787Stjr		} else if (re->__map & wct)
86131787Stjr			return (wc);
87131787Stjr	}
88131787Stjr	while (++re < rr->__ranges + rr->__nranges) {
89131787Stjr		wc = re->__min;
90131787Stjr		if (re->__types != NULL) {
91131787Stjr			for (; wc <= re->__max; wc++)
92131787Stjr				if (re->__types[wc - re->__min] & wct)
93131787Stjr					return (wc);
94131787Stjr		} else if (re->__map & wct)
95131787Stjr			return (wc);
96131787Stjr	}
97131787Stjr	return (-1);
98131787Stjr}
99227753Stheravenwint_t
100227753Stheravennextwctype(wint_t wc, wctype_t wct)
101227753Stheraven{
102227753Stheraven	return nextwctype_l(wc, wct, __get_locale());
103227753Stheraven}
104