NaturalStringCompare.cpp
4.38 KB
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
/*
* This software was written by people from OnShore Consulting services LLC
* <info@sabgroup.com> and placed in the public domain.
*
* We reserve no legal rights to any of this. You are free to do
* whatever you want with it. And we make no guarantee or accept
* any claims on damages as a result of this.
*
* If you change the software, please help us and others improve the
* code by sending your modifications to us. If you choose to do so,
* your changes will be included under this license, and we will add
* your name to the list of contributors.
*/
#include "NaturalStringCompare.h"
#include <QStringList>
#define INCBUF() { buffer += curr; ++pos; curr = ( pos < string.length() ) ? string[ pos ] : QChar(); }
void ExtractToken( QString & buffer, const QString & string, int & pos, bool & isNumber )
{
buffer.clear();
if ( string.isNull() || pos >= string.length() )
return;
isNumber = false;
QChar curr = string[ pos ];
if ( curr == '-' || curr == '+' || curr.isDigit() )
{
if ( curr == '-' || curr == '+' )
INCBUF();
if ( !curr.isNull() && curr.isDigit() )
{
isNumber = true;
while ( curr.isDigit() )
INCBUF();
if ( curr == '.' )
{
INCBUF();
while ( curr.isDigit() )
INCBUF();
}
if ( !curr.isNull() && curr.toLower() == 'e' )
{
INCBUF();
if ( curr == '-' || curr == '+' )
INCBUF();
if ( curr.isNull() || !curr.isDigit() )
isNumber = false;
else
while ( curr.isDigit() )
INCBUF();
}
}
}
if ( !isNumber )
{
while ( curr != '-' && curr != '+' && !curr.isDigit() && pos < string.length() )
INCBUF();
}
}
int NaturalStringCompare( const QString & lhs, const QString & rhs, Qt::CaseSensitivity caseSensitive )
{
int ii = 0;
int jj = 0;
QString lhsBufferQStr;
QString rhsBufferQStr;
int retVal = 0;
// all status values are created on the stack outside the loop to make as fast as possible
bool lhsNumber = false;
bool rhsNumber = false;
double lhsValue = 0.0;
double rhsValue = 0.0;
bool ok1;
bool ok2;
while ( retVal == 0 && ii < lhs.length() && jj < rhs.length() )
{
ExtractToken( lhsBufferQStr, lhs, ii, lhsNumber );
ExtractToken( rhsBufferQStr, rhs, jj, rhsNumber );
if ( !lhsNumber && !rhsNumber )
{
// both strings curr val is a simple strcmp
retVal = lhsBufferQStr.compare( rhsBufferQStr, caseSensitive );
int maxLen = qMin( lhsBufferQStr.length(), rhsBufferQStr.length() );
QString tmpRight = rhsBufferQStr.left( maxLen );
QString tmpLeft = lhsBufferQStr.left( maxLen );
if ( tmpLeft.compare( tmpRight, caseSensitive ) == 0 )
{
retVal = lhsBufferQStr.length() - rhsBufferQStr.length();
if ( retVal )
{
QChar nextChar;
if ( ii < lhs.length() ) // more on the lhs
nextChar = lhs[ ii ];
else if ( jj < rhs.length() ) // more on the rhs
nextChar = rhs[ jj ];
bool nextIsNum = ( nextChar == '-' || nextChar == '+' || nextChar.isDigit() );
if ( nextIsNum )
retVal = -1*retVal;
}
}
}
else if ( lhsNumber && rhsNumber )
{
// both numbers, convert and compare
lhsValue = lhsBufferQStr.toDouble( &ok1 );
rhsValue = rhsBufferQStr.toDouble( &ok2 );
if ( !ok1 || !ok2 )
retVal = lhsBufferQStr.compare( rhsBufferQStr, caseSensitive );
else if ( lhsValue > rhsValue )
retVal = 1;
else if ( lhsValue < rhsValue )
retVal = -1;
}
else
{
// completely arebitrary that a number comes before a string
retVal = lhsNumber ? -1 : 1;
}
}
if ( retVal != 0 )
return retVal;
if ( ii < lhs.length() )
return -1;
else if ( jj < rhs.length() )
return 1;
else
return 0;
}
bool NaturalStringCompareLessThan( const QString & lhs, const QString & rhs )
{
return NaturalStringCompare( lhs, rhs, Qt::CaseSensitive ) < 0;
}
bool NaturalStringCaseInsensitiveCompareLessThan( const QString & lhs, const QString & rhs )
{
return NaturalStringCompare( lhs, rhs, Qt::CaseInsensitive ) < 0;
}
QStringList NaturalStringSort( const QStringList & list, Qt::CaseSensitivity caseSensitive )
{
QStringList retVal = list;
if ( caseSensitive == Qt::CaseSensitive )
qSort( retVal.begin(), retVal.end(), NaturalStringCompareLessThan );
else
qSort( retVal.begin(), retVal.end(), NaturalStringCaseInsensitiveCompareLessThan );
return retVal;
}