Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/util/SubString.h @ 11534

Last change on this file since 11534 was 11098, checked in by muemart, 9 years ago

Minor C++11 improvements:

  • Drop ImplicitConversion.h in favor of std::is_convertible.
  • Move constructor & assignment for MultiType and SubString. I'm not sure if the MultiType should convert types when moving. Currently it doesn't, because otherwise it would have no benefit over copying.
  • Use standard library functions for sleeping.
  • Property svn:eol-style set to native
File size: 9.8 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Christian Meyer
24 *   Co-authors:
25 *      Benjamin Grauer
26 *      Fabian 'x3n' Landau
27 *
28
29//  splitLine
30//  STL string tokenizer
31//
32//  Created by Clemens Wacha.
33//  Version 1.0
34//  Copyright (c) 2005 Clemens Wacha. All rights reserved.
35
36 *   Extended by Fabian 'x3n' Landau by the SL_PARENTHESES mode.
37 */
38
39 /**
40    @file
41    @ingroup String
42    @brief A helper class to split a string into several tokens.
43
44    @anchor SubStringExample
45
46    The class SubString can be used to split an std::string into multiple tokens, using
47    a delimiter. SubString allows different options, for example to remove whitespaces
48    around the delimiters or different safe-mode chars, like quotation marks and braces.
49
50    You can access the tokens of the SubString using the [] operator like an array.
51    SubString also supports to join the tokens (or a subset of the tokens) again using
52    @ref orxonox::SubString::join() "join()". It's even possible to get a subset of the
53    SubString as another SubString using @ref orxonox::SubString::subSet() "subSet()".
54
55    Example:
56    @code
57    std::string text = "This is a test, \"Hello \\\" World\" and vector {1, 2, 3}";
58    SubString tokens(text, SubString::WhiteSpaces, "", false, '\\', true, '"', true, '{', '}', true, '\0');
59
60    for (size_t i = 0; i < tokens.size(); ++i)
61        orxout() << i << ": " << tokens[i] << endl;
62    @endcode
63
64    The output of this code is:
65     - 0: This
66     - 1: is
67     - 2: a
68     - 3: test,
69     - 4: Hello " World
70     - 5: and
71     - 6: vector
72     - 7: 1, 2, 3
73
74    The string was split using the delimiter " ". A string between quotation mark is not
75    split, the same holds for strings between '{' and '}'. Note how the quotation marks and
76    the braces were removed from the tokens, because the corresponding argument is 'true'.
77
78    Also note that the comma after "test" in token 3 is still there - it is neither part of the
79    delimiters SubString::WhiteSpaces nor part of the delimiterNeighbours parameter, so it
80    remains a part of the token.
81*/
82
83#ifndef __SubString_H__
84#define __SubString_H__
85
86#include "UtilPrereqs.h"
87
88#include <vector>
89#include <string>
90
91namespace orxonox
92{
93    /**
94        @brief A class that splits a string into multiple tokens using different options.
95
96        The string is split into multiple tokens using a delimiter. Different options like
97        escape character, quotation marks, and more can be used to satisfy your needs.
98
99        See @ref SubStringExample "this description" for an example.
100    */
101    class _UtilExport SubString
102    {
103        /// An enumerator for the internal state of the parser
104        enum class SPLIT_LINE_STATE
105        {
106            NORMAL,            //!< Normal state
107            ESCAPE,            //!< After an escape character
108            SAFEMODE,          //!< In safe mode (usually between quotation marks).
109            SAFEESCAPE,        //!< In safe mode with the internal escape character, that escapes even the savemode character.
110            COMMENT,           //!< In Comment mode.
111            PARENTHESES,       //!< Between parentheses (usually '{' and '}')
112            PARENTHESESESCAPE, //!< Between parentheses with the internal escape character, that escapes even the closing parenthesis character.
113        };
114
115    public:
116        SubString();
117        /// Copy constructor
118        SubString(const SubString& other) = default;
119        /// Move constructor
120        SubString(SubString&& other) = default;
121        SubString(const std::string& line,
122                  const std::string& delimiters = SubString::WhiteSpaces,
123                  const std::string& delimiterNeighbours = "",
124                  bool bAllowEmptyEntries=false,
125                  char escapeChar ='\\',
126                  bool bRemoveEscapeChar = true,
127                  char safemodeChar = '"',
128                  bool bRemoveSafemodeChar = true,
129                  char openparenthesisChar = '{',
130                  char closeparenthesisChar = '}',
131                  bool bRemoveParenthesisChars = true,
132                  char commentChar = '\0');
133        SubString(size_t argc, const char** argv);
134        SubString(const SubString& other, size_t begin, size_t length = std::string::npos);
135        ~SubString();
136
137        // operate on the SubString
138        SubString& operator=(const SubString& other);
139        /// Move assignment
140        SubString& operator=(SubString&& other) = default;
141        bool operator==(const SubString& other) const;
142        bool compare(const SubString& other, size_t length = std::string::npos) const;
143        SubString operator+(const SubString& other) const;
144        SubString& operator+=(const SubString& other);
145        /// Appends the tokens of another SubString to this. @return This SubString.
146        inline SubString& append(const SubString& other) { return (*this += other); }
147
148        /////////////////////////////////////////
149        // Split and Join the any String. ///////
150        size_t split(const std::string& line,
151                     const std::string& delimiters = SubString::WhiteSpaces,
152                     const std::string& delimiterNeighbours = "",
153                     bool bAllowEmptyEntries = false,
154                     char escapeChar ='\\',
155                     bool bRemoveEscapeChar = true,
156                     char safemodeChar = '"',
157                     bool bRemoveSafemodeChar = true,
158                     char openparenthesisChar = '{',
159                     char closeparenthesisChar = '}',
160                     bool bRemoveParenthesisChars = true,
161                     char commentChar = '\0');
162
163        std::string join(const std::string& delimiter = " ") const;
164        ////////////////////////////////////////
165
166        // retrieve a SubSet from the String
167        SubString subSet(size_t begin, size_t length = std::string::npos) const;
168
169        // retrieve Information from within
170        /// Returns true if the SubString is empty
171        inline bool empty() const { return this->tokens_.empty(); }
172        /// Returns the number of tokens stored in this SubString
173        inline size_t size() const { return this->tokens_.size(); }
174        /// Returns the i'th token from the subset of strings @param index The index of the requested token
175        inline const std::string& operator[](size_t index) const { return this->tokens_[index]; }
176        /// Returns the i'th token from the subset of strings @param index The index of the requested token
177        inline const std::string& getString(size_t index) const { return (*this)[index]; }
178        /// Returns all tokens as std::vector
179        inline const std::vector<std::string>& getAllStrings() const { return this->tokens_; }
180        /// Returns true if the token is in safemode. @param index The index of the token
181        inline bool isInSafemode(size_t index) const { return this->bTokenInSafemode_[index]; }
182        /// Returns the front of the list of tokens.
183        inline const std::string& front() const { return this->tokens_.front(); }
184        /// Returns the back of the list of tokens.
185        inline const std::string& back() const { return this->tokens_.back(); }
186        /// Removes the back of the list of tokens.
187        inline void pop_back() { this->tokens_.pop_back(); this->bTokenInSafemode_.pop_back(); }
188
189        void debug() const;
190
191    public:
192        static const std::string WhiteSpaces;           ///< All whitespaces (usually used as delimiters or delimiterNeighbours
193        static const std::string WhiteSpacesWithComma;  ///< All whitespaces and the comma (usually used as delimiters)
194        static const SubString   NullSubString;         ///< An empty SubString
195
196    private:
197        // the almighty algorithm.
198        static SPLIT_LINE_STATE splitLine(std::vector<std::string>& tokens,
199                                          std::vector<bool>& bTokenInSafemode,
200                                          const std::string& line,
201                                          const std::string& delimiters = SubString::WhiteSpaces,
202                                          const std::string& delimiterNeighbours = "",
203                                          bool bAllowEmptyEntries = false,
204                                          char escapeChar = '\\',
205                                          bool bRemoveEscapeChar = true,
206                                          char safemodeChar = '"',
207                                          bool bRemoveSafemodeChar = true,
208                                          char openparenthesisChar = '{',
209                                          char closeparenthesisChar = '}',
210                                          bool bRemoveParenthesisChars = true,
211                                          char commentChar = '\0',
212                                          SPLIT_LINE_STATE start_state = SPLIT_LINE_STATE::NORMAL);
213
214        std::vector<std::string>  tokens_;              ///< The tokens after splitting the input line
215        std::vector<bool>         bTokenInSafemode_;    ///< Saves for each token if it was in safe mode (between quotation marks or parenthesis)
216    };
217}
218
219#endif /* __SubString_H__ */
Note: See TracBrowser for help on using the repository browser.