From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id 4jm/AARV8l/ZAwAA0tVLHw (envelope-from ) for ; Sun, 03 Jan 2021 23:36:36 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id YO7TNwNV8l+TUAAAB5/wlQ (envelope-from ) for ; Sun, 03 Jan 2021 23:36:35 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [IPv6:2607:5300:201:3100::1657]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (2048 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id A675E940222 for ; Sun, 3 Jan 2021 23:36:35 +0000 (UTC) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id EDE2629D4A; Sun, 3 Jan 2021 18:36:08 -0500 (EST) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id C0B0929CB2 for ; Sun, 3 Jan 2021 18:36:01 -0500 (EST) Received: by fethera.tethera.net (Postfix, from userid 1001) id B904E5FF47; Sun, 3 Jan 2021 18:36:01 -0500 (EST) Received: (nullmailer pid 126216 invoked by uid 1000); Sun, 03 Jan 2021 23:35:56 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 11/36] util: add strsplit_len: simplified strtok with delimiter escaping Date: Sun, 3 Jan 2021 19:35:22 -0400 Message-Id: <20210103233547.122707-12-david@tethera.net> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210103233547.122707-1-david@tethera.net> References: <20210103233547.122707-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: 7BME67RJ4PCTMANWGOB7AO5NMCOHL2VB X-Message-ID-Hash: 7BME67RJ4PCTMANWGOB7AO5NMCOHL2VB X-MailFrom: bremner@tethera.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -0.88 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2607:5300:201:3100::1657 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: A675E940222 X-Spam-Score: -0.88 X-Migadu-Scanner: scn0.migadu.com X-TUID: Rpesa1h+xvX5 This will be used to make iterators for configuration values. --- util/string-util.c | 23 +++++++++++++++++++++++ util/string-util.h | 14 ++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/util/string-util.c b/util/string-util.c index de8430b2..27f8a26b 100644 --- a/util/string-util.c +++ b/util/string-util.c @@ -24,6 +24,7 @@ #include #include +#include char * strtok_len (char *s, const char *delim, size_t *len) @@ -37,6 +38,28 @@ strtok_len (char *s, const char *delim, size_t *len) return *len ? s : NULL; } +const char * +strsplit_len (const char *s, char delim, size_t *len) +{ + bool escaping = false; + size_t count = 0; + + /* Skip initial unescaped delimiters */ + while (*s && *s == delim) + s++; + + while (s[count] && (escaping || s[count] != delim)) { + escaping = (s[count] == '\\'); + count++; + } + + if (count==0) + return NULL; + + *len = count; + return s; +} + const char * strtok_len_c (const char *s, const char *delim, size_t *len) { diff --git a/util/string-util.h b/util/string-util.h index fb95a740..80647c5f 100644 --- a/util/string-util.h +++ b/util/string-util.h @@ -26,6 +26,20 @@ char *strtok_len (char *s, const char *delim, size_t *len); /* Const version of strtok_len. */ const char *strtok_len_c (const char *s, const char *delim, size_t *len); +/* Simplified version of strtok_len, with a single delimiter. + * Handles escaping delimiters with \ + * Usage pattern: + * + * const char *tok = input; + * const char *delim = ';'; + * size_t tok_len = 0; + * + * while ((tok = strsplit_len (tok + tok_len, delim, &tok_len)) != NULL) { + * // do stuff with string tok of length tok_len + * } + */ +const char *strsplit_len (const char *s, char delim, size_t *len); + /* Return a talloced string with str sanitized. * * Whitespace characters (tabs and newlines) are replaced with spaces, -- 2.29.2