From patchwork Fri Feb 16 10:24:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Marchand X-Patchwork-Id: 136844 X-Patchwork-Delegate: david.marchand@redhat.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 9BBDF43B30; Fri, 16 Feb 2024 11:25:10 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 8B46442F0F; Fri, 16 Feb 2024 11:25:10 +0100 (CET) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mails.dpdk.org (Postfix) with ESMTP id 2F45D42F0A for ; Fri, 16 Feb 2024 11:25:09 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708079108; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=w+pRqIiQ9P4Q1MFUJ1C7974i1B/CSpsbbafwStOP/JA=; b=Utz/mwwcm7GoG1vFvW7Sn4zSzg9UgvwnJmmTNLJBnw3cbdaiKaVNfCVcx3E5rTi8Kp3OBx cdvnKxgrYqCm8ZcMt7ejoASLjUCdUuYxuyFjwmWKTe3ikjgkCpHbHuhNjZ+z/aYCkMWwXy F9weU3C4lMNwm5OIaBZN95R5cyV0uSA= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-634-R7zqoM4EMca6VWSrkhQTGQ-1; Fri, 16 Feb 2024 05:25:05 -0500 X-MC-Unique: R7zqoM4EMca6VWSrkhQTGQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BF27484ACA3; Fri, 16 Feb 2024 10:25:04 +0000 (UTC) Received: from dmarchan.redhat.com (unknown [10.45.225.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 93FB1AD75; Fri, 16 Feb 2024 10:25:02 +0000 (UTC) From: David Marchand To: dev@dpdk.org Cc: Tyler Retzlaff , Sunil Kumar Kori , Rakesh Kudurumalla , Jerin Jacob , Srikanth Yalavarthi , Cristian Dumitrescu , Aman Singh , Yuying Zhang , Brian Dooley , Gowrishankar Muthukrishnan Subject: [PATCH v2 1/3] eal: add helper to skip whitespaces Date: Fri, 16 Feb 2024 11:24:52 +0100 Message-ID: <20240216102454.4156609-2-david.marchand@redhat.com> In-Reply-To: <20240216102454.4156609-1-david.marchand@redhat.com> References: <1700089378-26197-1-git-send-email-roretzla@linux.microsoft.com> <20240216102454.4156609-1-david.marchand@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Reduce code duplication by providing a simple inline helper. Signed-off-by: David Marchand Acked-by: Tyler Retzlaff Acked-by: Bruce Richardson --- app/graph/utils.c | 13 ++------ app/test-eventdev/parser.c | 14 ++++----- app/test-eventdev/parser.h | 8 ----- app/test-mldev/parser.c | 17 ++++++----- app/test-mldev/parser.h | 8 ----- app/test-pmd/cmdline_tm.c | 13 ++------ app/test/test_string_fns.c | 35 ++++++++++++++++++++++ examples/fips_validation/fips_validation.c | 16 +++------- examples/ip_pipeline/parser.c | 14 ++++----- examples/ip_pipeline/parser.h | 8 ----- examples/pipeline/cli.c | 13 ++------ lib/eal/include/rte_string_fns.h | 27 +++++++++++++++++ 12 files changed, 98 insertions(+), 88 deletions(-) diff --git a/app/graph/utils.c b/app/graph/utils.c index c7b6ae83cf..3e8099ea88 100644 --- a/app/graph/utils.c +++ b/app/graph/utils.c @@ -9,17 +9,10 @@ #include #include +#include #include "module_api.h" -#define white_spaces_skip(pos) \ -({ \ - __typeof__(pos) _p = (pos); \ - for ( ; isspace(*_p); _p++) \ - ; \ - _p; \ -}) - static void hex_string_to_uint64(uint64_t *dst, const char *hexs) { @@ -47,7 +40,7 @@ parser_uint64_read(uint64_t *value, const char *p) char *next; uint64_t val; - p = white_spaces_skip(p); + p = rte_str_skip_leading_spaces(p); if (!isdigit(*p)) return -EINVAL; @@ -73,7 +66,7 @@ parser_uint64_read(uint64_t *value, const char *p) break; } - p = white_spaces_skip(p); + p = rte_str_skip_leading_spaces(p); if (*p != '\0') return -EINVAL; diff --git a/app/test-eventdev/parser.c b/app/test-eventdev/parser.c index bbea47b87d..4fb6d43a06 100644 --- a/app/test-eventdev/parser.c +++ b/app/test-eventdev/parser.c @@ -40,7 +40,7 @@ get_hex_val(char c) int parser_read_arg_bool(const char *p) { - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); int result = -EINVAL; if (((p[0] == 'y') && (p[1] == 'e') && (p[2] == 's')) || @@ -67,7 +67,7 @@ parser_read_arg_bool(const char *p) result = 0; } - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if (p[0] != '\0') return -EINVAL; @@ -81,7 +81,7 @@ parser_read_uint64(uint64_t *value, const char *p) char *next; uint64_t val; - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if (!isdigit(*p)) return -EINVAL; @@ -107,7 +107,7 @@ parser_read_uint64(uint64_t *value, const char *p) break; } - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if (*p != '\0') return -EINVAL; @@ -121,7 +121,7 @@ parser_read_int32(int32_t *value, const char *p) char *next; int32_t val; - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if (!isdigit(*p)) return -EINVAL; @@ -139,13 +139,13 @@ parser_read_uint64_hex(uint64_t *value, const char *p) char *next; uint64_t val; - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); val = strtoul(p, &next, 16); if (p == next) return -EINVAL; - p = skip_white_spaces(next); + p = rte_str_skip_leading_spaces(next); if (*p != '\0') return -EINVAL; diff --git a/app/test-eventdev/parser.h b/app/test-eventdev/parser.h index 3617872997..6976efe877 100644 --- a/app/test-eventdev/parser.h +++ b/app/test-eventdev/parser.h @@ -10,14 +10,6 @@ #define PARSE_DELIMITER " \f\n\r\t\v" -#define skip_white_spaces(pos) \ -({ \ - __typeof__(pos) _p = (pos); \ - for ( ; isspace(*_p); _p++) \ - ; \ - _p; \ -}) - static inline size_t skip_digits(const char *src) { diff --git a/app/test-mldev/parser.c b/app/test-mldev/parser.c index 277dcaedb2..ad761312fd 100644 --- a/app/test-mldev/parser.c +++ b/app/test-mldev/parser.c @@ -12,6 +12,7 @@ #include #include +#include #include "parser.h" @@ -52,7 +53,7 @@ get_hex_val(char c) int parser_read_arg_bool(const char *p) { - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); int result = -EINVAL; if (((p[0] == 'y') && (p[1] == 'e') && (p[2] == 's')) || @@ -77,7 +78,7 @@ parser_read_arg_bool(const char *p) result = 0; } - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if (p[0] != '\0') return -EINVAL; @@ -91,7 +92,7 @@ parser_read_uint64(uint64_t *value, const char *p) char *next; uint64_t val; - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if (!isdigit(*p)) return -EINVAL; @@ -117,7 +118,7 @@ parser_read_uint64(uint64_t *value, const char *p) break; } - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if (*p != '\0') return -EINVAL; @@ -131,7 +132,7 @@ parser_read_int32(int32_t *value, const char *p) char *next; int32_t val; - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if ((*p != '-') && (!isdigit(*p))) return -EINVAL; @@ -149,7 +150,7 @@ parser_read_int16(int16_t *value, const char *p) char *next; int16_t val; - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if ((*p != '-') && (!isdigit(*p))) return -EINVAL; @@ -167,13 +168,13 @@ parser_read_uint64_hex(uint64_t *value, const char *p) char *next; uint64_t val; - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); val = strtoul(p, &next, 16); if (p == next) return -EINVAL; - p = skip_white_spaces(next); + p = rte_str_skip_leading_spaces(next); if (*p != '\0') return -EINVAL; diff --git a/app/test-mldev/parser.h b/app/test-mldev/parser.h index 8b4207d0b0..b12b8f24c0 100644 --- a/app/test-mldev/parser.h +++ b/app/test-mldev/parser.h @@ -12,14 +12,6 @@ #define PARSE_DELIMITER " \f\n\r\t\v" -#define skip_white_spaces(pos) \ - ({ \ - __typeof__(pos) _p = (pos); \ - for (; isspace(*_p); _p++) \ - ; \ - _p; \ - }) - static inline size_t skip_digits(const char *src) { diff --git a/app/test-pmd/cmdline_tm.c b/app/test-pmd/cmdline_tm.c index c11c80b158..6ce074f538 100644 --- a/app/test-pmd/cmdline_tm.c +++ b/app/test-pmd/cmdline_tm.c @@ -11,6 +11,7 @@ #include #include +#include #include #include "testpmd.h" @@ -19,14 +20,6 @@ #define PARSE_DELIMITER " \f\n\r\t\v" #define MAX_NUM_SHARED_SHAPERS 256 -#define skip_white_spaces(pos) \ -({ \ - __typeof__(pos) _p = (pos); \ - for ( ; isspace(*_p); _p++) \ - ; \ - _p; \ -}) - /** Display TM Error Message */ static void print_err_msg(struct rte_tm_error *error) @@ -112,7 +105,7 @@ read_uint64(uint64_t *value, const char *p) char *next; uint64_t val; - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if (!isdigit(*p)) return -EINVAL; @@ -138,7 +131,7 @@ read_uint64(uint64_t *value, const char *p) break; } - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if (*p != '\0') return -EINVAL; diff --git a/app/test/test_string_fns.c b/app/test/test_string_fns.c index ad41106df1..3b311325dc 100644 --- a/app/test/test_string_fns.c +++ b/app/test/test_string_fns.c @@ -172,6 +172,39 @@ test_rte_strlcat(void) return 0; } +static int +test_rte_str_skip_leading_spaces(void) +{ + static const char empty[] = ""; + static const char nowhitespace[] = "Thereisreallynowhitespace"; + static const char somewhitespaces[] = " \f\n\r\t\vThere are some whitespaces"; + const char *p; + + LOG("Checking '%s'\n", empty); + p = rte_str_skip_leading_spaces(empty); + if (p != empty) { + LOG("Returned address '%s' does not match expected result\n", p); + return -1; + } + LOG("Got expected '%s'\n", p); + LOG("Checking '%s'\n", nowhitespace); + p = rte_str_skip_leading_spaces(nowhitespace); + if (p != nowhitespace) { + LOG("Returned address '%s' does not match expected result\n", p); + return -1; + } + LOG("Got expected '%s'\n", p); + LOG("Checking '%s'\n", somewhitespaces); + p = rte_str_skip_leading_spaces(somewhitespaces); + if (p != strchr(somewhitespaces, 'T')) { + LOG("Returned address '%s' does not match expected result\n", p); + return -1; + } + LOG("Got expected '%s'\n", p); + + return 0; +} + static int test_string_fns(void) { @@ -179,6 +212,8 @@ test_string_fns(void) return -1; if (test_rte_strlcat() < 0) return -1; + if (test_rte_str_skip_leading_spaces() < 0) + return -1; return 0; } diff --git a/examples/fips_validation/fips_validation.c b/examples/fips_validation/fips_validation.c index f840804009..c15178e55b 100644 --- a/examples/fips_validation/fips_validation.c +++ b/examples/fips_validation/fips_validation.c @@ -13,14 +13,6 @@ #include "fips_validation.h" -#define skip_white_spaces(pos) \ -({ \ - __typeof__(pos) _p = (pos); \ - for ( ; isspace(*_p); _p++) \ - ; \ - _p; \ -}) - static int get_file_line(void) { @@ -579,13 +571,13 @@ parser_read_uint64_hex(uint64_t *value, const char *p) char *next; uint64_t val; - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); val = strtoul(p, &next, 16); if (p == next) return -EINVAL; - p = skip_white_spaces(next); + p = rte_str_skip_leading_spaces(next); if (*p != '\0') return -EINVAL; @@ -759,7 +751,7 @@ parser_read_uint64(uint64_t *value, const char *p) char *next; uint64_t val; - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if (!isdigit(*p)) return -EINVAL; @@ -785,7 +777,7 @@ parser_read_uint64(uint64_t *value, const char *p) break; } - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if (*p != '\0') return -EINVAL; diff --git a/examples/ip_pipeline/parser.c b/examples/ip_pipeline/parser.c index dfd71a71d3..62fc52f9db 100644 --- a/examples/ip_pipeline/parser.c +++ b/examples/ip_pipeline/parser.c @@ -42,7 +42,7 @@ get_hex_val(char c) int parser_read_arg_bool(const char *p) { - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); int result = -EINVAL; if (((p[0] == 'y') && (p[1] == 'e') && (p[2] == 's')) || @@ -69,7 +69,7 @@ parser_read_arg_bool(const char *p) result = 0; } - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if (p[0] != '\0') return -EINVAL; @@ -83,7 +83,7 @@ parser_read_uint64(uint64_t *value, const char *p) char *next; uint64_t val; - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if (!isdigit(*p)) return -EINVAL; @@ -109,7 +109,7 @@ parser_read_uint64(uint64_t *value, const char *p) break; } - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if (*p != '\0') return -EINVAL; @@ -123,13 +123,13 @@ parser_read_uint64_hex(uint64_t *value, const char *p) char *next; uint64_t val; - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); val = strtoul(p, &next, 16); if (p == next) return -EINVAL; - p = skip_white_spaces(next); + p = rte_str_skip_leading_spaces(next); if (*p != '\0') return -EINVAL; @@ -420,7 +420,7 @@ parse_cpu_core(const char *entry, uint32_t s = 0, c = 0, h = 0, val; uint8_t s_parsed = 0, c_parsed = 0, h_parsed = 0; - const char *next = skip_white_spaces(entry); + const char *next = rte_str_skip_leading_spaces(entry); char type; if (p == NULL) diff --git a/examples/ip_pipeline/parser.h b/examples/ip_pipeline/parser.h index 5224b18a89..32b5ff9976 100644 --- a/examples/ip_pipeline/parser.h +++ b/examples/ip_pipeline/parser.h @@ -13,14 +13,6 @@ #define PARSE_DELIMITER " \f\n\r\t\v" -#define skip_white_spaces(pos) \ -({ \ - __typeof__(pos) _p = (pos); \ - for ( ; isspace(*_p); _p++) \ - ; \ - _p; \ -}) - static inline size_t skip_digits(const char *src) { diff --git a/examples/pipeline/cli.c b/examples/pipeline/cli.c index 2ae6cc579f..c1efea3c1a 100644 --- a/examples/pipeline/cli.c +++ b/examples/pipeline/cli.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "cli.h" @@ -45,21 +46,13 @@ #define MSG_FILE_NOT_ENOUGH "Not enough rules in file \"%s\".\n" #define MSG_CMD_FAIL "Command \"%s\" failed.\n" -#define skip_white_spaces(pos) \ -({ \ - __typeof__(pos) _p = (pos); \ - for ( ; isspace(*_p); _p++) \ - ; \ - _p; \ -}) - static int parser_read_uint64(uint64_t *value, const char *p) { char *next; uint64_t val; - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if (!isdigit(*p)) return -EINVAL; @@ -85,7 +78,7 @@ parser_read_uint64(uint64_t *value, const char *p) break; } - p = skip_white_spaces(p); + p = rte_str_skip_leading_spaces(p); if (*p != '\0') return -EINVAL; diff --git a/lib/eal/include/rte_string_fns.h b/lib/eal/include/rte_string_fns.h index bb43b2cba3..13badec7b3 100644 --- a/lib/eal/include/rte_string_fns.h +++ b/lib/eal/include/rte_string_fns.h @@ -15,10 +15,12 @@ extern "C" { #endif +#include #include #include #include +#include /** * Takes string "string" parameter and splits it at character "delim" @@ -115,6 +117,31 @@ rte_strlcat(char *dst, const char *src, size_t size) ssize_t rte_strscpy(char *dst, const char *src, size_t dsize); +/** + * @warning + * @b EXPERIMENTAL: this API may change without prior notice. + * + * Search for the first non whitespace character. + * + * @param src + * The input string to be analysed. + * + * @return + * The address of the first non whitespace character. + */ +__rte_experimental +static inline const char * +rte_str_skip_leading_spaces(const char *src) +{ + const char *p = src; + + while (isspace(*p)) + p++; + + return p; +} + + #ifdef __cplusplus } #endif