StringUtils.h 4.76 KB
Newer Older
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
1
2
3
#ifndef STRING_UTILS_H
#define STRING_UTILS_H

Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
4
5
6
/**
 * @file StringUtils.h
 * @author Bryan BERTHOU
Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
7
 * @date April 04, 2009
Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
8
 * @version 0.1
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
9
10
 */

Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
11
#include <stddef.h>
Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
12
#include <ctime>
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
13
14
15
16
17
18
19
20
#include <string>
#include <utility>
#include <vector>

//TODO uniformiser le fichier, corriger les parametres des fonctions (const, &, ...) refaire la documentation

namespace ElemUtils {

Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
21
22
23
24
/**
 * @class StringUtils
 *
 * @brief Manipulations et traitements sur les chaines de caracteres
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
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
 */
class StringUtils {

public:

    static std::string EMPTY;

    /**
     * @fn static bool isEmpty(const std::string & str)
     * @brief Test si la chaine de caracteres cible est NULL ou vide ""
     * @param str : chaine de caracteres a tester
     * @return bool
     */
    static bool isEmpty(const std::string & str);

    /*!
     * \fn static bool equals(const std::string & str, const std::string & str2)
     * \brief Applique une comparaison entre la chaine 1 et 2 en tenant compte de la casse
     * \param _chaine1 : chaine de caracteres a comparer
     * \param _chaine2 : chaine de caracteres a comparer
     * \return bool : true si les deux chaines de caracteres sont egales
     */
    static bool equals(const std::string & str, const std::string & str2);

    /*!
     * \fn static bool equalsIgnoreCase( std::string _chaine1, std::string _chaine2, bool toUpperCase )
     * \brief Applique une comparaison entre la chaine 1 et 2 sans tenir compte de la casse
     * \param _chaine1 : chaine de caracteres a comparer
     * \param _chaine2 : chaine de caracteres a comparer
     * \return bool : true si les deux chaines de caracteres sont egales
     */
    static bool equalsIgnoreCase(const std::string &chaine,
            const std::string &chaine2, const bool &toUpperCase = true);

    static bool contains(const std::string & str,
            const std::string & searchString);

    static bool containsIgnoreCase(std::string* chaine,
            std::string* searchString, bool toUpperCase);

    static void replaceAll(std::string & str, const std::string & searchString,
            const std::string & replaceString);

    static void replaceAllIgnoreCase(std::string* chaine,
            std::string* searchString, std::string* replaceString,
            bool toUpperCase);

    // Remove leading and trailing spaces from a string
    static void trim(std::string &str, const std::string& whitespace = " \t");

75
76
77
    // Remove all white spaces from a string
    static void trimAll(std::string &str, const std::string& whitespace = " \t");

Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
78
79
80
81
82
83
84
85
86
87
88
89
90
    static unsigned int count(std::string str,
            const std::string & searchString);

    /*!
     * \fn static std::vector<std::string> stringToVector( std::string _chaine, char splitCharacter )
     * \brief Transforme une chaine de caracteres en un vector de string
     * \param _chaine : chaine a splitter
     * \param splitCharacter : caractere de split
     * \return std::vector<std::string>
     */
    static std::vector<std::string> split(const std::string &chaine,
            char splitCharacter);

Nabil Chouika's avatar
refs#16    
Nabil Chouika committed
91
92
93
    static std::vector<std::string> split(const std::string &chaine,
            const std::string & splitSymbols);

Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    /*!
     * \fn static std::string vectorToString( std::vector<std::string> vector )
     * \brief Transforme un vector de string en une chaine de caracteres
     * \param vector : vector a transformer en chaine de caracteres
     * \return std::string
     */
    static std::string vectorToString(std::vector<std::string>* vector);

    static void to_upperCase(std::string &chaine);

    static void to_lowerCase(std::string* chaine);

    static std::pair<std::string, std::vector<char> > fromStringToArrayOfChar(
            const std::string &string);

    static std::string removeAfterLast(const std::string &chaine,
            const char lastCharacter);
Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

    /**
     * Find first occurrence of the pattern in the string and return index position on the string where the last character is matching pattern.
     *
     * @param file
     * @param pattern
     * @param startIndex
     * @return
     */
    static size_t findFirstJustAfter(const std::string &file,
            const std::string &pattern, const size_t startIndex = 0);

    /**
     * Find first occurrence of the pattern in the string and return index position on the string where pattern is starting.
     *
     * @param file
     * @param pattern
     * @param startIndex
     * @return
     */
Bryan Berthou's avatar
refs#16    
Bryan Berthou committed
131
132
133
134
135
    static size_t findFirst(const std::string &file, const std::string &pattern,
            const size_t startIndex = 0);

    static std::string formatDate(const time_t &time,
            const std::string &format);
Bryan Berthou's avatar
refs#16  
Bryan Berthou committed
136
137
138
139
140
};

} // namespace ElemUtils

#endif /* STRING_UTILS_H */