mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Only use numeric collation for sorting since it breaks searching
This commit is contained in:
parent
5b48a4c07b
commit
b35fceb2df
@ -59,7 +59,6 @@ icu_Collator_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|||||||
PyErr_SetString(PyExc_Exception, "Failed to create collator.");
|
PyErr_SetString(PyExc_Exception, "Failed to create collator.");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
ucol_setAttribute(collator, UCOL_NUMERIC_COLLATION, UCOL_ON, &status);
|
|
||||||
|
|
||||||
self = (icu_Collator *)type->tp_alloc(type, 0);
|
self = (icu_Collator *)type->tp_alloc(type, 0);
|
||||||
if (self != NULL) {
|
if (self != NULL) {
|
||||||
|
@ -12,7 +12,7 @@ from functools import partial
|
|||||||
from calibre.constants import plugins
|
from calibre.constants import plugins
|
||||||
from calibre.utils.config_base import tweaks
|
from calibre.utils.config_base import tweaks
|
||||||
|
|
||||||
_icu = _collator = _primary_collator = _secondary_collator = None
|
_icu = _collator = _primary_collator = _sort_collator = None
|
||||||
_locale = None
|
_locale = None
|
||||||
|
|
||||||
_none = u''
|
_none = u''
|
||||||
@ -41,28 +41,34 @@ def load_icu():
|
|||||||
return _icu
|
return _icu
|
||||||
|
|
||||||
def load_collator():
|
def load_collator():
|
||||||
|
'The default collator for most locales takes both case and accented letters into account'
|
||||||
global _collator
|
global _collator
|
||||||
if _collator is None:
|
if _collator is None:
|
||||||
icu = load_icu()
|
icu = load_icu()
|
||||||
if icu is not None:
|
if icu is not None:
|
||||||
_collator = icu.Collator(get_locale())
|
_collator = icu.Collator(get_locale())
|
||||||
if not tweaks['numeric_collation']:
|
|
||||||
_collator.numeric = False
|
|
||||||
return _collator
|
return _collator
|
||||||
|
|
||||||
def primary_collator():
|
def primary_collator():
|
||||||
|
'Ignores case differences and accented characters'
|
||||||
global _primary_collator
|
global _primary_collator
|
||||||
if _primary_collator is None:
|
if _primary_collator is None:
|
||||||
_primary_collator = _collator.clone()
|
_primary_collator = _collator.clone()
|
||||||
_primary_collator.strength = _icu.UCOL_PRIMARY
|
_primary_collator.strength = _icu.UCOL_PRIMARY
|
||||||
return _primary_collator
|
return _primary_collator
|
||||||
|
|
||||||
def secondary_collator():
|
def sort_collator():
|
||||||
global _secondary_collator
|
'Ignores case differences and recognizes numbers in strings'
|
||||||
if _secondary_collator is None:
|
global _sort_collator
|
||||||
_secondary_collator = _collator.clone()
|
if _sort_collator is None:
|
||||||
_secondary_collator.strength = _icu.UCOL_SECONDARY
|
_sort_collator = _collator.clone()
|
||||||
return _secondary_collator
|
_sort_collator.strength = _icu.UCOL_SECONDARY
|
||||||
|
if tweaks['numeric_collation']:
|
||||||
|
try:
|
||||||
|
_sort_collator.numeric = True
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
return _sort_collator
|
||||||
|
|
||||||
def py_sort_key(obj):
|
def py_sort_key(obj):
|
||||||
if not obj:
|
if not obj:
|
||||||
@ -74,15 +80,15 @@ def icu_sort_key(collator, obj):
|
|||||||
return _none2
|
return _none2
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
return _secondary_collator.sort_key(obj)
|
return _sort_collator.sort_key(obj)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return secondary_collator().sort_key(obj)
|
return sort_collator().sort_key(obj)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
if isinstance(obj, unicode):
|
if isinstance(obj, unicode):
|
||||||
obj = obj.replace(u'\0', u'')
|
obj = obj.replace(u'\0', u'')
|
||||||
else:
|
else:
|
||||||
obj = obj.replace(b'\0', b'')
|
obj = obj.replace(b'\0', b'')
|
||||||
return _secondary_collator.sort_key(obj)
|
return _sort_collator.sort_key(obj)
|
||||||
|
|
||||||
def icu_change_case(upper, locale, obj):
|
def icu_change_case(upper, locale, obj):
|
||||||
func = _icu.upper if upper else _icu.lower
|
func = _icu.upper if upper else _icu.lower
|
||||||
@ -235,9 +241,9 @@ def collation_order(a):
|
|||||||
if _icu_not_ok:
|
if _icu_not_ok:
|
||||||
return (ord(a[0]), 1) if a else (0, 0)
|
return (ord(a[0]), 1) if a else (0, 0)
|
||||||
try:
|
try:
|
||||||
return icu_collation_order(_secondary_collator, a)
|
return icu_collation_order(_sort_collator, a)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return icu_collation_order(secondary_collator(), a)
|
return icu_collation_order(sort_collator(), a)
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user