]> Savannah Git Hosting - gnulib.git/commitdiff
filevercmp: don’t treat entire filename as suffix
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 4 Jun 2022 00:27:44 +0000 (17:27 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 4 Jun 2022 00:30:39 +0000 (17:30 -0700)
Problem reported by Artém S. Tashkinóv in:
https://lists.gnu.org/r/bug-gnulib/2022-06/msg00012.html
* lib/filevercmp.c (file_prefixlen): When stripping
(\.[A-Za-z~][A-Za-z0-9~]*)*$ suffixes, do not strip
the entire file name.
* tests/test-filevercmp.c (examples): Adjust to match new behavior.

ChangeLog
lib/filevercmp.c
lib/filevercmp.h
tests/test-filevercmp.c

index 4449ff14f91be763022a93b2bc4ed3d682f132d7..95d1314cdc785b0c4ed951ec90aa7bcb0d77d44c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2022-06-03  Paul Eggert  <eggert@cs.ucla.edu>
+
+       filevercmp: don’t treat entire filename as suffix
+       Problem reported by Artém S. Tashkinóv in:
+       https://lists.gnu.org/r/bug-gnulib/2022-06/msg00012.html
+       * lib/filevercmp.c (file_prefixlen): When stripping
+       (\.[A-Za-z~][A-Za-z0-9~]*)*$ suffixes, do not strip
+       the entire file name.
+       * tests/test-filevercmp.c (examples): Adjust to match new behavior.
+
 2022-06-03  Bruno Haible  <bruno@clisp.org>
 
        setlocale: Update after Turkey changed its name.
index d546e790548b0fc3c3001212fe607256c2e849dd..7e54793e613d442b53567d85ddf7d8710e7a4ee6 100644 (file)
@@ -29,6 +29,8 @@
 /* Return the length of a prefix of S that corresponds to the suffix
    defined by this extended regular expression in the C locale:
      (\.[A-Za-z~][A-Za-z0-9~]*)*$
+   Use the longest suffix matching this regular expression,
+   except do not use all of S as a suffix if S is nonempty.
    If *LEN is -1, S is a string; set *LEN to S's length.
    Otherwise, *LEN should be nonnegative, S is a char array,
    and *LEN does not change.  */
@@ -36,20 +38,22 @@ static idx_t
 file_prefixlen (char const *s, ptrdiff_t *len)
 {
   size_t n = *len;  /* SIZE_MAX if N == -1.  */
+  idx_t prefixlen = 0;
 
-  for (idx_t i = 0; ; i++)
+  for (idx_t i = 0; ; )
     {
-      idx_t prefixlen = i;
-      while (i + 1 < n && s[i] == '.' && (c_isalpha (s[i + 1])
-                                          || s[i + 1] == '~'))
-        for (i += 2; i < n && (c_isalnum (s[i]) || s[i] == '~'); i++)
-          continue;
-
       if (*len < 0 ? !s[i] : i == n)
         {
           *len = i;
           return prefixlen;
         }
+
+      i++;
+      prefixlen = i;
+      while (i + 1 < n && s[i] == '.' && (c_isalpha (s[i + 1])
+                                          || s[i + 1] == '~'))
+        for (i += 2; i < n && (c_isalnum (s[i]) || s[i] == '~'); i++)
+          continue;
     }
 }
 
index 5a3367767197e4d5d7ffd16cce37be67708ecd40..57949760b25eafa946add9d34b2ef998d501d9e9 100644 (file)
@@ -61,7 +61,9 @@
    without them, using version sort without special priority;
    if they do not compare equal, this comparison result is used and
    the suffixes are effectively ignored.  Otherwise, the entire
-   strings are compared using version sort.
+   strings are compared using version sort.  When removing a suffix
+   from a nonempty string, remove the maximal-length suffix such that
+   the remaining string is nonempty.
 
    This function is intended to be a replacement for strverscmp.  */
 int filevercmp (char const *a, char const *b) _GL_ATTRIBUTE_PURE;
index b2a7e90f3fe1bda01a82f1861a1f85899e5e6c0b..998250990dac1c078a1b438e14aafe86389ed6bc 100644 (file)
@@ -29,6 +29,8 @@ static const char *const examples[] =
   "",
   ".",
   "..",
+  ".0",
+  ".9",
   ".A",
   ".Z",
   ".a~",
@@ -39,8 +41,6 @@ static const char *const examples[] =
   ".zz~",
   ".zz",
   ".zz.~1~",
-  ".0",
-  ".9",
   ".zz.0",
   ".\1",
   ".\1.txt",