From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id ELegC5BIHWAeTAAA0tVLHw (envelope-from ) for ; Fri, 05 Feb 2021 13:30:56 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id QIiAB5BIHWBDJgAA1q6Kng (envelope-from ) for ; Fri, 05 Feb 2021 13:30:56 +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 9E8BB9402BD for ; Fri, 5 Feb 2021 13:30:55 +0000 (UTC) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 1E04426A29; Fri, 5 Feb 2021 08:28:33 -0500 (EST) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id D25A91FD75 for ; Fri, 5 Feb 2021 08:27:38 -0500 (EST) Received: by fethera.tethera.net (Postfix, from userid 1001) id CA6726067F; Fri, 5 Feb 2021 08:27:38 -0500 (EST) Received: (nullmailer pid 3258392 invoked by uid 1000); Fri, 05 Feb 2021 13:27:02 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 09/39] util: add strsplit_len: simplified strtok with delimiter escaping Date: Fri, 5 Feb 2021 09:26:24 -0400 Message-Id: <20210205132654.3258292-10-david@tethera.net> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210205132654.3258292-1-david@tethera.net> References: <20210205132654.3258292-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: RYPZ4KODZKRERUMJCWJF3BDSGWOUCU4K X-Message-ID-Hash: RYPZ4KODZKRERUMJCWJF3BDSGWOUCU4K 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.93 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: 9E8BB9402BD X-Spam-Score: -0.93 X-Migadu-Scanner: scn0.migadu.com X-TUID: qaRuffNV14gF 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.30.0