diff mbox

[1/2] kconfig: add chomp like helper function

Message ID 1458125619-28093-1-git-send-email-pebolle@tiscali.nl (mailing list archive)
State New, archived
Headers show

Commit Message

Paul Bolle March 16, 2016, 10:53 a.m. UTC
Add a helper function that strips trailing new lines and carriage
returns from strings. Call it chomp, after the perl function that
inspired it.

Signed-off-by: Paul Bolle <pebolle@tiscali.nl>
---
 scripts/kconfig/confdata.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

Comments

Michal Marek March 16, 2016, 11:42 a.m. UTC | #1
On 2016-03-16 11:53, Paul Bolle wrote:
> Add a helper function that strips trailing new lines and carriage
> returns from strings. Call it chomp, after the perl function that
> inspired it.
> 
> Signed-off-by: Paul Bolle <pebolle@tiscali.nl>
> ---
>  scripts/kconfig/confdata.c | 22 ++++++++++++++++++++++
>  1 file changed, 22 insertions(+)
> 
> diff --git a/scripts/kconfig/confdata.c b/scripts/kconfig/confdata.c
> index 0b7dc2fd7bac..51904c423411 100644
> --- a/scripts/kconfig/confdata.c
> +++ b/scripts/kconfig/confdata.c
> @@ -248,6 +248,28 @@ e_out:
>  	return -1;
>  }
>  
> +/*
> + * Return newly allocated copy of string "in" with all trailing new lines and
> + * carriage returns removed.
> + */
> +static char *chomp(char *in)
> +{
> +	size_t last = strlen(in);
> +	char *copy;
> +
> +	copy = malloc(last + 1);
> +	if (!copy)
> +		return NULL;
> +
> +	strcpy(copy, in);
> +	if (last)
> +		last--;
> +	while (last  && (copy[last] == '\r' || copy[last] == '\n'))
> +		copy[last--] = '\0';
> +
> +	return copy;
> +}
> +

For this particular use, it's probably easier to just write

conf_warning("unexpected data: %.*s",
	     (int)strcspn(line, "\r\n"), line);

Or do you see more use cases for the chomp function?

No matter how the string is constructed, I like the verbose warning :)

Thanks,
Michal
--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Paul Bolle March 16, 2016, 12:41 p.m. UTC | #2
On wo, 2016-03-16 at 12:42 +0100, Michal Marek wrote:
> For this particular use, it's probably easier to just write
> 
> conf_warning("unexpected data: %.*s",
> 	     (int)strcspn(line, "\r\n"), line);

OK. (Next time I'll try listen to the voice in my head whispering:
"There must be an easier way to do this.".)

> Or do you see more use cases for the chomp function?

No.

> No matter how the string is constructed, I like the verbose warning :)

I'll respin and resend (in a few hours).

Thanks,


Paul Bolle
--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/scripts/kconfig/confdata.c b/scripts/kconfig/confdata.c
index 0b7dc2fd7bac..51904c423411 100644
--- a/scripts/kconfig/confdata.c
+++ b/scripts/kconfig/confdata.c
@@ -248,6 +248,28 @@  e_out:
 	return -1;
 }
 
+/*
+ * Return newly allocated copy of string "in" with all trailing new lines and
+ * carriage returns removed.
+ */
+static char *chomp(char *in)
+{
+	size_t last = strlen(in);
+	char *copy;
+
+	copy = malloc(last + 1);
+	if (!copy)
+		return NULL;
+
+	strcpy(copy, in);
+	if (last)
+		last--;
+	while (last  && (copy[last] == '\r' || copy[last] == '\n'))
+		copy[last--] = '\0';
+
+	return copy;
+}
+
 int conf_read_simple(const char *name, int def)
 {
 	FILE *in = NULL;