Skip to content

Commit

Permalink
utils: Don't strip modifiers when stripping encoding
Browse files Browse the repository at this point in the history
We have some locales which are dupes if we do this, for example
ca_ES.UTF-8 and ca_ES.UTF-8@valencia. The latter of these should become
ca_ES@valencia in the output. That is what `locale -a` shows.

Previously `as_locale_strip_encoding ()` modified the passed-in string
in place. However, in the one place where don't `g_strdup ()` the string
before passing to this function, it is the key in a `GHashTable`. We
can't do this, and only get away with it because the hash table isn't
touched after this call. Fix the function to instead return a newly
allocated string, and drop the `g_strdup` calls from the other call
sites.

Add a small test for this function too.

ximion/appstream-generator#92
  • Loading branch information
Iain Lane committed Jun 7, 2021
1 parent b6c9439 commit 0ca74e1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/as-context.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ as_context_localized_ht_set (AsContext *ctx, GHashTable *lht, const gchar *value
if (selected_locale == NULL)
selected_locale = "C";

locale_noenc = as_locale_strip_encoding (g_strdup (selected_locale));
locale_noenc = as_locale_strip_encoding (selected_locale);
g_hash_table_insert (lht,
g_ref_string_new_intern (locale_noenc),
g_strdup (value));
Expand Down
5 changes: 4 additions & 1 deletion src/as-utils-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ AS_INTERNAL_VISIBLE
gboolean as_copy_file (const gchar *source, const gchar *destination, GError **error);

gboolean as_is_cruft_locale (const gchar *locale);
gchar *as_locale_strip_encoding (gchar *locale);

AS_INTERNAL_VISIBLE
gchar *as_locale_strip_encoding (const gchar *locale);

gchar *as_utils_locale_to_language (const gchar *locale);

gchar *as_get_current_arch (void);
Expand Down
10 changes: 3 additions & 7 deletions src/as-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -776,16 +776,12 @@ as_is_cruft_locale (const gchar *locale)
* as_locale_strip_encoding:
*
* Remove the encoding from a locale string.
* The function modifies the string directly.
* The function returns a newly allocated string.
*/
gchar*
as_locale_strip_encoding (gchar *locale)
as_locale_strip_encoding (const gchar *locale)
{
gchar *tmp;
tmp = g_strstr_len (locale, -1, ".UTF-8");
if (tmp != NULL)
*tmp = '\0';
return locale;
return as_str_replace (locale, ".UTF-8", "");
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/as-yaml.c
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ as_yaml_set_localized_table (AsContext *ctx, GNode *node, GHashTable *l10n_table
for (GNode *n = node->children; n != NULL; n = n->next) {
const gchar *locale = as_yaml_get_node_locale (ctx, n);
if (locale != NULL) {
g_autofree gchar *locale_noenc = as_locale_strip_encoding (g_strdup (locale));
g_autofree gchar *locale_noenc = as_locale_strip_encoding (locale);
g_hash_table_insert (l10n_table,
g_ref_string_new_intern (locale_noenc),
g_strdup (as_yaml_node_get_value (n)));
Expand Down Expand Up @@ -641,6 +641,7 @@ as_yaml_emit_sequence_from_str_array (yaml_emitter_t *emitter, const gchar *key,
static void
as_yaml_localized_list_helper (gchar *key, gchar **strv, yaml_emitter_t *emitter)
{
g_autofree gchar *locale_noenc;
guint i;
if (strv == NULL)
return;
Expand All @@ -649,7 +650,8 @@ as_yaml_localized_list_helper (gchar *key, gchar **strv, yaml_emitter_t *emitter
if (as_is_cruft_locale (key))
return;

as_yaml_emit_scalar (emitter, as_locale_strip_encoding (key));
locale_noenc = as_locale_strip_encoding (key);
as_yaml_emit_scalar (emitter, locale_noenc);
as_yaml_sequence_start (emitter);
for (i = 0; strv[i] != NULL; i++) {
as_yaml_emit_scalar (emitter, strv[i]);
Expand Down
18 changes: 18 additions & 0 deletions tests/test-misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <glib.h>
#include "appstream.h"
#include "as-news-convert.h"
#include "as-utils-private.h"

#include "as-test-utils.h"

Expand Down Expand Up @@ -192,6 +193,22 @@ test_readwrite_text_news ()
g_free (tmp);
}

static void
test_locale_strip_encoding ()
{
g_autofree gchar *c = NULL;
g_autofree gchar *cutf8 = NULL;
g_autofree gchar *cutf8valencia = NULL;

c = as_locale_strip_encoding ("C");
cutf8 = as_locale_strip_encoding ("C.UTF-8");
cutf8valencia = as_locale_strip_encoding ("C.UTF-8@valencia");

g_assert_cmpstr (c, ==, "C");
g_assert_cmpstr (cutf8, ==, "C");
g_assert_cmpstr (cutf8valencia, ==, "C@valencia");
}

int
main (int argc, char **argv)
{
Expand All @@ -214,6 +231,7 @@ main (int argc, char **argv)

g_test_add_func ("/AppStream/Misc/YAMLNews", test_readwrite_yaml_news);
g_test_add_func ("/AppStream/Misc/TextNews", test_readwrite_text_news);
g_test_add_func ("/AppStream/Misc/StripLocaleEncoding", test_locale_strip_encoding);

ret = g_test_run ();
g_free (datadir);
Expand Down

0 comments on commit 0ca74e1

Please sign in to comment.