]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs_scrub: hoist code that removes ignorable characters
authorDarrick J. Wong <djwong@kernel.org>
Mon, 29 Jul 2024 23:23:09 +0000 (16:23 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Tue, 30 Jul 2024 00:01:08 +0000 (17:01 -0700)
Hoist the loop that removes "ignorable" code points from the skeleton
string into a separate function and give the UChar cursors names that
are easier to understand.  Convert the code to use the safe versions of
the U16_ accessor functions.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
scrub/unicrash.c

index 02a1b94efb4de4f4d81360b0867cac8442a83d0a..96e20114c48492a838a8284da4bd4836c2656841 100644 (file)
@@ -145,6 +145,31 @@ is_utf8_locale(void)
        return answer;
 }
 
+/*
+ * Remove control/formatting characters from this string and return its new
+ * length.  UChar32 is required for U16_NEXT, despite the name.
+ */
+static int32_t
+remove_ignorable(
+       UChar           *ustr,
+       int32_t         ustrlen)
+{
+       UChar32         uchr;
+       int32_t         src, dest;
+
+       for (src = 0, dest = 0; src < ustrlen; dest = src) {
+               U16_NEXT(ustr, src, ustrlen, uchr);
+               if (!u_isIDIgnorable(uchr))
+                       continue;
+               memmove(&ustr[dest], &ustr[src],
+                               (ustrlen - src + 1) * sizeof(UChar));
+               ustrlen -= (src - dest);
+               src = dest;
+       }
+
+       return dest;
+}
+
 /*
  * Generate normalized form and skeleton of the name.  If this fails, just
  * forget everything and return false; this is an advisory checker.
@@ -160,9 +185,6 @@ name_entry_compute_checknames(
        int32_t                 normstrlen;
        int32_t                 unistrlen;
        int32_t                 skelstrlen;
-       UChar32                 uchr;
-       int32_t                 i, j;
-
        UErrorCode              uerr = U_ZERO_ERROR;
 
        /* Convert bytestr to unistr for normalization */
@@ -206,16 +228,7 @@ name_entry_compute_checknames(
        if (U_FAILURE(uerr))
                goto out_skelstr;
 
-       /* Remove control/formatting characters from skeleton. */
-       for (i = 0, j = 0; i < skelstrlen; j = i) {
-               U16_NEXT_UNSAFE(skelstr, i, uchr);
-               if (!u_isIDIgnorable(uchr))
-                       continue;
-               memmove(&skelstr[j], &skelstr[i],
-                               (skelstrlen - i + 1) * sizeof(UChar));
-               skelstrlen -= (i - j);
-               i = j;
-       }
+       skelstrlen = remove_ignorable(skelstr, skelstrlen);
 
        entry->skelstr = skelstr;
        entry->skelstrlen = skelstrlen;