string_utils.hpp 3 KB
Newer Older
1
// Copyright 2021 Thomas A. R. Purcell
2
//
3
4
5
// 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
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
10
11
12
13
// 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.
14

15
/** @file utils/string_utils.hpp
16
 *  @brief Defines a set of functions to manipulate strings
17
 *
18
 *  @author Thomas A. R. Purcell (tpurcell90)
19
20
 *  @bug No known bugs.
 */
21

Thomas Purcell's avatar
Thomas Purcell committed
22
23
24
#ifndef STRING_UTILS
#define STRING_UTILS

25
#include <algorithm>
Thomas Purcell's avatar
Thomas Purcell committed
26
#include <cmath>
27
#include <iostream>
Thomas Purcell's avatar
Thomas Purcell committed
28
29
30
31
32
33
34
35
#include <string>
#include <vector>

#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/trim.hpp>

namespace str_utils
{
36
37
38
39
    /**
     * @brief split a string and trim whitespace
     *
     * @param str String to split
Thomas Purcell's avatar
Thomas Purcell committed
40
     * @param split_tokens Characters to split the string with
41
42
     * @return The vector with the string split along the split tokens
     */
43
    std::vector<std::string> split_string_trim(const std::string str, const std::string split_tokens = ",;:");
44

45
    // DocString: str_utils_latexify
46
47
48
49
    /**
     * @brief Convert a string into a latex string
     *
     * @param str String to convert to a latex string
Thomas Purcell's avatar
Thomas Purcell committed
50
     * @return The LaTeXified version of the string
51
     */
52
    std::string latexify(const std::string str);
53

54
    // DocString: str_utils_matlabify
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    /**
     * @brief Convert a string into a matlab valid name
     *
     * @param str String to convert to a valid matlab name
     * @return The matlab safe version of the string
     */
    std::string matlabify(const std::string str);

    /**
     * @brief Convert an operator into a valid matlab string
     *
     * @param str String to remove operators from
     * @param op The operator to replace
     * @param op_str The string to replace the operator with
     * @return The string with the operators replaced
     */
    std::string op2str(const std::string str, const std::string op, const std::string op_str);
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

    /**
     * @brief Join a vector of strings together
     *
     * @param join_section string to put in between each element
     * @param start pointer to the first element of the vector to join
     * @param sz Number of elements of the vector to join
     * @return The joined string
     */
    std::string join(std::string join_section, std::string* start, int sz);

    /**
     * @brief Join a vector of strings together
     *
     * @param join_section string to put in between each element
     * @param list The vector of strings to join together
     * @return The joined string
     */
    inline std::string join(std::string join_section, std::vector<std::string> list)
    {
        return join(join_section, list.data(), list.size());
    }
Thomas Purcell's avatar
Thomas Purcell committed
94
95
96
}


97
#endif