collcmp.c revision 19275
117530Sache/*
217530Sache * Copyright (C) 1996 by Andrey A. Chernov, Moscow, Russia.
317530Sache * All rights reserved.
417530Sache *
517530Sache * Redistribution and use in source and binary forms, with or without
617530Sache * modification, are permitted provided that the following conditions
717530Sache * are met:
817530Sache * 1. Redistributions of source code must retain the above copyright
917530Sache *    notice, this list of conditions and the following disclaimer.
1017530Sache * 2. Redistributions in binary form must reproduce the above copyright
1117530Sache *    notice, this list of conditions and the following disclaimer in the
1217530Sache *    documentation and/or other materials provided with the distribution.
1317530Sache *
1417530Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
1517530Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1617530Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1717530Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1817530Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1917530Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2017530Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2117530Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2217530Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2317530Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2417530Sache * SUCH DAMAGE.
2517599Sache *
2619275Sache * $Id: collcmp.c,v 1.7 1996/10/23 15:35:46 ache Exp $
2717530Sache */
2817530Sache
2919129Sache#define ASCII_COMPATIBLE_COLLATE        /* see usr.bin/colldef/data */
3019129Sache
3117530Sache#include <string.h>
3219275Sache#include "collate.h"
3319129Sache#ifndef ASCII_COMPATIBLE_COLLATE
3419129Sache#include <ctype.h>
3519129Sache#endif
3617551Sache
3719275Sache/* Temporary backward compatibility */
3819275Sache
3919275Sacheint collate_range_cmp (c1, c2)
4019275Sache	int c1, c2;
4119275Sache{
4219275Sache	return __collate_range_cmp(c1, c2);
4319275Sache}
4419275Sache
4519275Sache
4618331Sache/*
4718331Sache * Compare two characters converting collate information
4818331Sache * into ASCII-compatible range, it allows to handle
4918331Sache * "[a-z]"-type ranges with national characters.
5018331Sache */
5118331Sache
5219275Sacheint __collate_range_cmp (c1, c2)
5317551Sache	int c1, c2;
5417551Sache{
5517530Sache	static char s1[2], s2[2];
5619129Sache	int ret;
5719129Sache#ifndef ASCII_COMPATIBLE_COLLATE
5819129Sache	int as1, as2, al1, al2;
5919129Sache#endif
6017530Sache
6117551Sache	c1 &= UCHAR_MAX;
6217551Sache	c2 &= UCHAR_MAX;
6317530Sache	if (c1 == c2)
6417530Sache		return (0);
6517576Sache
6619129Sache#ifndef ASCII_COMPATIBLE_COLLATE
6717576Sache	as1 = isascii(c1);
6817576Sache	as2 = isascii(c2);
6917576Sache	al1 = isalpha(c1);
7017576Sache	al2 = isalpha(c2);
7117576Sache
7217576Sache	if (as1 || as2 || al1 || al2) {
7317576Sache		if ((as1 && as2) || (!al1 && !al2))
7417576Sache			return (c1 - c2);
7517576Sache		if (al1 && !al2) {
7617576Sache			if (isupper(c1))
7717576Sache				return ('A' - c2);
7817576Sache			else
7917576Sache				return ('a' - c2);
8017576Sache		} else if (al2 && !al1) {
8117576Sache			if (isupper(c2))
8217576Sache				return (c1 - 'A');
8317576Sache			else
8417576Sache				return (c1 - 'a');
8517576Sache		}
8617530Sache	}
8719129Sache#endif
8817551Sache	s1[0] = c1;
8917551Sache	s2[0] = c2;
9018331Sache	if ((ret = strcoll(s1, s2)) != 0)
9118331Sache		return (ret);
9218331Sache	return (c1 - c2);
9317530Sache}
94