From 4bd20a9e2a2760c05bd27c5fb96b11668ff5c682 Mon Sep 17 00:00:00 2001 From: mattprost Date: Wed, 6 Apr 2022 12:29:04 -0500 Subject: utils: string: Add split string utility function Signed-off-by: mattprost --- host/include/uhd/utils/string.hpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 host/include/uhd/utils/string.hpp (limited to 'host/include') diff --git a/host/include/uhd/utils/string.hpp b/host/include/uhd/utils/string.hpp new file mode 100644 index 000000000..426ce798d --- /dev/null +++ b/host/include/uhd/utils/string.hpp @@ -0,0 +1,35 @@ +// +// Copyright 2022 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#pragma once + +#include +#include + +namespace uhd { namespace string { + +/*! + * Split a string using a delimiter. The delimiter must be a substring of the + * original string. + * + * \param str the string to be split + * \param delim the delimiter in str used to determine the position of the split + * \return a pair of substrings containing the characters before the first + * instance of the delimiter and after the first instance of the delimiter + * \throws uhd::runtime_error if the delimiter is not found in the original string + */ +UHD_API std::pair split( + const std::string& str, const std::string& delim) +{ + auto delim_pos = str.find(delim); + if (delim_pos == std::string::npos) { + throw uhd::runtime_error( + "Delimiter \"" + delim + "\" not found in string \"" + str + "\""); + } + return std::make_pair(str.substr(0, delim_pos), str.substr(delim_pos + 1)); +} + +}} // namespace uhd::string -- cgit v1.2.3