File: string.hpp

package info (click to toggle)
sight 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,252 kB
  • sloc: cpp: 310,629; xml: 17,622; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (171 lines) | stat: -rw-r--r-- 5,403 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/************************************************************************
 *
 * Copyright (C) 2024 IRCAD France
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#pragma once

#include <sight/core/config.hpp>

#include <iostream>
#include <sstream>
#include <string>

namespace sight::core
{

class SIGHT_CORE_CLASS_API string final
{
public:

    /// @brief Concatenate the given arguments into a string using the given separator.
    /// @note This method uses variadic template as argument to concatenate.
    /// @param[in] _separator : the separator to use. If the separator is an empty string, it will be ignored.
    /// @param[in] _first : the first element to concatenate.
    /// @param[in] _args : the other elements to concatenate.
    template<typename S, typename T, typename ... Args>
    static std::string join(const S& _separator, const T& _first, const Args& ... _args);

    template<typename T, typename ... Args>
    static std::string concat(const T& _first, const Args& ... _args);

private:

    template<typename S, typename T>
    static void join_impl(std::ostringstream& _stream, const S& _separator, const T& _t);

    template<typename S, typename T, typename ... Args>
    static void join_impl(
        std::ostringstream& _stream,
        const S& _separator,
        const T& _first,
        const Args& ... _args
    );
};

//------------------------------------------------------------------------------

template<typename S, typename T>
void string::join_impl(std::ostringstream& _stream, const S& _separator, const T& _current)
{
    // By default, we ignore empty elements.
    bool skip = false;

    // Nullptr is skipped.
    if constexpr(std::is_null_pointer_v<T>)
    {
        skip = true;
    }
    // Empty string is skipped.
    else if constexpr(std::is_base_of_v<std::string, T>|| std::is_base_of_v<std::string, T>)
    {
        skip = _current.empty();
    }
    // Empty char* like is skipped.
    else if constexpr(std::is_same_v<typename std::decay<T>::type, char*>
                      || std::is_same_v<typename std::decay<T>::type, const char*>)
    {
        if constexpr(std::is_pointer_v<T>)
        {
            skip = _current == nullptr || _current[0] == '\0';
        }
        else
        {
            skip = _current[0] == '\0';
        }
    }

    if(!skip)
    {
        bool skip_separator = false;

        // If the separator is not null, we add it if not the first elements.
        // Of course, this is still a noop if the separator is empty.
        if constexpr(std::is_null_pointer_v<S>)
        {
            skip_separator = true;
        }
        else if constexpr(std::is_base_of_v<std::string, S>|| std::is_base_of_v<std::string, S>)
        {
            skip_separator = _separator.empty();
        }
        else if constexpr(std::is_same_v<typename std::decay<S>::type, char*>
                          || std::is_same_v<typename std::decay<S>::type, const char*>)
        {
            if constexpr(std::is_pointer_v<S>)
            {
                skip_separator = _separator == nullptr || _separator[0] == '\0';
            }
            else
            {
                skip_separator = _separator[0] == '\0';
            }
        }

        // Add the separator if necessary (not for the first element).
        if(!skip_separator && _stream.tellp() != 0)
        {
            _stream << _separator;
        }

        // Concatenate the current element.
        _stream << _current;
    }
}

//------------------------------------------------------------------------------

template<typename S, typename T, typename ... Args>
void string::join_impl(
    std::ostringstream& _stream,
    const S& _separator,
    const T& _first,
    const Args& ... _args
)
{
    // Concatenate the first element.
    join_impl(_stream, _separator, _first);

    // Recursively concatenate the next elements.
    join_impl(_stream, _separator, _args ...);
}

//------------------------------------------------------------------------------

template<typename S, typename T, typename ... Args>
std::string string::join(const S& _separator, const T& _first, const Args& ... _args)
{
    // Create the stream buffer to concatenate the elements.
    std::ostringstream stream;
    join_impl(stream, _separator, _first, _args ...);

    // Return the concatenated string.
    return stream.str();
}

//------------------------------------------------------------------------------

template<typename T, typename ... Args>
std::string string::concat(const T& _first, const Args& ... _args)
{
    // Join with an empty/null separator.
    return join(nullptr, _first, _args ...);
}

} // namespace sight::core