Factorize integer argument parsing

Add util functions for integer parsing (with tests), and factorize
integer argument parsing to avoid code duplication.
This commit is contained in:
Romain Vimont
2019-12-07 11:01:55 +01:00
parent 64bcac9157
commit 61274a7cdb
4 changed files with 189 additions and 88 deletions

View File

@@ -1,6 +1,7 @@
#ifndef STRUTIL_H
#define STRUTIL_H
#include <stdbool.h>
#include <stddef.h>
#include "config.h"
@@ -25,6 +26,18 @@ xstrjoin(char *dst, const char *const tokens[], char sep, size_t n);
char *
strquote(const char *src);
// parse s as an integer into value
// returns true if the conversion succeeded, false otherwise
bool
parse_integer(const char *s, long *out);
// parse s as an integer into value
// like parse_integer(), but accept 'k'/'K' (x1000) and 'm'/'M' (x1000000) as
// suffix
// returns true if the conversion succeeded, false otherwise
bool
parse_integer_with_suffix(const char *s, long *out);
// return the index to truncate a UTF-8 string at a valid position
size_t
utf8_truncation_index(const char *utf8, size_t max_len);