summaryrefslogtreecommitdiffstats
path: root/private/crt32/mbstring/mbsnbicm.c
blob: 3ee1d9ba99eb1974e8c74f6ac3ce1b331ae34b9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*** 
*mbsnbicmp.c - Compare n bytes of strings, ignoring case (MBCS)
*
*	Copyright (c) 1985-1993, Microsoft Corporation.  All rights reserved.
*
*Purpose:
*	Compare n bytes of strings, ignoring case (MBCS)
*
*Revision History:
*	08-03-93  KRS	Ported from 16-bit sources.
*	10-12-93  CFW	Compare lower case, not upper.
*
*******************************************************************************/

#ifdef _MBCS
#include <cruntime.h>
#include <mbdata.h>
#include <mbctype.h>
#include <mbstring.h>


/***
* _mbsnbicmp - Compare n bytes of strings, ignoring case (MBCS)
*
*Purpose:
*	Compares up to n bytes of two strings for lexical order.
*	Strings are compared on a character basis, not a byte basis.
*	Case of characters is not considered.
*
*Entry:
*	unsigned char *s1, *s2 = strings to compare
*	size_t n = maximum number of bytes to compare
*
*Exit:
*       returns <0 if s1 < s2
*	returns  0 if s1 == s2
*       returns >0 if s1 > s2
*
*Exceptions:
*
*******************************************************************************/

int _CRTAPI1 _mbsnbicmp(s1, s2, n)
const unsigned char *s1, *s2;
size_t n;
{
	unsigned short c1, c2;

	if (n==0)
		return(0);

	while (n--) {

		c1 = *s1++;
		if (_ISLEADBYTE(c1)) {
			if (n==0)
				break;
			if (*s1 == '\0')
				c1 = 0;
			else {
				c1 = ((c1<<8) | *s1++);
#ifndef _MBCS_OS
				if ( (_mbascii) &&
				     ((c1 >= _MBUPPERLOW) && (c1 <= _MBUPPERHIGH))
				   )
					c1 += _MBCASEDIFF;
#endif
			}
		}
		else
			c1 = tolower(c1);

		c2 = *s2++;
		if (_ISLEADBYTE(c2)) {
			if (n--==0)
				break;
			if (*s2 == '\0')
				c2 = 0;
			else {
				c2 = ((c2<<8) | *s2++);
#ifndef _MBCS_OS
				if ( (_mbascii) &&
				     ((c2 >= _MBUPPERLOW) && (c2 <= _MBUPPERHIGH))
				   )
					c2 += _MBCASEDIFF;
#endif
			}
		}
		else
			c2 = tolower(c2);

		if (c1 != c2)
			return( (c1 > c2) ? 1 : -1);

		if (c1 == 0)
			return(0);
	}

	return(0);
}
#endif	/* _MBCS */