1*ad30f8e7SGabor Kovesdan /* $FreeBSD$ */ 2*ad30f8e7SGabor Kovesdan /* $NetBSD: citrus_bcs.c,v 1.5 2005/05/14 17:55:42 tshiozak Exp $ */ 3*ad30f8e7SGabor Kovesdan 4*ad30f8e7SGabor Kovesdan /*- 5*ad30f8e7SGabor Kovesdan * Copyright (c)2003 Citrus Project, 6*ad30f8e7SGabor Kovesdan * All rights reserved. 7*ad30f8e7SGabor Kovesdan * 8*ad30f8e7SGabor Kovesdan * Redistribution and use in source and binary forms, with or without 9*ad30f8e7SGabor Kovesdan * modification, are permitted provided that the following conditions 10*ad30f8e7SGabor Kovesdan * are met: 11*ad30f8e7SGabor Kovesdan * 1. Redistributions of source code must retain the above copyright 12*ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer. 13*ad30f8e7SGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright 14*ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer in the 15*ad30f8e7SGabor Kovesdan * documentation and/or other materials provided with the distribution. 16*ad30f8e7SGabor Kovesdan * 17*ad30f8e7SGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18*ad30f8e7SGabor Kovesdan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19*ad30f8e7SGabor Kovesdan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20*ad30f8e7SGabor Kovesdan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21*ad30f8e7SGabor Kovesdan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22*ad30f8e7SGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23*ad30f8e7SGabor Kovesdan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24*ad30f8e7SGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25*ad30f8e7SGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26*ad30f8e7SGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27*ad30f8e7SGabor Kovesdan * SUCH DAMAGE. 28*ad30f8e7SGabor Kovesdan */ 29*ad30f8e7SGabor Kovesdan 30*ad30f8e7SGabor Kovesdan #include <sys/cdefs.h> 31*ad30f8e7SGabor Kovesdan 32*ad30f8e7SGabor Kovesdan #include <assert.h> 33*ad30f8e7SGabor Kovesdan #include <stdlib.h> 34*ad30f8e7SGabor Kovesdan 35*ad30f8e7SGabor Kovesdan #include "citrus_namespace.h" 36*ad30f8e7SGabor Kovesdan #include "citrus_bcs.h" 37*ad30f8e7SGabor Kovesdan 38*ad30f8e7SGabor Kovesdan /* 39*ad30f8e7SGabor Kovesdan * case insensitive comparison between two C strings. 40*ad30f8e7SGabor Kovesdan */ 41*ad30f8e7SGabor Kovesdan int 42*ad30f8e7SGabor Kovesdan _citrus_bcs_strcasecmp(const char * __restrict str1, 43*ad30f8e7SGabor Kovesdan const char * __restrict str2) 44*ad30f8e7SGabor Kovesdan { 45*ad30f8e7SGabor Kovesdan int c1, c2; 46*ad30f8e7SGabor Kovesdan 47*ad30f8e7SGabor Kovesdan c1 = c2 = 1; 48*ad30f8e7SGabor Kovesdan 49*ad30f8e7SGabor Kovesdan while (c1 && c2 && c1 == c2) { 50*ad30f8e7SGabor Kovesdan c1 = _bcs_toupper(*str1++); 51*ad30f8e7SGabor Kovesdan c2 = _bcs_toupper(*str2++); 52*ad30f8e7SGabor Kovesdan } 53*ad30f8e7SGabor Kovesdan 54*ad30f8e7SGabor Kovesdan return ((c1 == c2) ? 0 : ((c1 > c2) ? 1 : -1)); 55*ad30f8e7SGabor Kovesdan } 56*ad30f8e7SGabor Kovesdan 57*ad30f8e7SGabor Kovesdan /* 58*ad30f8e7SGabor Kovesdan * case insensitive comparison between two C strings with limitation of length. 59*ad30f8e7SGabor Kovesdan */ 60*ad30f8e7SGabor Kovesdan int 61*ad30f8e7SGabor Kovesdan _citrus_bcs_strncasecmp(const char * __restrict str1, 62*ad30f8e7SGabor Kovesdan const char * __restrict str2, size_t sz) 63*ad30f8e7SGabor Kovesdan { 64*ad30f8e7SGabor Kovesdan int c1, c2; 65*ad30f8e7SGabor Kovesdan 66*ad30f8e7SGabor Kovesdan c1 = c2 = 1; 67*ad30f8e7SGabor Kovesdan 68*ad30f8e7SGabor Kovesdan while (c1 && c2 && c1 == c2 && sz != 0) { 69*ad30f8e7SGabor Kovesdan c1 = _bcs_toupper(*str1++); 70*ad30f8e7SGabor Kovesdan c2 = _bcs_toupper(*str2++); 71*ad30f8e7SGabor Kovesdan sz--; 72*ad30f8e7SGabor Kovesdan } 73*ad30f8e7SGabor Kovesdan 74*ad30f8e7SGabor Kovesdan return ((c1 == c2) ? 0 : ((c1 > c2) ? 1 : -1)); 75*ad30f8e7SGabor Kovesdan } 76*ad30f8e7SGabor Kovesdan 77*ad30f8e7SGabor Kovesdan /* 78*ad30f8e7SGabor Kovesdan * skip white space characters. 79*ad30f8e7SGabor Kovesdan */ 80*ad30f8e7SGabor Kovesdan const char * 81*ad30f8e7SGabor Kovesdan _citrus_bcs_skip_ws(const char *p) 82*ad30f8e7SGabor Kovesdan { 83*ad30f8e7SGabor Kovesdan 84*ad30f8e7SGabor Kovesdan while (*p && _bcs_isspace(*p)) 85*ad30f8e7SGabor Kovesdan p++; 86*ad30f8e7SGabor Kovesdan 87*ad30f8e7SGabor Kovesdan return (p); 88*ad30f8e7SGabor Kovesdan } 89*ad30f8e7SGabor Kovesdan 90*ad30f8e7SGabor Kovesdan /* 91*ad30f8e7SGabor Kovesdan * skip non white space characters. 92*ad30f8e7SGabor Kovesdan */ 93*ad30f8e7SGabor Kovesdan const char * 94*ad30f8e7SGabor Kovesdan _citrus_bcs_skip_nonws(const char *p) 95*ad30f8e7SGabor Kovesdan { 96*ad30f8e7SGabor Kovesdan 97*ad30f8e7SGabor Kovesdan while (*p && !_bcs_isspace(*p)) 98*ad30f8e7SGabor Kovesdan p++; 99*ad30f8e7SGabor Kovesdan 100*ad30f8e7SGabor Kovesdan return (p); 101*ad30f8e7SGabor Kovesdan } 102*ad30f8e7SGabor Kovesdan 103*ad30f8e7SGabor Kovesdan /* 104*ad30f8e7SGabor Kovesdan * skip white space characters with limitation of length. 105*ad30f8e7SGabor Kovesdan */ 106*ad30f8e7SGabor Kovesdan const char * 107*ad30f8e7SGabor Kovesdan _citrus_bcs_skip_ws_len(const char * __restrict p, size_t * __restrict len) 108*ad30f8e7SGabor Kovesdan { 109*ad30f8e7SGabor Kovesdan 110*ad30f8e7SGabor Kovesdan while (*p && *len > 0 && _bcs_isspace(*p)) { 111*ad30f8e7SGabor Kovesdan p++; 112*ad30f8e7SGabor Kovesdan (*len)--; 113*ad30f8e7SGabor Kovesdan } 114*ad30f8e7SGabor Kovesdan 115*ad30f8e7SGabor Kovesdan return (p); 116*ad30f8e7SGabor Kovesdan } 117*ad30f8e7SGabor Kovesdan 118*ad30f8e7SGabor Kovesdan /* 119*ad30f8e7SGabor Kovesdan * skip non white space characters with limitation of length. 120*ad30f8e7SGabor Kovesdan */ 121*ad30f8e7SGabor Kovesdan const char * 122*ad30f8e7SGabor Kovesdan _citrus_bcs_skip_nonws_len(const char * __restrict p, size_t * __restrict len) 123*ad30f8e7SGabor Kovesdan { 124*ad30f8e7SGabor Kovesdan 125*ad30f8e7SGabor Kovesdan while (*p && *len > 0 && !_bcs_isspace(*p)) { 126*ad30f8e7SGabor Kovesdan p++; 127*ad30f8e7SGabor Kovesdan (*len)--; 128*ad30f8e7SGabor Kovesdan } 129*ad30f8e7SGabor Kovesdan 130*ad30f8e7SGabor Kovesdan return (p); 131*ad30f8e7SGabor Kovesdan } 132*ad30f8e7SGabor Kovesdan 133*ad30f8e7SGabor Kovesdan /* 134*ad30f8e7SGabor Kovesdan * truncate trailing white space characters. 135*ad30f8e7SGabor Kovesdan */ 136*ad30f8e7SGabor Kovesdan void 137*ad30f8e7SGabor Kovesdan _citrus_bcs_trunc_rws_len(const char * __restrict p, size_t * __restrict len) 138*ad30f8e7SGabor Kovesdan { 139*ad30f8e7SGabor Kovesdan 140*ad30f8e7SGabor Kovesdan while (*len > 0 && _bcs_isspace(p[*len - 1])) 141*ad30f8e7SGabor Kovesdan (*len)--; 142*ad30f8e7SGabor Kovesdan } 143*ad30f8e7SGabor Kovesdan 144*ad30f8e7SGabor Kovesdan /* 145*ad30f8e7SGabor Kovesdan * destructive transliterate to lowercase. 146*ad30f8e7SGabor Kovesdan */ 147*ad30f8e7SGabor Kovesdan void 148*ad30f8e7SGabor Kovesdan _citrus_bcs_convert_to_lower(char *s) 149*ad30f8e7SGabor Kovesdan { 150*ad30f8e7SGabor Kovesdan 151*ad30f8e7SGabor Kovesdan while (*s) { 152*ad30f8e7SGabor Kovesdan *s = _bcs_tolower(*s); 153*ad30f8e7SGabor Kovesdan s++; 154*ad30f8e7SGabor Kovesdan } 155*ad30f8e7SGabor Kovesdan } 156*ad30f8e7SGabor Kovesdan 157*ad30f8e7SGabor Kovesdan /* 158*ad30f8e7SGabor Kovesdan * destructive transliterate to uppercase. 159*ad30f8e7SGabor Kovesdan */ 160*ad30f8e7SGabor Kovesdan void 161*ad30f8e7SGabor Kovesdan _citrus_bcs_convert_to_upper(char *s) 162*ad30f8e7SGabor Kovesdan { 163*ad30f8e7SGabor Kovesdan 164*ad30f8e7SGabor Kovesdan while (*s) { 165*ad30f8e7SGabor Kovesdan *s = _bcs_toupper(*s); 166*ad30f8e7SGabor Kovesdan s++; 167*ad30f8e7SGabor Kovesdan } 168*ad30f8e7SGabor Kovesdan } 169