~vexo/mirage/mirage

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <Python.h>
#include <ctype.h>
/* $Id: numacomp.c,v 1.2 2002/02/22 01:45:37 gregs Exp $
 * numacomp.c -
 * python extension module 'numacomp' for
 * numerically aware string compares
 * Greg Smith Toronto Feb 2002
 * This code is released under the GNU General Public;
 * See the file COPYING.
 */
/* 2010/06/24 Fredric Johansson
 * Introduced a case insensitive version of the compare function
*/
/*-------------------------------------- */
static char numacomp_docs[] = 
"numacomp(s1,s2): compare two strings with case sensitive numerically aware comparison\n"
"    e.g. \"A1\" < \"A3\" < \"A3x\" < \"A10\" < \"A30x\" < \"B1\"\n"
;
static char numacompi_docs[] = 
"numacompi(s1,s2): compare two strings with case insensitive numerically aware comparison\n"
"    e.g. \"A1\" < \"A3\" < \"a3x\" < \"A10\" < \"a30x\" < \"B1\"\n"
;
/*
 * things to do:
 *    - support unicode
 */
#define ISDIGIT(x)  ((unsigned)((x)-'0')<(unsigned)10)

/*#define CASE(x) printf("--case{%c}--\n", x ) */
#define CASE(x)
/*
 * this is the 'core' compare routine
 * it returns <0, 0, >0
 */
static int
numacomp( unsigned char const *sa,		/* string a */
		  int lena,			/* len of string a */
		  unsigned char const *sb,	/* string b */
		  int lenb,			/* len of string b */
		  int case_sensitive)					
{
	int lenmin,i;
	int za,zb,na,nb;

	lenmin = (lena<lenb)? lena: lenb;
	i = 0;
	/*
	 * skip any common prefix
	 */
	if (case_sensitive){
		while( i < lenmin && sa[i] == sb[i]) {
			++i;
		}
	}
	else{
		while( i < lenmin && tolower(sa[i]) == tolower(sb[i])) {
			++i;
		}
	}
	/*
	 * some cases to get out of the way. If sa[i]
	 * and sb[i] are both non-digit (incl eos) we
	 * can declare a winner
	 */
	if( (i == lena || !ISDIGIT(sa[i]))
	   && (i == lenb || !ISDIGIT(sb[i])) ){
		if( i == lena ){
			CASE('A');		/* strings match if i==lenb or */
			return i-lenb;  /* a is prefix of b if i < lenb */
								
		}
		if ( i == lenb){
			CASE('B');
			return 1;				/* b is prefix of a */
		}
		CASE('C');
		if (case_sensitive)
			return sa[i]-sb[i];	/* compare the chars */
		else
			return tolower(sa[i])-tolower(sb[i]);	/* compare the chars */
	}
	/* at least one of sa[i], sb[i] is a digit.
	 * look back for more... */
	if(  i > 0 && ISDIGIT(sa[i-1]) ){
		do{
			--i;
		}while( i > 0 && ISDIGIT(sa[i-1]));
	}else if( i == lenmin ){	
		/* reached the end of one of the strings && didn't
		 * --i at all. So one is a prefix of the other and
		 * the prefix doesn't end in a digit. Eg:
		 * "KE8" vs "KE"  or  "" vs "12"
		 */
		CASE('D');
		return lena-lenb;
	}
	/* i < lenmin here.
	 * sa[i] and sb[i] could be both digits, or one each.
	 * Unless they are both digits, we can just do a lexical comp.
	 */
	if(!ISDIGIT(sa[i]) || !ISDIGIT(sb[i])){
		CASE('E');
		if(case_sensitive)
			return sa[i]-sb[i];
		else
			return tolower(sa[i])-tolower(sb[i]);
	}
	/* sa[i] and sb[i] are both digits, and are preceded by
	 * a common prefix which does not end in a digit.
	 * here's where we do an actual numeric compare...
	 */
	lena -= i;   /* discard common prefix... */
	lenb -= i;
	sa += i;
	sb += i;
	za = 0; na = 0;
	/* count any leading zeroes. */
	while( lena > 0 && *sa == '0'){
		++sa;
		++za;
		--lena;
	}
	/* count the digits */
	while( lena > 0 && ISDIGIT(*sa)){
		++sa;
		++na;
		--lena;
	}
	/* same for b */
	zb = 0; nb = 0;
	while( lenb > 0 && *sb == '0'){
		++sb;
		++zb;
		--lenb;
	}
	/* count the digits */
	while( lenb > 0 && ISDIGIT(*sb)){
		++sb;
		++nb;
		--lenb;
	}
	if( na != nb){		/* different # sig. digits */
		CASE('F');
		return na-nb;
	}
	i = 0;
	sa -= na;
	sb -= nb;		/* back up to 1st non-zero */
	while( i < na ){ /* note na cld be zero! "X00" vs "X000" */
		if(case_sensitive){
			if(sa[i] != sb[i]){
				CASE('G');	/* was a difference */
				return sa[i]-sb[i];
			}
		}else{
			if( tolower(sa[i]) != tolower(sb[i])){
				CASE('G');	/* was a difference */
				return tolower(sa[i])-tolower(sb[i]);
			}
		} /* else */
		++i;
	}
	CASE('H');
	return za-zb; 	/* most zeroes wins */
}

static PyObject*
C_numacomp(PyObject* self, PyObject* args)
{
	unsigned char *sa, *sb;
	int lena,lenb;
	int k;

	if (!PyArg_ParseTuple(args,"s#s#:numacomp",&sa, &lena,&sb,&lenb)) 
		return NULL;
	k = numacomp( sa, lena, sb, lenb, 1); /* case sensitive version */
	
	/*
	 * python compare may only return -1,0,1
	 */
	if (k < 0 ) 
		k = -1;
	else
		k = (k>0);

	return PyInt_FromLong(k);
}

static PyObject*
C_numacomp_i(PyObject* self, PyObject* args)
{
	unsigned char *sa, *sb;
	int lena,lenb;
	int k;

	if (!PyArg_ParseTuple(args,"s#s#:numacomp",&sa, &lena,&sb,&lenb)) 
		return NULL;
	k = numacomp( sa, lena, sb, lenb, 0 ); /* case insensitive version */
	
	/*
	 * python compare may only return -1,0,1
	 */
	if (k < 0 ) 
		k = -1;
	else
		k = (k>0);

	return PyInt_FromLong(k);
}

static PyMethodDef numacomp_methods[] = {
	{"numacomp", C_numacomp, METH_VARARGS, numacomp_docs },
	{"numacompi", C_numacomp_i, METH_VARARGS, numacompi_docs },
	{NULL, NULL, 0, NULL}
};

DL_EXPORT(void)
initmirage_numacomp(void) 
{
	Py_InitModule("mirage_numacomp", numacomp_methods);
}