-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.naturalsortstringlist.pas
237 lines (210 loc) · 6.11 KB
/
utils.naturalsortstringlist.pas
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
unit Utils.NaturalSortStringList;
interface
uses
{$if Defined(MsWindows)}
Windows,
{$endif}
Classes, SysUtils, LazUTF8, LconvEncoding;
type
{ TNaturalSortStringList }
TNaturalSortStringList = Class(TStringList)
private
protected
Function DoCompareText(const s1,s2 : string) : PtrInt; override;
public
procedure Sort; override;
End;
function NaturalCompare(const Item1, Item2 : String): Integer;
implementation
var
CodePage: integer;
CodePageString: string;
function RemoveDiacritics(const S: string): string;
// by SilvioProg
var
F: Boolean;
I: SizeInt;
PS, PD: PChar;
begin
SetLength(Result, Length(S));
PS := PChar(S);
PD := PChar(Result);
I := 0;
while PS^ <> #0 do
begin
F := PS^ = #195;
if F then
case PS[1] of
#128..#132: PD^ := #65;
#135: PD^ := #67; // letra Ç
#136..#139: PD^ := #69;
#140..#143: PD^ := #73;
#145: PD^ := #78; // letra Ñ
#146..#150: PD^ := #79;
#153..#156: PD^ := #85;
#157: PD^ := #89;
#160..#164: PD^ := #97;
#167: PD^ := #99; // letra ç
#168..#171: PD^ := #101;
#172..#175: PD^ := #105;
#177: PD^ := #110;
#178..#182: PD^ := #111; // letra o
#185..#188: PD^ := #117;
#189..#191: PD^ := #121;
else
F := False;
end;
if F then
Inc(PS)
else
PD^ := PS^;
Inc(I);
Inc(PD);
Inc(PS);
end;
SetLength(Result, I);
end;
function EvsCompareNatural_A(const S1, S2 :AnsiString; aCaseSensitive : Boolean = True):integer;
// by taazz
var
vChr1, vChr2 : PChar;
vCmp1, vCmp2 : string;
function Sign(aNo:integer):integer; inline;
begin
Result := 0;
if aNo > 0 then Result := 1
else if aNo < 0 then Result := -1;
end;
function NumCharLen(const aStart:PAnsiChar):Integer;
var
vNext : PAnsiChar;
begin
vNext := aStart;
repeat
inc(vNext);
until (vNext^ = #0) or (not (vNext^ in ['0'..'9']));
Result := vNext-aStart;
end;
function CompToChar(var aStr:PAnsiChar; const aChar:Char; aCount:Integer):Integer;inline;
begin // compares the next aCount characters of aStr with aChar and returns 0 if they are all the same
Result := 0; // or <>0 if they differ. It is used to avoid padding a string with zeros.
repeat
Result := sign(Ord(aStr^) - ord(aChar));
Dec(aCount); Inc(aStr);
until (Result <> 0) or (aStr^=#0) or (aCount = 0);
end; //when checking numeric characters[0..9] against zero it always returns a positive number.
function NumComp:Integer;
var
vNl : Integer;
begin
Result := -2;
vNl := NumCharLen(vChr1) - NumCharLen(vChr2);
if vNl < 0 then Result := CompToChar(vChr2, '0', abs(vNl))
else if vNl > 0 then Result := CompToChar(vChr1, '0', vNl);
if (Result > 0) then begin
Result := Sign(vNl);
Exit;
end;
repeat
Result := sign(ord(vChr1^) - ord(vChr2^));
inc(vChr1); inc(vChr2);
until ((vChr1^=#0) or (vChr2^=#0)) //end of string has been reached
or (Result <> 0) //conclusion has been reached
or (not (vChr1^ in ['0'..'9'])) //numeric characters end here
or (not (vChr2^ in ['0'..'9'])); //numeric characters end here
if Result = 0 then begin
if vChr1^ in ['0'..'9'] then Result := 1
else if vChr2^ in ['0'..'9'] then Result := -1;
end;
end;
begin
//s1<s2 = -1, S1=S2 =0, S1>S2 = 1;
if aCaseSensitive then begin
vChr1 := @S1[1]; vChr2 := @S2[1]
end else begin
vCmp1 := LowerCase(S1); vCmp2 := LowerCase(S2);
vChr1 := @vCmp1[1]; vChr2 := @vCmp2[1];
end;
repeat
if (vChr1^ in ['0'..'9']) and (vChr2^ in ['0'..'9']) then
Result := NumComp // it exits ready in the next position
else begin
Result := Sign(ord(vChr1^)- ord(vChr2^));
if vChr1^ <> #0 then inc(vChr1);
if vChr2^ <> #0 then inc(vChr2);
end;
until (vChr1^=#0) or (vChr2^=#0) or (Result <> 0);
if (Result = 0) then Result := Sign(ord(vChr1^) - Ord(vChr2^));
end;
function NaturalCompare(const Item1, Item2 : String): Integer;
var
Str1, Str2 :string;
{$IFDEF LINUX}
lang :string;
{$endif}
begin
Str1 := RemoveDiacritics(Item1);
Str2 := RemoveDiacritics(Item2);
// Case insensitive.
{$IFDEF MsWindows}
if CodePage = 1252 then // Latin chars
begin
Result := EvsCompareNatural_A(Str1, Str2, False);
// Places unsigned words (without diacritics) before the
// signed ones (with diacritics) when they are equal.
// The order among signed ones is preserved.
if Str1 = Str2 then
Result := EvsCompareNatural_A(Str1, Str2, False);
end
else
Result := EvsCompareNatural_A(Str1, Str2, False);
{$ELSE}
{$IFDEF LINUX}
if CodePageString <> '' then
begin
lang := CodePageString[1] + CodePageString[2];
if (lang = 'pt') or (lang = 'es') or (lang = 'fr') or (lang = 'it')
or (lang = 'gl' {galician}) or (lang = 'gn' {guarani}) or (lang = 'ay' {aymará})
then
begin
Result := EvsCompareNatural_A(Str1, Str2, False);
if Str1 = Str2 then
Result := EvsCompareNatural_A(Str1, Str2, False);
end
else
Result := EvsCompareNatural_A(Str1, Str2, False)
end;
{$ELSE}
Result := EvsCompareNatural_A(Str1, Str2, False)
{$ENDIF}
{$ENDIF}
end;
function NaturalSortCompare(aList: TStringList; Index1, Index2: Integer): Integer;
begin
//result := NaturalCompareText(aList[Index1], aList[Index2]);
result := NaturalCompare(aList[Index1], aList[Index2]);
end;
{ TNaturalSortStringList }
function TNaturalSortStringList.DoCompareText(const s1, s2: string): PtrInt;
begin
Result:=UTF8CompareStr(s1, s2);
end;
procedure TNaturalSortStringList.Sort;
begin
CustomSort(@NaturalSortCompare);
end;
initialization
CodePage := 0;
CodePageString := '';
{$IFDEF MsWindows}
CodePage := GetACP;
{$ELSE}
{$IFDEF LINUX}
CodePageString := sysutils.GetEnvironmentVariable('LC_ALL');
if CodePageString = '' then
CodePageString := sysutils.GetEnvironmentVariable('LC_CTYPE');
if CodePageString = '' then
CodePageString := sysutils.GetEnvironmentVariable('LANG');
{$ENDIF}
{$ENDIF}
end.