Re: [PATCH 1/5] service: Add function to remove empty strings
Hi Jaakko, Make the whole change it one loop. No need to check if there is, empty strings, at first. It's greedy and, if there 1+ empty lines, you will anyway loop all over again. Tomasz This helper function takes in a heap-allocated buffer of heap-allocated strings. If no strings should be removed, return the buffer. Else, free all empty strings, place nonempty strings to a new container and return it, freeing the old container. --- src/service.c | 32 1 file changed, 32 insertions(+) diff --git a/src/service.c b/src/service.c index 2d8245e..1723586 100644 --- a/src/service.c +++ b/src/service.c @@ -2926,6 +2926,38 @@ static DBusMessage *get_properties(DBusConnection *conn, return reply; } +static char **remove_empty_strings(char **strv) +{ + int amount, length, index; + char **iter, **out; + + amount = 0; + length = g_strv_length(strv); + iter = strv; + + while (*iter) + if (strlen(*iter++)) + amount++; + + if (amount == length - 1) + return strv; + + out = g_new0(char *, amount+1); + index = 0; + iter = strv; + + while (*iter) { + if (strlen(*iter)) + out[index++] = *iter; + else + g_free(*iter); + iter++; + } + + g_free(strv); + return out; +} + static int update_proxy_configuration(struct connman_service *service, DBusMessageIter *array) { ___ connman mailing list connman@connman.net https://lists.connman.net/mailman/listinfo/connman
Re: [PATCH 1/5] service: Add function to remove empty strings
Hi Jaakko, On ke, 2015-07-15 at 11:35 +0300, Jaakko Hannikainen wrote: This helper function takes in a heap-allocated buffer of heap-allocated strings. If no strings should be removed, return the buffer. Else, free all empty strings, place nonempty strings to a new container and return it, freeing the old container. --- src/service.c | 32 1 file changed, 32 insertions(+) diff --git a/src/service.c b/src/service.c index 2d8245e..1723586 100644 --- a/src/service.c +++ b/src/service.c @@ -2926,6 +2926,38 @@ static DBusMessage *get_properties(DBusConnection *conn, return reply; } +static char **remove_empty_strings(char **strv) +{ +int amount, length, index; +char **iter, **out; + +amount = 0; +length = g_strv_length(strv); We could remove the call to g_strv_length() and calculate the max length in the while loop below. +iter = strv; + +while (*iter) +if (strlen(*iter++)) +amount++; And it's unnecessary to calculate the length of each string. Twice. It would suffice to just check the first byte. Regards, -Slava ___ connman mailing list connman@connman.net https://lists.connman.net/mailman/listinfo/connman
[PATCH 1/5] service: Add function to remove empty strings
This helper function takes in a heap-allocated buffer of heap-allocated strings. If no strings should be removed, return the buffer. Else, free all empty strings, place nonempty strings to a new container and return it, freeing the old container. --- src/service.c | 32 1 file changed, 32 insertions(+) diff --git a/src/service.c b/src/service.c index 2d8245e..1723586 100644 --- a/src/service.c +++ b/src/service.c @@ -2926,6 +2926,38 @@ static DBusMessage *get_properties(DBusConnection *conn, return reply; } +static char **remove_empty_strings(char **strv) +{ + int amount, length, index; + char **iter, **out; + + amount = 0; + length = g_strv_length(strv); + iter = strv; + + while (*iter) + if (strlen(*iter++)) + amount++; + + if (amount == length - 1) + return strv; + + out = g_new0(char *, amount+1); + index = 0; + iter = strv; + + while (*iter) { + if (strlen(*iter)) + out[index++] = *iter; + else + g_free(*iter); + iter++; + } + + g_free(strv); + return out; +} + static int update_proxy_configuration(struct connman_service *service, DBusMessageIter *array) { -- 2.1.0 ___ connman mailing list connman@connman.net https://lists.connman.net/mailman/listinfo/connman
Re: [PATCH 1/5] service: Add function to remove empty strings
Hi Jaakko, On ke, 2015-07-15 at 11:35 +0300, Jaakko Hannikainen wrote: This helper function takes in a heap-allocated buffer of heap-allocated strings. If no strings should be removed, return the buffer. Else, free all empty strings, place nonempty strings to a new container and return it, freeing the old container. --- src/service.c | 32 1 file changed, 32 insertions(+) diff --git a/src/service.c b/src/service.c index 2d8245e..1723586 100644 --- a/src/service.c +++ b/src/service.c @@ -2926,6 +2926,38 @@ static DBusMessage *get_properties(DBusConnection *conn, return reply; } +static char **remove_empty_strings(char **strv) +{ + int amount, length, index; + char **iter, **out; + + amount = 0; + length = g_strv_length(strv); We could remove the call to g_strv_length() and calculate the max length in the while loop below. + iter = strv; + + while (*iter) + if (strlen(*iter++)) + amount++; + + if (amount == length - 1) + return strv; + + out = g_new0(char *, amount+1); + index = 0; + iter = strv; + + while (*iter) { + if (strlen(*iter)) + out[index++] = *iter; + else + g_free(*iter); + iter++; + } + + g_free(strv); + return out; +} + static int update_proxy_configuration(struct connman_service *service, DBusMessageIter *array) { Cheers, Jukka ___ connman mailing list connman@connman.net https://lists.connman.net/mailman/listinfo/connman