-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstringComparePlus.sc
executable file
·195 lines (153 loc) · 4.66 KB
/
stringComparePlus.sc
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
// adapted from http://sourcefrog.net/projects/natsort/strnatcmp.c
// by Martin Pool
+ String {
compareRight{ |string|
var bias = 0, a, b;
/* The longest run of digits wins. That aside, the greatest
value wins, but we can't know that it will until we've scanned
both numbers to know that they have the same magnitude, so we
remember it in BIAS. */
inf.do({|i|
a = this[i];
b = string[i];
if ((a.isNil or: {a.isDecDigit.not}) && (b.isNil or: {b.isDecDigit.not}), {^bias});
if (a.isNil or: {a.isDecDigit.not}, {^-1});
if (b.isNil or: {b.isDecDigit.not}, {^1});
if (a < b, {
if (bias == 0, { bias = -1 });
}, {
if (a > b, {
if (bias == 0, { bias = 1 },{
if (a.isNil && b.isNil, {^bias});
});
});
});
});
^0;
}
compareLeft { |string|
var a, b;
/* Compare two left-aligned numbers: the first to have a
different value wins. */
inf.do({|i|
a = this[i];
b = string[i];
if((a.isNil or: {a.isDecDigit.not}) && (b.isNil or: {b.isDecDigit.not}), {^0});
if(a.isNil or: {a.isDecDigit.not}, {^-1});
if(b.isNil or: {b.isDecDigit.not}, {^1});
if(a < b, {^-1});
if(a > b, {^1});
});
^0;
}
naturalCompare { |string, ignoreCase = false|
var ai, bi; // int
var ca, cb; // nat_char
var fractional, result; // int
ai = bi = 0;
while(true, {
ca = this[ai]; cb = string[bi];
/* skip over leading spaces or zeros */
while ({ca.notNil and: {ca.isSpace}}, {ca = this[ai = ai + 1]});
while ({cb.notNil and: {cb.isSpace}}, {cb = string[bi = bi + 1]});
if (ca.isNil && cb.isNil, {^0});
/* The strings compare the same. Perhaps the caller
will want to call strcmp to break the tie. */
/* process run of digits */
if (ca.notNil && cb.notNil and: {ca.isDecDigit && cb.isDecDigit}, {
fractional = (ca == $0 || cb == $0);
if (fractional, {
if((result = this.copyToEnd(ai).compareLeft(string.copyToEnd(bi))) != 0, {
^result
});
}, {
if((result = this.copyToEnd(ai).compareRight(string.copyToEnd(bi))) != 0, {
^result
});
});
});
if (ignoreCase, {
ca = ca.toUpper;
cb = cb.toUpper;
});
if (ca < cb, {^-1});
if (ca > cb, {^1});
ai = ai + 1; bi = bi + 1;
});
}
naturalCompareWithSpaces { |string, ignoreCase = false|
var ai, bi; // int
var ca, cb; // nat_char
var fractional, result; // int
ai = bi = 0;
while(true, {
ca = this[ai]; cb = string[bi];
/* skip over leading spaces or zeros */
/*while ({ca.notNil and: {ca.isSpace}}, {ca = this[ai = ai + 1]});
while ({cb.notNil and: {cb.isSpace}}, {cb = string[bi = bi + 1]});
if (ca.isNil && cb.isNil, {^0});*/
/* The strings compare the same. Perhaps the caller
will want to call strcmp to break the tie. */
/* process run of digits */
if (ca.notNil && cb.notNil and: {ca.isDecDigit && cb.isDecDigit}, {
fractional = (ca == $0 || cb == $0);
if (fractional, {
if((result = this.copyToEnd(ai).compareLeft(string.copyToEnd(bi))) != 0, {
^result
});
}, {
if((result = this.copyToEnd(ai).compareRight(string.copyToEnd(bi))) != 0, {
^result
});
});
});
if (ignoreCase, {
ca = ca.toUpper;
cb = cb.toUpper;
});
if (ca < cb, {^-1});
if (ca > cb, {^1});
ai = ai + 1; bi = bi + 1;
});
}
// Test pour essayer que 2.WAV soit placé après 2#1.WAV dans dossier PianoCagedRusted -> comment avoir le même classement que dans le Finder OSX ?????????
naturalCompareWithSpaces2 { |string, ignoreCase = false|
var ai, bi; // int
var ca, cb; // nat_char
var fractional, result; // int
ai = bi = 0;
while(true, {
ca = this[ai]; cb = string[bi];
/* skip over leading spaces or zeros */
/*while ({ca.notNil and: {ca.isSpace}}, {ca = this[ai = ai + 1]});
while ({cb.notNil and: {cb.isSpace}}, {cb = string[bi = bi + 1]});
if (ca.isNil && cb.isNil, {^0});*/
/* The strings compare the same. Perhaps the caller
will want to call strcmp to break the tie. */
/* process run of digits */
if (ca.notNil && cb.notNil and: {ca.isDecDigit && cb.isDecDigit}, {
fractional = (ca == $0 || cb == $0);
if (fractional, {
if((result = this.copyToEnd(ai).compareLeft(string.copyToEnd(bi))) != 0, {
^result
});
}, {
if((result = this.copyToEnd(ai).compareRight(string.copyToEnd(bi))) != 0, {
^result
});
});
});
if (ignoreCase, {
ca = ca.toUpper;
cb = cb.toUpper;
});
if (ca < cb, {^-1});
if (ca > cb, {^1});
/*if (ca.isNil and: cb.notNil, {^-1});
if (ca.notNil and: cb.isNil, {^1});*/
if (ca.size < cb.size, {^-1});
if (ca.size > cb.size, {^1});
ai = ai + 1; bi = bi + 1;
});
}
}