@@ -23,12 +23,15 @@
#include <stddef.h>
#include <stdint.h>
#include <string.h>
+#include <stdio.h>
#include <sys/param.h>
+#include <bits/libc-lock.h>
#ifndef STRING_TYPE
# define STRING_TYPE char
# define USTRING_TYPE unsigned char
# define STRCOLL __strcoll_l
+# define STRDIFF __strdiff
# define STRCMP strcmp
# define WEIGHT_H "../locale/weight.h"
# define SUFFIX MB
@@ -41,6 +44,87 @@
#include "../locale/localeinfo.h"
#include WEIGHT_H
+/* Mapping of locale to encoding type. */
+typedef struct encoding
+{
+ int type;
+ const char *locale;
+ struct encoding *next;
+} *encoding_t;
+
+#define ENCODING_OTHER 0
+#define ENCODING_8BIT 1
+#define ENCODING_UTF8 2
+
+__libc_lock_define (typedef, lock_t)
+static encoding_t encodings = NULL;
+static lock_t encodings_lock = LLL_LOCK_INITIALIZER;
+
+static int
+strsuffix(const char *str, const char *suffix)
+{
+ if (!str || !suffix)
+ return 0;
+ size_t lenstr = strlen (str);
+ size_t lensuffix = strlen (suffix);
+ if (lensuffix > lenstr)
+ return 0;
+ return strncmp (str + lenstr - lensuffix, suffix, lensuffix) == 0;
+}
+
+static int
+get_encoding (const char *locale, struct __locale_data * ctype)
+{
+ encoding_t encoding = encodings;
+ while (encoding != NULL)
+ {
+ if (encoding->locale == locale)
+ return encoding->type;
+ encoding = encoding->next;
+ }
+
+ /* Add new encoding type. Lock encoding map and check if another thread
+ has already added the new type while waiting for the lock. */
+ __libc_lock_lock (encodings_lock);
+
+ encoding = encodings;
+ while (encoding != NULL)
+ {
+ if (encoding->locale == locale)
+ {
+ __libc_lock_unlock (encodings_lock);
+ return encoding->type;
+ }
+ encoding = encoding->next;
+ }
+
+ encoding_t new_encoding = malloc (sizeof (encoding_t*));
+ size_t mb_cur_max = ctype->values[_NL_ITEM_INDEX (_NL_CTYPE_MB_CUR_MAX)].word;
+ if (mb_cur_max == 1)
+ new_encoding->type = ENCODING_8BIT;
+ else if (strsuffix (locale, "UTF-8"))
+ new_encoding->type = ENCODING_UTF8;
+ else
+ new_encoding->type = ENCODING_OTHER;
+ new_encoding->locale = locale;
+ new_encoding->next = encodings;
+ encodings = new_encoding;
+
+ __libc_lock_unlock (encodings_lock);
+ return new_encoding->type;
+}
+
+size_t
+STRDIFF (const STRING_TYPE *s, const STRING_TYPE *t)
+{
+ size_t n;
+
+ for (n = 0; *s != '\0' && *s++ == *t++; ++n)
+ continue;
+
+ return n;
+}
+
/* Track status while looking for sequences in a string. */
typedef struct
{
@@ -242,6 +326,9 @@ out:
return result;
}
+#define MASK_UTF8_7BIT (1 << 7)
+#define MASK_UTF8_START (3 << 6)
+
int
STRCOLL (const STRING_TYPE *s1, const STRING_TYPE *s2, __locale_t l)
{
@@ -255,9 +342,28 @@ STRCOLL (const STRING_TYPE *s1, const STRING_TYPE *s2,
__locale_t l)
const USTRING_TYPE *extra;
const int32_t *indirect;
+ /* In case there is no locale specific sort order (C locale). */
if (nrules == 0)
return STRCMP (s1, s2);
+ /* Fast forward to the position of the first difference. Needs to be
+ encoding aware as the byte-by-byte comparison can stop in the midle
+ of a char sequence for non fixed width encodings like UTF-8. */
+ int encoding = get_encoding (l->__names[LC_CTYPE], l->__locales[LC_CTYPE]);
+ if (encoding != ENCODING_OTHER)
+ {
+ size_t diff = STRDIFF (s1, s2);
+ if (diff > 0)
+ {
+ if (encoding == ENCODING_UTF8 && (*(s1 + diff) & MASK_UTF8_7BIT) != 0)
+ do
+ diff--;
+ while (diff > 0 && (*(s1 + diff) & MASK_UTF8_START) != MASK_UTF8_START);
+ s1 += diff;
+ s2 += diff;
+ }
+ }
+
/* Catch empty strings. */
if (__glibc_unlikely (*s1 == '\0') || __glibc_unlikely (*s2 == '\0'))
return (*s1 != '\0') - (*s2 != '\0');
@@ -321,7 +427,8 @@ STRCOLL (const STRING_TYPE *s1, const STRING_TYPE *s2,
__locale_t l)
byte-level comparison to ensure that we don't waste time
going through multiple passes for totally equal strings
before proceeding to subsequent passes. */
- if (pass == 0 && STRCMP (s1, s2) == 0)
+ if (pass == 0 && encoding == ENCODING_OTHER &&
+ STRCMP (s1, s2) == 0)
return result;
else
break;
@@ -23,6 +23,7 @@
#define STRING_TYPE wchar_t
#define USTRING_TYPE wint_t
#define STRCOLL __wcscoll_l
+#define STRDIFF __wcsdiff
#define STRCMP wcscmp
#define WEIGHT_H "../locale/weightwc.h"
#define SUFFIX WC