string-array: New functions for comparing string arrays.
authorBen Pfaff <blp@cs.stanford.edu>
Mon, 6 Dec 2021 05:20:42 +0000 (21:20 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Mon, 6 Dec 2021 17:05:31 +0000 (09:05 -0800)
src/libpspp/i18n.c
src/libpspp/i18n.h
src/libpspp/string-array.c
src/libpspp/string-array.h

index 4e04d32c25f39564af7fa7acc451389acaf4eebe..3faadcbb87c770395f5ce0c86fdef509df50c612 100644 (file)
@@ -880,7 +880,15 @@ utf8_hash_case_bytes (const char *s, size_t n, unsigned int basis)
 unsigned int
 utf8_hash_case_string (const char *s, unsigned int basis)
 {
-  return utf8_hash_case_bytes (s, strlen (s), basis);
+  return utf8_hash_case_substring (ss_cstr (s), basis);
+}
+
+/* Returns a hash value for UTF-8 string S, with lowercase and uppercase
+   letters treated as equal, starting from BASIS. */
+unsigned int
+utf8_hash_case_substring (struct substring s, unsigned int basis)
+{
+  return utf8_hash_case_bytes (s.string, s.length, basis);
 }
 
 /* Compares UTF-8 strings A and B case-insensitively.
@@ -888,7 +896,13 @@ utf8_hash_case_string (const char *s, unsigned int basis)
 int
 utf8_strcasecmp (const char *a, const char *b)
 {
-  return utf8_strncasecmp (a, strlen (a), b, strlen (b));
+  return utf8_sscasecmp (ss_cstr (a), ss_cstr (b));
+}
+
+int
+utf8_sscasecmp (struct substring a, struct substring b)
+{
+  return utf8_strncasecmp (a.string, a.length, b.string, b.length);
 }
 
 /* Compares UTF-8 strings A (with length AN) and B (with length BN)
index 3ae1e9e0b10456c679955ea999bcb64c39834c35..d41ef1ef2c80046f15d84404db542a038e072c5a 100644 (file)
@@ -18,6 +18,7 @@
 #define I18N_H
 
 #include "libpspp/compiler.h"
+#include "libpspp/str.h"
 #include <stdbool.h>
 #include <unistr.h>
 
@@ -77,7 +78,10 @@ const char *uc_name (ucs4_t uc, char buffer[16]);
 
 unsigned int utf8_hash_case_bytes (const char *, size_t n, unsigned int basis) WARN_UNUSED_RESULT;
 unsigned int utf8_hash_case_string (const char *, unsigned int basis) WARN_UNUSED_RESULT;
+unsigned int utf8_hash_case_substring (struct substring, unsigned int basis)
+  WARN_UNUSED_RESULT;
 int utf8_strcasecmp (const char *, const char *);
+int utf8_sscasecmp (struct substring, struct substring);
 int utf8_strncasecmp (const char *, size_t, const char *, size_t);
 int utf8_strverscasecmp (const char *, const char *);
 char *utf8_to_upper (const char *);
index d4badf4638df124681e964f78286a9cb3fda02bc..d162593b2cfb4102dd30a959cb1fc306276f0241 100644 (file)
@@ -23,6 +23,7 @@
 #include <string.h>
 
 #include "libpspp/array.h"
+#include "libpspp/i18n.h"
 #include "libpspp/str.h"
 
 #include "gl/xalloc.h"
@@ -253,6 +254,36 @@ string_array_uniq (struct string_array *sa)
   sa->n = n;
 }
 
+/* Returns true if A and B contain the same strings in the same order,
+   false otherwise. */
+bool
+string_array_equal (const struct string_array *a,
+                    const struct string_array *b)
+{
+  if (a->n != b->n)
+    return false;
+
+  for (size_t i = 0; i < a->n; i++)
+    if (strcmp (a->strings[i], b->strings[i]))
+      return false;
+  return true;
+}
+
+/* Returns true if A and B contain the same strings in the same order,
+   false otherwise. */
+bool
+string_array_equal_case (const struct string_array *a,
+                         const struct string_array *b)
+{
+  if (a->n != b->n)
+    return false;
+
+  for (size_t i = 0; i < a->n; i++)
+    if (utf8_strcasecmp (a->strings[i], b->strings[i]))
+      return false;
+  return true;
+}
+
 /* Divides STRING into tokens at DELIMITERS and adds each token to SA. */
 void
 string_array_parse (struct string_array *sa, struct substring string,
index 353ddf5018b8301a9b1e45749fab9b15fb55644a..b4a6989d6deb47a83ae6a594aefba914c26b9dfe 100644 (file)
@@ -63,6 +63,11 @@ void string_array_shrink (struct string_array *);
 void string_array_sort (struct string_array *);
 void string_array_uniq (struct string_array *);
 
+bool string_array_equal (const struct string_array *,
+                         const struct string_array *);
+bool string_array_equal_case (const struct string_array *,
+                              const struct string_array *);
+
 void string_array_parse (struct string_array *, struct substring string,
                          struct substring delimiters);
 char *string_array_join (const struct string_array *, const char *separator);