[pacman-dev] [PATCH v2 1/7] move strtim to util-common

Silvan Jegen s.jegen at gmail.com
Fri Sep 18 17:17:12 UTC 2015


Hi

There is a typo in the commit message.

[pacman-dev] [PATCH v2 1/7] move strtim to util-common

vs.

[pacman-dev] [PATCH v2 1/7] move strt*r*im to util-common


one other thing below.

On Mon, Sep 14, 2015 at 06:37:38PM -0400, Andrew Gregory wrote:
> Signed-off-by: Andrew Gregory <andrew.gregory.8 at gmail.com>
> ---
>  src/common/util-common.c | 39 +++++++++++++++++++++++++++++++++++++++
>  src/common/util-common.h |  2 ++
>  src/pacman/ini.c         |  2 +-
>  src/pacman/util.c        | 39 ---------------------------------------
>  src/pacman/util.h        |  1 -
>  src/util/pactree.c       | 36 ------------------------------------
>  6 files changed, 42 insertions(+), 77 deletions(-)
> 
> diff --git a/src/common/util-common.c b/src/common/util-common.c
> index e834168..542dcfd 100644
> --- a/src/common/util-common.c
> +++ b/src/common/util-common.c
> @@ -17,6 +17,7 @@
>   *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
>   */
>  
> +#include <ctype.h>
>  #include <errno.h>
>  #include <stdlib.h>
>  #include <string.h>
> @@ -127,6 +128,44 @@ char *safe_fgets(char *s, int size, FILE *stream)
>  	return ret;
>  }
>  
> +/* Trim whitespace and newlines from a string
> + */
> +size_t strtrim(char *str)
> +{
> +	char *end, *pch = str;
> +
> +	if(str == NULL || *str == '\0') {
> +		/* string is empty, so we're done. */
> +		return 0;
> +	}
> +
> +	while(isspace((unsigned char)*pch)) {
> +		pch++;
> +	}
> +	if(pch != str) {
> +		size_t len = strlen(pch);
> +		if(len) {
> +			memmove(str, pch, len + 1);
> +			pch = str;
> +		} else {
> +			*str = '\0';
> +		}
> +	}

The last part could be simplified as follows.

[...]
if(pch != str) {
	size_t len = strlen(pch);
	if(!len) {
		/* check if there wasn't anything but whitespace in the string. */
		*str = '\0';
		return 0;
	}
	memmove(str, pch, len + 1);
	pch = str;
}

and these three lines could then be removed...

> +	if(*str == '\0') {
> +		return 0;
> +	}
> [...]

...because we already checked for (*str == '\0') in the empty string
case further up in the function.


Cheers,

Silvan


More information about the pacman-dev mailing list