1219019Sgabor/* $FreeBSD$ */
2219019Sgabor/* $NetBSD: citrus_bcs.c,v 1.5 2005/05/14 17:55:42 tshiozak Exp $ */
3219019Sgabor
4219019Sgabor/*-
5219019Sgabor * Copyright (c)2003 Citrus Project,
6219019Sgabor * All rights reserved.
7219019Sgabor *
8219019Sgabor * Redistribution and use in source and binary forms, with or without
9219019Sgabor * modification, are permitted provided that the following conditions
10219019Sgabor * are met:
11219019Sgabor * 1. Redistributions of source code must retain the above copyright
12219019Sgabor *    notice, this list of conditions and the following disclaimer.
13219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright
14219019Sgabor *    notice, this list of conditions and the following disclaimer in the
15219019Sgabor *    documentation and/or other materials provided with the distribution.
16219019Sgabor *
17219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20219019Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27219019Sgabor * SUCH DAMAGE.
28219019Sgabor */
29219019Sgabor
30219019Sgabor#include <sys/cdefs.h>
31219019Sgabor
32219019Sgabor#include <assert.h>
33219019Sgabor#include <stdlib.h>
34219019Sgabor
35219019Sgabor#include "citrus_namespace.h"
36219019Sgabor#include "citrus_bcs.h"
37219019Sgabor
38219019Sgabor/*
39219019Sgabor * case insensitive comparison between two C strings.
40219019Sgabor */
41219019Sgaborint
42219019Sgabor_citrus_bcs_strcasecmp(const char * __restrict str1,
43219019Sgabor    const char * __restrict str2)
44219019Sgabor{
45219019Sgabor	int c1, c2;
46219019Sgabor
47219019Sgabor	c1 = c2 = 1;
48219019Sgabor
49219019Sgabor	while (c1 && c2 && c1 == c2) {
50219019Sgabor		c1 = _bcs_toupper(*str1++);
51219019Sgabor		c2 = _bcs_toupper(*str2++);
52219019Sgabor	}
53219019Sgabor
54219019Sgabor	return ((c1 == c2) ? 0 : ((c1 > c2) ? 1 : -1));
55219019Sgabor}
56219019Sgabor
57219019Sgabor/*
58219019Sgabor * case insensitive comparison between two C strings with limitation of length.
59219019Sgabor */
60219019Sgaborint
61219019Sgabor_citrus_bcs_strncasecmp(const char * __restrict str1,
62219019Sgabor    const char * __restrict str2, size_t sz)
63219019Sgabor{
64219019Sgabor	int c1, c2;
65219019Sgabor
66219019Sgabor	c1 = c2 = 1;
67219019Sgabor
68219019Sgabor	while (c1 && c2 && c1 == c2 && sz != 0) {
69219019Sgabor		c1 = _bcs_toupper(*str1++);
70219019Sgabor		c2 = _bcs_toupper(*str2++);
71219019Sgabor		sz--;
72219019Sgabor	}
73219019Sgabor
74219019Sgabor	return ((c1 == c2) ? 0 : ((c1 > c2) ? 1 : -1));
75219019Sgabor}
76219019Sgabor
77219019Sgabor/*
78219019Sgabor * skip white space characters.
79219019Sgabor */
80219019Sgaborconst char *
81219019Sgabor_citrus_bcs_skip_ws(const char *p)
82219019Sgabor{
83219019Sgabor
84219019Sgabor	while (*p && _bcs_isspace(*p))
85219019Sgabor		p++;
86219019Sgabor
87219019Sgabor	return (p);
88219019Sgabor}
89219019Sgabor
90219019Sgabor/*
91219019Sgabor * skip non white space characters.
92219019Sgabor */
93219019Sgaborconst char *
94219019Sgabor_citrus_bcs_skip_nonws(const char *p)
95219019Sgabor{
96219019Sgabor
97219019Sgabor	while (*p && !_bcs_isspace(*p))
98219019Sgabor		p++;
99219019Sgabor
100219019Sgabor	return (p);
101219019Sgabor}
102219019Sgabor
103219019Sgabor/*
104219019Sgabor * skip white space characters with limitation of length.
105219019Sgabor */
106219019Sgaborconst char *
107219019Sgabor_citrus_bcs_skip_ws_len(const char * __restrict p, size_t * __restrict len)
108219019Sgabor{
109219019Sgabor
110219019Sgabor	while (*p && *len > 0 && _bcs_isspace(*p)) {
111219019Sgabor		p++;
112219019Sgabor		(*len)--;
113219019Sgabor	}
114219019Sgabor
115219019Sgabor	return (p);
116219019Sgabor}
117219019Sgabor
118219019Sgabor/*
119219019Sgabor * skip non white space characters with limitation of length.
120219019Sgabor */
121219019Sgaborconst char *
122219019Sgabor_citrus_bcs_skip_nonws_len(const char * __restrict p, size_t * __restrict len)
123219019Sgabor{
124219019Sgabor
125219019Sgabor	while (*p && *len > 0 && !_bcs_isspace(*p)) {
126219019Sgabor		p++;
127219019Sgabor		(*len)--;
128219019Sgabor	}
129219019Sgabor
130219019Sgabor	return (p);
131219019Sgabor}
132219019Sgabor
133219019Sgabor/*
134219019Sgabor * truncate trailing white space characters.
135219019Sgabor */
136219019Sgaborvoid
137219019Sgabor_citrus_bcs_trunc_rws_len(const char * __restrict p, size_t * __restrict len)
138219019Sgabor{
139219019Sgabor
140219019Sgabor	while (*len > 0 && _bcs_isspace(p[*len - 1]))
141219019Sgabor		(*len)--;
142219019Sgabor}
143219019Sgabor
144219019Sgabor/*
145219019Sgabor * destructive transliterate to lowercase.
146219019Sgabor */
147219019Sgaborvoid
148219019Sgabor_citrus_bcs_convert_to_lower(char *s)
149219019Sgabor{
150219019Sgabor
151219019Sgabor	while (*s) {
152219019Sgabor		*s = _bcs_tolower(*s);
153219019Sgabor		s++;
154219019Sgabor	}
155219019Sgabor}
156219019Sgabor
157219019Sgabor/*
158219019Sgabor * destructive transliterate to uppercase.
159219019Sgabor */
160219019Sgaborvoid
161219019Sgabor_citrus_bcs_convert_to_upper(char *s)
162219019Sgabor{
163219019Sgabor
164219019Sgabor	while (*s) {
165219019Sgabor		*s = _bcs_toupper(*s);
166219019Sgabor		s++;
167219019Sgabor	}
168219019Sgabor}
169