mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-03-22 19:40:41 +01:00
shared: add nm_strstrip_avoid_copy()
This commit is contained in:
parent
637c6cb333
commit
6a3005ea72
1 changed files with 27 additions and 0 deletions
|
|
@ -858,6 +858,33 @@ nm_strstrip (char *str)
|
|||
return str ? g_strstrip (str) : NULL;
|
||||
}
|
||||
|
||||
static inline const char *
|
||||
nm_strstrip_avoid_copy (const char *str, char **str_free)
|
||||
{
|
||||
gsize l;
|
||||
char *s;
|
||||
|
||||
nm_assert (str_free && !*str_free);
|
||||
|
||||
if (!str)
|
||||
return NULL;
|
||||
|
||||
str = nm_str_skip_leading_spaces (str);
|
||||
l = strlen (str);
|
||||
if ( l == 0
|
||||
|| !g_ascii_isspace (str[l - 1]))
|
||||
return str;
|
||||
while ( l > 0
|
||||
&& g_ascii_isspace (str[l - 1]))
|
||||
l--;
|
||||
|
||||
s = g_new (char, l + 1);
|
||||
memcpy (s, str, l);
|
||||
s[l] = '\0';
|
||||
*str_free = s;
|
||||
return s;
|
||||
}
|
||||
|
||||
/* g_ptr_array_sort()'s compare function takes pointers to the
|
||||
* value. Thus, you cannot use strcmp directly. You can use
|
||||
* nm_strcmp_p().
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue