From owner-svn-src-projects@freebsd.org Fri Oct 23 23:24:05 2015 Return-Path: Delivered-To: svn-src-projects@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 0E547A1DBB5 for ; Fri, 23 Oct 2015 23:24:05 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id DB1B0117; Fri, 23 Oct 2015 23:24:04 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id t9NNO37h056496; Fri, 23 Oct 2015 23:24:03 GMT (envelope-from bapt@FreeBSD.org) Received: (from bapt@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id t9NNO3uL056494; Fri, 23 Oct 2015 23:24:03 GMT (envelope-from bapt@FreeBSD.org) Message-Id: <201510232324.t9NNO3uL056494@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: bapt set sender to bapt@FreeBSD.org using -f From: Baptiste Daroussin Date: Fri, 23 Oct 2015 23:24:03 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org Subject: svn commit: r289860 - in projects/collation/lib/libc: locale string X-SVN-Group: projects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 23 Oct 2015 23:24:05 -0000 Author: bapt Date: Fri Oct 23 23:24:03 2015 New Revision: 289860 URL: https://svnweb.freebsd.org/changeset/base/289860 Log: collate: Fix expansion substitions (broken upstream too) Through testing, the user noted that some Cyrillic characters were not sorting correctly, and this was confirmed. After extensive testing and review, the localedef tool was eliminated as the culprit. The sustitutions were encoded correctly in LC_COLLATE. The error was mainly in wcscoll where character expansions were mishandled. The main directive pass routines had to be written to go back for a new collation value when the "state" variable was set. Before pointers were being advanced, the second lookup was gettting applied to the wrong character, etc. The "eat expansion codes" section on collate.c also had a bug. Later own, the "state" variable logic was changed to only set if next code was greater than zero (rather than >= 0). Some additional cleanups got captured from previous work: 1) The previous commit moved the binary search comment from the correct location to a wrong location because it's wrong upstream in Illumos. The comment has little value so I just removed it. 2) Don't check if pointers are null before freeing, this is redundant as free() handles null pointers. 3) The two binary search trees were standardized wrt initialization 4) On the binary search trees, a negative "high" exits rather than checking the table count again. Submitted by: marino Obtained from: DragonflyBSD Modified: projects/collation/lib/libc/locale/collate.c projects/collation/lib/libc/string/wcscoll.c Modified: projects/collation/lib/libc/locale/collate.c ============================================================================== --- projects/collation/lib/libc/locale/collate.c Fri Oct 23 23:07:45 2015 (r289859) +++ projects/collation/lib/libc/locale/collate.c Fri Oct 23 23:24:03 2015 (r289860) @@ -202,7 +202,6 @@ __collate_load_tables_l(const char *enco table->large_pri_table = NULL; table->__collate_load_error = 0; - return (_LDP_LOADED); } @@ -226,27 +225,18 @@ substsearch(struct xlocale_collate *tabl return (p->pri); } -/* - * Note: for performance reasons, we have expanded bsearch here. This avoids - * function call overhead with each comparison. - */ - static collate_chain_t * chainsearch(struct xlocale_collate *table, const wchar_t *key, int *len) { - int low; - int high; + int low = 0; + int high = table->info->chain_count - 1;; int next, compar, l; collate_chain_t *p; - collate_chain_t *tab; + collate_chain_t *tab = table->chain_pri_table; - if (table->info->chain_count == 0) + if (high < 0) return (NULL); - low = 0; - high = table->info->chain_count - 1; - tab = table->chain_pri_table; - while (low <= high) { next = (low + high) / 2; p = tab + next; @@ -276,7 +266,7 @@ largesearch(struct xlocale_collate *tabl collate_large_t *p; collate_large_t *tab = table->large_pri_table; - if (table->info->large_count == 0) + if (high < 0) return (NULL); while (low <= high) { @@ -320,7 +310,10 @@ _collate_lookup(struct xlocale_collate * if ((sptr = *state) != NULL) { *pri = *sptr; sptr++; - *state = *sptr ? sptr : NULL; + if ((sptr == *state) || (sptr == NULL)) + *state = NULL; + else + *state = sptr; *len = 0; return; } @@ -381,7 +374,7 @@ _collate_lookup(struct xlocale_collate * * code ensures this for us. */ if ((sptr = substsearch(table, *pri, which)) != NULL) { - if ((*pri = *sptr) != 0) { + if ((*pri = *sptr) > 0) { sptr++; *state = *sptr ? sptr : NULL; } Modified: projects/collation/lib/libc/string/wcscoll.c ============================================================================== --- projects/collation/lib/libc/string/wcscoll.c Fri Oct 23 23:07:45 2015 (r289859) +++ projects/collation/lib/libc/string/wcscoll.c Fri Oct 23 23:24:03 2015 (r289860) @@ -77,6 +77,7 @@ wcscoll_l(const wchar_t *ws1, const wcha const int32_t *st2 = NULL; const wchar_t *w1 = ws1; const wchar_t *w2 = ws2; + int check1, check2; /* special pass for UNDEFINED */ if (pass == table->info->directive_count) { @@ -110,25 +111,36 @@ wcscoll_l(const wchar_t *ws1, const wcha } if (direc & DIRECTIVE_POSITION) { - while ((*w1 || st1) && (*w2 || st2)) { + while (*w1 && *w2) { pri1 = pri2 = 0; - _collate_lookup(table, w1, &len1, &pri1, pass, - &st1); - if (pri1 <= 0) { - if (pri1 < 0) { - errno = EINVAL; - goto fail; + check1 = check2 = 1; + while ((pri1 == pri2) && (check1 || check2)) { + if (check1) { + _collate_lookup(table, w1, &len1, + &pri1, pass, &st1); + if (pri1 < 0) { + errno = EINVAL; + goto fail; + } + if (!pri1) { + pri1 = COLLATE_MAX_PRIORITY; + st1 = NULL; + } + check1 = (st1 != NULL); } - pri1 = COLLATE_MAX_PRIORITY; - } - _collate_lookup(table, w2, &len2, &pri2, pass, - &st2); - if (pri2 <= 0) { - if (pri2 < 0) { - errno = EINVAL; - goto fail; + if (check2) { + _collate_lookup(table, w2, &len2, + &pri2, pass, &st2); + if (pri2 < 0) { + errno = EINVAL; + goto fail; + } + if (!pri2) { + pri2 = COLLATE_MAX_PRIORITY; + st2 = NULL; + } + check2 = (st2 != NULL); } - pri2 = COLLATE_MAX_PRIORITY; } if (pri1 != pri2) { ret = pri1 - pri2; @@ -138,29 +150,38 @@ wcscoll_l(const wchar_t *ws1, const wcha w2 += len2; } } else { - while ((*w1 || st1) && (*w2 || st2)) { + while (*w1 && *w2) { pri1 = pri2 = 0; - while (*w1) { - _collate_lookup(table, w1, &len1, - &pri1, pass, &st1); - if (pri1 > 0) - break; - if (pri1 < 0) { - errno = EINVAL; - goto fail; + check1 = check2 = 1; + while ((pri1 == pri2) && (check1 || check2)) { + while (check1 && *w1) { + _collate_lookup(table, w1, + &len1, &pri1, pass, &st1); + if (pri1 > 0) + break; + if (pri1 < 0) { + errno = EINVAL; + goto fail; + } + st1 = NULL; + w1 += 1; } - w1 += len1; - } - while (*w2) { - _collate_lookup(table, w2, &len2, - &pri2, pass, &st2); - if (pri2 > 0) - break; - if (pri2 < 0) { - errno = EINVAL; - goto fail; + check1 = (st1 != NULL); + while (check2 && *w2) { + _collate_lookup(table, w2, + &len2, &pri2, pass, &st2); + if (pri2 > 0) + break; + if (pri2 < 0) { + errno = EINVAL; + goto fail; + } + st2 = NULL; + w2 += 1; } - w2 += len2; + check2 = (st2 != NULL); + if (!pri1 || !pri2) + break; } if (!pri1 || !pri2) break; @@ -185,10 +206,8 @@ wcscoll_l(const wchar_t *ws1, const wcha ret = 0; end: - if (tr1) - free(tr1); - if (tr2) - free(tr2); + free(tr1); + free(tr2); return (ret);