From: Matthieu Longo <matthieu.longo@arm.com>
To: <gdb-patches@sourceware.org>
Cc: Tom Tromey <tom@tromey.com>, Matthieu Longo <matthieu.longo@arm.com>
Subject: [PATCH v4 1/5] gdbsupport: add extract_next_field helper
Date: Mon, 16 Nov 2026 12:09:32 +0000 [thread overview]
Message-ID: <20261116120936.425082-2-matthieu.longo@arm.com> (raw)
In-Reply-To: <20261116120936.425082-1-matthieu.longo@arm.com>
Add extract_next_field, a helper for extracting a view from a buffer
up to a given separator. The helper also returns an iterator to the
beginning of the next entry, skipping consecutive separators.
The helper supports views whose value type is 'char', such as
gdb::array_view<char> and std::string_view (from c++20).
---
gdb/Makefile.in | 1 +
| 74 ++++++++++++++++++++++++
| 77 +++++++++++++++++++++++++
3 files changed, 152 insertions(+)
create mode 100644 gdb/unittests/extract-field-selftests.c
create mode 100644 gdbsupport/extract-field.h
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 55c4d012f25..fa709199ecb 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -511,6 +511,7 @@ SELFTESTS_SRCS = \
unittests/cli-utils-selftests.c \
unittests/command-def-selftests.c \
unittests/common-utils-selftests.c \
+ unittests/extract-field-selftests.c \
unittests/copy_bitwise-selftests.c \
unittests/enum-flags-selftests.c \
unittests/enumerate-selftests.c \
--git a/gdb/unittests/extract-field-selftests.c b/gdb/unittests/extract-field-selftests.c
new file mode 100644
index 00000000000..28c14bb3f3e
--- /dev/null
+++ b/gdb/unittests/extract-field-selftests.c
@@ -0,0 +1,74 @@
+/* Self tests for general utility routines for GDB, the GNU debugger.
+
+ Copyright (C) 2026 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "gdbsupport/selftest.h"
+
+#include "gdbsupport/array-view.h"
+#include "gdbsupport/extract-field.h"
+
+namespace selftests {
+
+static void
+extract_next_field_tests ()
+{
+ auto bytewise_match = [] (const auto &s1, const auto &s2) -> bool
+ {
+ return std::equal (s1.begin (), s1.end (), s2.begin (), s2.end ());
+ };
+
+ {
+ std::vector<std::string> inputs = {
+ "\n1234\nabc\n\ndef",
+ "\n1234\nabc\n\ndef\n\n",
+ };
+ std::vector<std::vector<std::string>> expecteds = {
+ {"1234", "abc", "def" },
+ {"1234", "abc", "def" },
+ };
+
+ for (auto n = 0; n < inputs.size () && n < expecteds.size (); ++n)
+ {
+ auto &s = inputs[n];
+ auto &expected = expecteds[n];
+
+ using view_t = gdb::array_view<char>;
+ std::vector<view_t> lines;
+ view_t buffer (s.data (), s.size ());
+ for (auto it = buffer.begin (); it != buffer.end ();)
+ {
+ auto [line, next_line_begin]
+ = extract_next_field (buffer, it, '\n');
+ lines.emplace_back (line);
+ it = next_line_begin;
+ }
+
+ SELF_CHECK (expected.size () == lines.size ());
+ for (auto i = 0; i < expected.size () && i < lines.size (); ++i)
+ SELF_CHECK (bytewise_match (expected[i], lines[i]));
+ }
+ }
+}
+
+} /* namespace selftests */
+
+INIT_GDB_FILE (common_string_selftests)
+{
+ selftests::register_test ("extract_next_field",
+ selftests::extract_next_field_tests);
+}
--git a/gdbsupport/extract-field.h b/gdbsupport/extract-field.h
new file mode 100644
index 00000000000..1eee1042026
--- /dev/null
+++ b/gdbsupport/extract-field.h
@@ -0,0 +1,77 @@
+/* String extraction utility routine for GDB, the GNU debugger.
+
+ Copyright (C) 2026 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef GDBSUPPORT_EXTRACT_FIELD_H
+#define GDBSUPPORT_EXTRACT_FIELD_H
+
+#include <algorithm>
+
+/* Extract a view from BUFFER starting at START.
+
+ Leading occurrences of SEPARATOR at START are skipped. The returned view
+ extends from the first non-separator character to the next occurrence of
+ SEPARATOR, or to BUFFER.end () if no separator is found.
+
+ Return the extracted view together with an iterator pointing to the first
+ non-separator character after the extracted view. Successive separators
+ are skipped. If there is no following non-separator character, the returned
+ iterator is BUFFER.end ().
+
+ If START does not point into BUFFER, return an empty view and BUFFER.end ().
+
+ ViewT may be gdb::array_view<char> or std::string_view. The later requires
+ C++20, which added the constructor taking the range [first, last). */
+
+template <class ViewT>
+std::pair<ViewT, typename ViewT::iterator>
+extract_next_field (ViewT buffer,
+ typename ViewT::iterator start,
+ char separator = '\0')
+{
+ static_assert (!std::is_reference_v <ViewT>);
+ static_assert (std::is_same_v <typename ViewT::value_type, char>);
+
+ auto next_start = buffer.end ();
+
+ /* Reject a START iterator that does not point into BUFFER. */
+ if (start < buffer.begin () || start >= buffer.end ())
+ return {ViewT (), next_start};
+
+ /* Skip leading occurrences of the separator. */
+ while (start != buffer.end () && *start == separator)
+ ++start;
+
+ auto it = std::find (start, buffer.end (), separator);
+
+ /* If no separator is found, the remainder of BUFFER is the final string. */
+ if (it != buffer.end ())
+ {
+ /* Otherwise, skip successive separators so that NEXT_START points to
+ the beginning of the next string, if any.
+ Without this, trailing separators would cause an extra empty string
+ to be returned. */
+ for (next_start = std::next (it);
+ next_start != buffer.end () && *next_start == separator;
+ next_start = std::next (next_start));
+ }
+
+ return {ViewT (start, it), next_start};
+}
+
+#endif /* GDBSUPPORT_EXTRACT_FIELD_H */
--
2.56.0
next prev parent reply other threads:[~2026-11-16 12:11 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 12:09 [PATCH v4 0/5] gdb: introduce file_reader_t to read procfs files Matthieu Longo
2026-11-16 12:09 ` Matthieu Longo [this message]
2026-11-16 12:09 ` [PATCH v4 2/5] gdb/linux: refactor /proc mapping parsing to use array_view Matthieu Longo
2026-11-16 12:09 ` [PATCH v4 3/5] gdb: introduce helper class target_file_reader Matthieu Longo
2026-11-16 12:09 ` [PATCH v4 4/5] gdb/linux-tdep: use target_file_reader for procfs parsing Matthieu Longo
2026-11-16 12:09 ` [PATCH v4 5/5] gdb/linux-tdep: remove legacy parse_smaps_data overload Matthieu Longo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261116120936.425082-2-matthieu.longo@arm.com \
--to=matthieu.longo@arm.com \
--cc=gdb-patches@sourceware.org \
--cc=tom@tromey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox