Commit 2a0ac05e authored by Niels De Graef's avatar Niels De Graef

persona: Don't use string.replace

Although it's supposedly part of `glib-2.0.vapi`, `string.replace` is
_not_ a function implemented by GLib. Worse, what it does is to create
and compile a `GRegex`, which then gets used to do the substitution.

Since we call `Persona.build_uid()` for each persona, and then even 3
times in a row, we create an amount of unnecessary temporary
allocations that linearly increases with the amount of contacts.

To mitigate this, use a `GString` (aka `GLib.StringBuilder`) and try to
allocate the right amount from the start.
parent 6d443480
......@@ -283,11 +283,18 @@ public abstract class Folks.Persona : Object
assert_not_reached ();
}
private static string _escape_uid_component (string component)
private static void _add_escaped_uid_component (StringBuilder uid, string component)
{
/* Escape colons with backslashes */
string escaped = component.replace ("\\", "\\\\");
return escaped.replace (":", "\\:");
for (int i = 0; i < component.length; i++)
{
char c = component[i];
if (c == ':' || c == '\\')
{
uid.append_c ('\\');
}
uid.append_c (c);
}
}
private static string _unescape_uid_component (string component)
......@@ -316,9 +323,20 @@ public abstract class Folks.Persona : Object
requires (persona_store_id != "")
requires (persona_id != "")
{
return "%s:%s:%s".printf (Persona._escape_uid_component (backend_name),
Persona._escape_uid_component (persona_store_id),
Persona._escape_uid_component (persona_id));
long min_total_length = backend_name.length
+ persona_store_id.length
+ persona_id.length
+ 2 // 2 colons
+ 1; // terminator
StringBuilder uid = new StringBuilder.sized (min_total_length);
Persona._add_escaped_uid_component (uid, backend_name);
uid.append_c (':');
Persona._add_escaped_uid_component (uid, persona_store_id);
uid.append_c (':');
Persona._add_escaped_uid_component (uid, persona_id);
return (owned) uid.str;
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment