in jspwiki-util/src/main/java/org/apache/wiki/util/comparators/HumanComparator.java [82:173]
public int compare(final String str1, final String str2 )
{
// Some quick and easy checks
if( StringUtils.equals( str1, str2 ) ) {
// they're identical, possibly both null
return 0;
} else if ( str1 == null ) {
// str1 is null and str2 isn't so str1 is smaller
return -1;
} else if ( str2 == null ) {
// str2 is null and str1 isn't so str1 is bigger
return 1;
}
final char[] s1 = str1.toCharArray();
final char[] s2 = str2.toCharArray();
final int len1 = s1.length;
final int len2 = s2.length;
int idx = 0;
// caseComparison used to defer a case sensitive comparison
int caseComparison = 0;
while ( idx < len1 && idx < len2 )
{
char c1 = s1[idx];
char c2 = s2[idx++];
// Convert to lower case
final char lc1 = Character.toLowerCase( c1 );
final char lc2 = Character.toLowerCase( c2 );
// If case makes a difference, note the difference the first time
// it's encountered
if( caseComparison == 0 && c1 != c2 && lc1 == lc2 )
{
if( Character.isLowerCase( c1 ) )
caseComparison = 1;
else if( Character.isLowerCase( c2 ) )
caseComparison = -1;
}
// Do the rest of the tests in lower case
c1 = lc1;
c2 = lc2;
// leading zeros are a special case
if( c1 != c2 || c1 == '0' )
{
// They might be different, now we can do a comparison
final CharType type1 = mapCharTypes( c1 );
final CharType type2 = mapCharTypes( c2 );
// Do the character class check
int result = compareCharTypes( type1, type2 );
if( result != 0 )
{
// different character classes so that's sufficient
return result;
}
// If they're not digits, use character to character comparison
if( type1 != CharType.TYPE_DIGIT )
{
final Character ch1 = c1;
final Character ch2 = c2;
return ch1.compareTo( ch2 );
}
// The only way to get here is both characters are digits
assert( type1 == CharType.TYPE_DIGIT && type2 == CharType.TYPE_DIGIT );
result = compareDigits( s1, s2, idx - 1 );
if( result != 0 )
{
// Got a result so return it
return result;
}
// No result yet, spin through the digits and continue trying
while ( idx < len1 && idx < len2 && Character.isDigit( s1[idx] ) ) {
idx++;
}
}
}
if( len1 == len2 )
{
// identical so return any case dependency
return caseComparison;
}
// Shorter String is less
return len1 - len2;
}