Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: Don't strip modifiers when stripping encoding #322

Merged
merged 2 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compose/asc-font.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,12 +423,12 @@ asc_font_get_id (AscFont *font)
return priv->id;

tmp = g_utf8_strdown (asc_font_get_family (font), -1);
tmp_family = as_str_replace (tmp, " ", "");
tmp_family = as_str_replace (tmp, " ", "", 0);
as_strstripnl (tmp_family);
g_free (tmp);

tmp = g_utf8_strdown (asc_font_get_style (font), -1);
tmp_style = as_str_replace (tmp, " ", "");
tmp_style = as_str_replace (tmp, " ", "", 0);
as_strstripnl (tmp_style);
g_free (tmp);

Expand Down
2 changes: 1 addition & 1 deletion src/as-news-convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ as_news_text_to_releases (const gchar *data, GError **error)

/* try to unsplit lines */
data_str = g_string_new (data);
as_gstring_replace (data_str, "\n ", " ");
as_gstring_replace (data_str, "\n ", " ", 0);

/* break up into sections */
desc = g_string_new ("");
Expand Down
8 changes: 4 additions & 4 deletions src/as-spdx.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ static GString*
as_utils_spdx_license_3to2 (const gchar *license3)
{
GString *license2 = g_string_new (license3);
as_gstring_replace (license2, "-only", "");
as_gstring_replace (license2, "-or-later", "+");
as_gstring_replace (license2, "-only", "", 1);
as_gstring_replace (license2, "-or-later", "+", 1);
return license2;
}

Expand All @@ -302,8 +302,8 @@ static GString*
as_utils_spdx_license_2to3 (const gchar *license2)
{
GString *license3 = g_string_new (license2);
as_gstring_replace (license3, ".0+", ".0-or-later");
as_gstring_replace (license3, ".1+", ".1-or-later");
as_gstring_replace (license3, ".0+", ".0-or-later", 1);
as_gstring_replace (license3, ".1+", ".1-or-later", 1);
return license3;
}

Expand Down
3 changes: 2 additions & 1 deletion src/as-utils-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ gboolean as_utils_is_writable (const gchar *path);
AS_INTERNAL_VISIBLE
gchar *as_str_replace (const gchar *str,
const gchar *old_str,
const gchar *new_str);
const gchar *new_str,
guint limit);

gchar **as_ptr_array_to_strv (GPtrArray *array);
const gchar *as_ptr_array_find_string (GPtrArray *array,
Expand Down
99 changes: 45 additions & 54 deletions src/as-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -593,85 +593,76 @@ as_ptr_array_to_strv (GPtrArray *array)
}

/**
* as_gstring_replace:
* @string: The #GString to operate on
* @search: The text to search for
* @replace: The text to use for substitutions
*
* Performs multiple search and replace operations on the given string.
*
* Returns: the number of replacements done, or 0 if @search is not found.
* g_string_replace:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a typo (c+p from glib)

* @string: a #GString
* @find: the string to find in @string
* @replace: the string to insert in place of @find
* @limit: the maximum instances of @find to replace with @replace, or `0` for
* no limit
*
* Replaces the string @find with the string @replace in a #GString up to
* @limit times. If the number of instances of @find in the #GString is
* less than @limit, all instances are replaced. If @limit is `0`,
* all instances of @find are replaced.
*
* Returns: the number of find and replace operations performed.
**/
guint
as_gstring_replace (GString *string, const gchar *search, const gchar *replace)
as_gstring_replace (GString *string, const gchar *find, const gchar *replace, guint limit)
{
gchar *tmp;
guint count = 0;
gsize search_idx = 0;
gsize replace_len;
gsize search_len;
#if GLIB_CHECK_VERSION(2,68,0)
return g_string_replace (string, find, replace, limit);
#else
/* note: This is a direct copy from GLib upstream (with whitespace
* fixed spaces to tabs and with style fixed). Once we can depend on
* GLib 2.68, this copy should be dropped and g_string_replace() used
* instead.
*
* GLib is licensed under the LGPL-2.1+.
iainlane marked this conversation as resolved.
Show resolved Hide resolved
*/
gsize f_len, r_len, pos;
gchar *cur, *next;
guint n = 0;

g_return_val_if_fail (string != NULL, 0);
g_return_val_if_fail (search != NULL, 0);
g_return_val_if_fail (find != NULL, 0);
g_return_val_if_fail (replace != NULL, 0);

/* nothing to do */
if (string->len == 0)
return 0;

search_len = strlen (search);
replace_len = strlen (replace);
f_len = strlen (find);
r_len = strlen (replace);
cur = string->str;

do {
tmp = g_strstr_len (string->str + search_idx, -1, search);
if (tmp == NULL)
break;

/* advance the counter in case @replace contains @search */
search_idx = (gsize) (tmp - string->str);

/* reallocate the string if required */
if (search_len > replace_len) {
g_string_erase (string,
(gssize) search_idx,
(gssize) (search_len - replace_len));
memcpy (tmp, replace, replace_len);
} else if (search_len < replace_len) {
g_string_insert_len (string,
(gssize) search_idx,
replace,
(gssize) (replace_len - search_len));
/* we have to treat this specially as it could have
* been reallocated when the insertion happened */
memcpy (string->str + search_idx, replace, replace_len);
} else {
/* just memcmp in the new string */
memcpy (tmp, replace, replace_len);
}
search_idx += replace_len;
count++;
} while (TRUE);
while ((next = strstr (cur, find)) != NULL) {
pos = next - string->str;
g_string_erase (string, pos, f_len);
g_string_insert (string, pos, replace);
cur = string->str + pos + r_len;
n++;
}

return count;
return n;
#endif /* !GLIB_CHECK_VERSION(2,68.0) */
}

/**
* as_str_replace:
* @str: The string to operate on
* @old_str: The old value to replace.
* @new_str: The new value to replace @old_str with.
* @limit: the maximum instances of @find to replace with @new_str, or `0` for
* no limit
*
* Performs search & replace on the given string.
*
* Returns: A new string with the characters replaced.
*/
gchar*
as_str_replace (const gchar *str, const gchar *old_str, const gchar *new_str)
as_str_replace (const gchar *str, const gchar *old_str, const gchar *new_str, guint limit)
{
GString *gstr;

gstr = g_string_new (str);
as_gstring_replace (gstr, old_str, new_str);
as_gstring_replace (gstr, old_str, new_str, limit);
return g_string_free (gstr, FALSE);
}

Expand Down Expand Up @@ -781,7 +772,7 @@ as_is_cruft_locale (const gchar *locale)
gchar*
as_locale_strip_encoding (const gchar *locale)
{
return as_str_replace (locale, ".UTF-8", "");
return as_str_replace (locale, ".UTF-8", "", 1);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/as-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ guint as_utils_data_id_hash (const gchar *data_id);

guint as_gstring_replace (GString *string,
const gchar *search,
const gchar *replace);
const gchar *replace,
guint limit);
ximion marked this conversation as resolved.
Show resolved Hide resolved

gboolean as_utils_is_platform_triplet (const gchar *triplet);

Expand Down
2 changes: 1 addition & 1 deletion tools/ascli-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ ascli_format_long_output (const gchar *str, guint line_length, guint indent_leve
if (indent_level > 0) {
g_autofree gchar *spacing = g_strnfill (indent_level, ' ');
g_autofree gchar *spacing_nl = g_strconcat ("\n", spacing, NULL);
as_gstring_replace (res, "\n", spacing_nl);
as_gstring_replace (res, "\n", spacing_nl, 0);
g_string_prepend (res, spacing);
}

Expand Down