Program Listing for File string_util.hpp#

Return to documentation for file (python/morpheus/morpheus/_lib/include/morpheus/utilities/string_util.hpp)

/*
 * SPDX-FileCopyrightText: Copyright (c) 2021-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <sstream>
#include <string>

namespace morpheus {

#define MORPHEUS_CONCAT_STR(strs) ((std::ostringstream&)(std::ostringstream() << strs)).str()

/****** Component public implementations *******************/
/****** StringUtil ****************************************/

struct StringUtil
{
    template <typename IterT>
    static std::string join(IterT begin, IterT end, std::string const& separator)
    {
        std::ostringstream result;
        if (begin != end)
            result << *begin++;
        while (begin != end)
            result << separator << *begin++;
        return result.str();
    }

    template <typename IterT>
    static std::string array_to_str(IterT begin, IterT end)
    {
        return MORPHEUS_CONCAT_STR("[" << join(begin, end, ", ") << "]");
    }

    template <typename IterT>
    static std::string map_to_str(IterT begin, IterT end)
    {
        std::ostringstream ss;

        ss << "{";

        if (begin != end)
        {
            ss << begin->first << ": '" << begin->second << "'";
            ++begin;
        }
        while (begin != end)
        {
            ss << ", " << begin->first << ": '" << begin->second << "'";
            ++begin;
        }

        ss << "}";

        return ss.str();
    }

    static bool str_contains(const std::string& str, const std::string& search_str);
};  // end of group
}  // namespace morpheus