diff options
author | mattprost <matt.prost@ni.com> | 2022-04-06 12:29:04 -0500 |
---|---|---|
committer | Aaron Rossetto <aaron.rossetto@ni.com> | 2022-04-07 13:28:02 -0700 |
commit | 4bd20a9e2a2760c05bd27c5fb96b11668ff5c682 (patch) | |
tree | b48b6e9579f6bab07bafef8a11449391931ed109 /host/include | |
parent | c0b2e69fb1d10e3b53a97e56070634cc92da8fa2 (diff) | |
download | uhd-4bd20a9e2a2760c05bd27c5fb96b11668ff5c682.tar.gz uhd-4bd20a9e2a2760c05bd27c5fb96b11668ff5c682.tar.bz2 uhd-4bd20a9e2a2760c05bd27c5fb96b11668ff5c682.zip |
utils: string: Add split string utility function
Signed-off-by: mattprost <matt.prost@ni.com>
Diffstat (limited to 'host/include')
-rw-r--r-- | host/include/uhd/utils/string.hpp | 35 |
1 files changed, 35 insertions, 0 deletions
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 <uhd/config.hpp> +#include <uhd/exception.hpp> + +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<std::string, std::string> 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 |