summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2017-10-06 15:27:21 +0200
committerThomas Haller <thaller@redhat.com>2017-10-09 22:05:35 +0200
commit6a3005ea72ecf9a5df7386c4e106e5816c836980 (patch)
tree41efa6aa9fe41d24318c531db925308d12c966f3
parent637c6cb3339d645c3123b9d4c31a8f614202188c (diff)
downloadNetworkManager-6a3005ea72ecf9a5df7386c4e106e5816c836980.tar.gz
shared: add nm_strstrip_avoid_copy()
-rw-r--r--shared/nm-utils/nm-macros-internal.h27
1 files changed, 27 insertions, 0 deletions
diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h
index b946a276ca..f511c142b8 100644
--- a/shared/nm-utils/nm-macros-internal.h
+++ b/shared/nm-utils/nm-macros-internal.h
@@ -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().