Re: [RFC 1/9] lib/string: Add function to trim duplicate WS

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Dec 23, 2019 at 04:55:50PM -0600, Tony Asleson wrote:
> +/**
> + * Removes leading and trailing whitespace and removes duplicate
> + * adjacent whitespace in a string, modifies string in place.
> + * @s The %NUL-terminated string to have spaces removed
> + * Returns the new length
> + */

This isn't good kernel-doc.  See Documentation/doc-guide/kernel-doc.rst
Compile with W=1 to get the format checked.

> +size_t strim_dupe(char *s)
> +{
> +	size_t ret = 0;
> +	char *w = s;
> +	char *p;
> +
> +	/*
> +	 * This will remove all leading and duplicate adjacent, but leave
> +	 * 1 space at the end if one or more are present.
> +	 */
> +	for (p = s; *p != '\0'; ++p) {
> +		if (!isspace(*p) || (p != s && !isspace(*(p - 1)))) {
> +			*w = *p;
> +			++w;
> +			ret += 1;
> +		}
> +	}

I'd be tempted to do ...

	size_t ret = 0;
	char *w = s;
	bool last_space = false;

	do {
		bool this_space = isspace(*s);

		if (!this_space || !last_space) {
			*w++ = *s;
			ret++;
		}
		s++;
		last_space = this_space;
	} while (s[-1] != '\0');

> +	/* Take off the last character if it's a space too */
> +	if (ret && isspace(*(w - 1))) {
> +		ret--;
> +		*(w - 1) = '\0';
> +	}
> +	return ret;
> +}
> +EXPORT_SYMBOL(strim_dupe);
> +
>  #ifndef __HAVE_ARCH_STRLEN
>  /**
>   * strlen - Find the length of a string
> -- 
> 2.21.0
> 



[Index of Archives]     [Linux RAID]     [Linux SCSI]     [Linux ATA RAID]     [IDE]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Device Mapper]

  Powered by Linux