Currently job artifacts in CI/CD pipelines on LRZ GitLab never expire. Starting from Wed 26.1.2022 the default expiration time will be 30 days (GitLab default). Currently existing artifacts in already completed jobs will not be affected by the change. The latest artifacts for all jobs in the latest successful pipelines will be kept. More information: https://gitlab.lrz.de/help/user/admin_area/settings/continuous_integration.html#default-artifacts-expiration

stringutils.h 7.57 KB
Newer Older
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
schultezub's avatar
schultezub committed
11
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
12
// 
13
14
15
16
// 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
17
// 
18
19
20
21
// 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.
22
23
24
// 
// ================================================================================================

schultezub's avatar
schultezub committed
25
26
27
#ifndef STRINGUTILS_H__
#define STRINGUTILS_H__

28
#include "cgt/exception.h"
schultezub's avatar
schultezub committed
29
#include <iomanip>
schultezub's avatar
schultezub committed
30
31
32
33
#include <sstream>
#include <string>
#include <vector>

34
35
#include "core/coreapi.h"

schultezub's avatar
schultezub committed
36
namespace campvis {
schultezub's avatar
schultezub committed
37
38
39
40
41
42

    /**
     * Collection of various helper methods for strings.
     *
     * \sa std::string
     */
43
    class CAMPVIS_CORE_API StringUtils {
44
    public:
schultezub's avatar
schultezub committed
45
46
47
48
49
        /**
         * Converts the string \a str to lowercase.
         * \param str   String to convert.
         * \return Lowercase version of \a str.
         */
50
51
        static std::string lowercase(const std::string& str);

schultezub's avatar
schultezub committed
52
53
54
55
56
        /**
         * Converts the string \a str to uppercase.
         * \param str   String to convert.
         * \return Uppercase version of \a str.
         */
57
58
59
60
61
62
63
64
        static std::string uppercase(const std::string& str);

        /**
         * Splits \a str into a vector of strings representing float values.
         * Floats formatted as [-][0-9]*.[0-9]* are considered, all other characters in between are ignored.
         *
         * \param   str     Input string to parse.
         *
65
         * \note    TODO: The detection algorithm is a little simplified and will not yield correct results
66
67
68
69
70
         *          in every case.
         **/
        static std::vector<std::string> parseFloats(const std::string& str);

        /**
71
         * Replaces all occurrences of \a from in \a str with \a to.
72
73
         * \param str   String to perform replacement on.
         * \param from  String to be replaced.
74
75
         * \param to    String replace.
         * \return  \a str with all occurrences of \a from replaced with \a to.
76
77
         */
        static std::string replaceAll(const std::string& str, const std::string& from, const std::string& to);
schultezub's avatar
schultezub committed
78
79

        /**
80
81
82
83
84
85
86
87
88
89
90
91
92
93
         * Splits the string \a str into pieces separated by the delimiters in \a delimiter.
         * \param str       String to split.
         * \param delimiter Set of delimiters.
         * \return  Vector of the split substrings.
         */
        static std::vector<std::string> split(const std::string& str, const std::string& delimiter);

        /**
         * Splits the string \a str into trimmed pieces separated by the delimiters in \a delimiter.
         * Delimiters in quoted strings (\a quotes) will be ignored, double quotes within quoted strings will be 
         * interpreted as literal quotes. Each token will be trimmed.
         *
         * \param str           String to split.
         * \param delimiter     Set of delimiters.
schultezub's avatar
schultezub committed
94
95
96
97
         * \param quotes        Character used for quotes.
         * \param whitespace    Set of whitespace characters which shall be removed during trimming.
         * \return  Vector of the split substrings.
         */
98
        static std::vector<std::string> splitStringsafe(const std::string& str, const std::string& delimiter, char quotes = '\'', const std::string& whitespace = " \t\n\r\0\x0B");
schultezub's avatar
schultezub committed
99
100
101

        /**
         * Trims the string \a str.
102
         * All leading and trailing occurrences of the characters in \a whitespace will be removed.
schultezub's avatar
schultezub committed
103
104
105
106
107
         * 
         * \param str           The string to trim.
         * \param whitespace    Set of whitespace characters which shall be removed at the beginning and the end.
         * \return  The original string without leading and trailing whitespaces.
         */
schultezub's avatar
schultezub committed
108
        static std::string trim(const std::string& str, const std::string& whitespace = " \t\n\r\0\x0B");
schultezub's avatar
schultezub committed
109

110
111
112
113
114
115
116
117
        /**
         * Converts the value \a value to a string.
         * \param value     The value to convert, must be compatible with std::stringstream.
         * \return  A string representation of \a value.
         */
        template<class T>
        static std::string toString(const T& value);

schultezub's avatar
schultezub committed
118
119
120
121
122
123
124
125
126
127
        /**
         * Converts the value \a value to a zero-padded string.
         * \param   value           The value to convert, must be compatible with std::stringstream.
         * \param   paddingWidth    Number of digits for padding to apply
         * \param   fill            Fill character for padding
         * \return  A string representation of \a value.
         */
        template<class T>
        static std::string toString(const T& value, size_t paddingWidth, char fill);

128
129
130
131
        /**
         * Converts the string \a str to its original value.
         * \param str   The string to convert
         * \return  The back-converted value of \a str, type must be compatible with std::stringstream.
schultezub's avatar
schultezub committed
132
         * \throw   tgt::Exception on conversion failure
133
134
         */
        template<class T>
schultezub's avatar
schultezub committed
135
        static T fromString(const std::string& str) throw (tgt::Exception);
136
137
138
139
140
141
142
143

        /**
         * Joins the substrings in \a tokens together using \a delimiter in between.
         * \param tokens    List of substrings to join.
         * \param delimiter Delimiter which shall be placed between the substrings.
         * \return  A string containing the joined substrings.
         */
        template<typename T>
schultezub's avatar
schultezub committed
144
145
146
147
148
        static std::string join(const std::vector<T>& tokens, const std::string& delimiter);
    };

// - Template definition --------------------------------------------------------------------------

149
    template<class T>
schultezub's avatar
schultezub committed
150
    std::string StringUtils::toString(const T& value) {
151
152
153
154
155
        std::ostringstream stream;
        stream << value;
        return stream.str();
    }

schultezub's avatar
schultezub committed
156
157
158
159
160
161
    template<class T>
    std::string StringUtils::toString(const T& value, size_t paddingWidth, char fill) {
        std::ostringstream stream;
        stream << std::setw(paddingWidth) << std::setfill(fill) << value;
        return stream.str();
    }
162
    template<class T>
schultezub's avatar
schultezub committed
163
    T StringUtils::fromString(const std::string& str) throw (tgt::Exception) {
164
165
166
167
        T toReturn;
        std::istringstream stream;
        stream.str(str);
        if (!(stream >> toReturn))
schultezub's avatar
schultezub committed
168
            throw tgt::Exception("Failed to convert string '" + str + "'");
169
170
171
172
        return toReturn;
    }

    template<typename T>
schultezub's avatar
schultezub committed
173
    std::string StringUtils::join(const std::vector<T>& tokens, const std::string& delimiter) {
174
175
176
177
        if (tokens.empty())
            return "";
        std::stringstream s;
        s << tokens[0];
schultezub's avatar
schultezub committed
178
        for (typename std::vector<T>::size_type i = 1; i < tokens.size(); ++i)
179
180
            s << delimiter << tokens[i];
        return s.str();
schultezub's avatar
schultezub committed
181
182
183
184
185
    }

}

#endif // STRINGUTILS_H__