String.h revision 10447:a465576671d4
1#ifndef __STRING_H__
2#define __STRING_H__
3
4#include <string>
5#include <cstdarg>
6#include <vector>
7#include <sstream>
8#include <bitset>
9
10namespace LibUtil
11{
12    using std::string;
13    using std::vector;
14
15    class String : public string
16    {
17        public:
18            static String format(const String& format_, ...);
19            static String format(const String& format_, va_list args_);
20            template<class T> static String toString(const T& value_);
21            static String toBitString(unsigned int value_, unsigned int num_bits_);
22            template<class T> static T fromString(const String& str_);
23
24        private:
25            static const unsigned int msBufferSize;
26
27        public:
28            String();
29            String(const string& str_);
30            String(const char* str_, size_t n);
31            String(const char* str_);
32            String(size_t n, char c);
33            String(int value_);
34            String(unsigned int value_);
35            String(long value_);
36            String(unsigned long value_);
37            String(float value_);
38            String(double value_);
39            String(bool value_);
40            ~String();
41
42        public:
43            // Remove leading and trailing whitespace
44            String& trim();
45            // Substitute str1 with str2
46            String& substitute(const String& str1_, const String& str2_);
47            // Split the String into vector of Strings separated by delimiters_
48            vector<String> split(const char* delimiters_) const;
49            vector<String> split(const String* delimiters_, unsigned int num_delimiters_ = 1) const;
50            vector<String> splitByString(const String& delimiters_) const;
51
52            // Check if contains str
53            bool contain(const String& str_) const;
54
55        public:
56            // Convertions
57            const char* toCString() const;
58            int toInt() const;
59            unsigned int toUInt() const;
60            long toLong() const;
61            unsigned long toULong() const;
62            float toFloat() const;
63            double toDouble() const;
64            bool toBool() const;
65            operator const char*() const;
66            operator int() const;
67            operator unsigned int() const;
68            operator long() const;
69            operator unsigned long() const;
70            operator float() const;
71            operator double() const;
72            operator bool() const;
73            String& operator=(char c_);
74    };
75
76    template<class T> String String::toString(const T& value_)
77    {
78        std::ostringstream ost;
79        ost << value_;
80        return ost.str();
81    }
82
83    template<> inline String String::toString<bool>(const bool& value_)
84    {
85        if(value_ == true)
86        {
87            return "TRUE";
88        }
89        else
90        {
91            return "FALSE";
92        }
93    }
94
95    inline String String::toBitString(unsigned int value_, unsigned int num_bits_)
96    {
97        std::bitset<sizeof(unsigned int)*8> bitSet(value_);
98        String ret = String(bitSet.to_string());
99        ret = ret.substr(ret.length()-num_bits_);
100        return ret;
101    }
102
103    template<class T> T String::fromString(const String& str_)
104    {
105        T ret;
106        std::istringstream ist(str_);
107        ist >> ret;
108        return ret;
109    }
110
111    template<> inline String String::fromString<String>(const String& str_)
112    {
113        return str_;
114    }
115
116    template<> inline bool String::fromString<bool>(const String& str_)
117    {
118        bool ret;
119        if((str_ == String("TRUE")) || (str_ == String("true")))
120        {
121            ret = true;
122        }
123        else if((str_ == string("FALSE")) || (str_ == String("false")))
124        {
125            ret = false;
126        }
127        else
128        {
129            //std::cerr << "Invalid bool value: " << str_ << std::endl;
130            throw ("Invalid bool value: " + str_);
131        }
132        return ret;
133    }
134
135    template<class T> String arrayToString(
136            const T* array_, unsigned int start_index_, unsigned int end_index_,
137            const String& delimiters_
138            )
139    {
140        // Ensure end_index_ >= start_index_ + 1
141        if(end_index_ <= start_index_)
142        {
143            throw("Invalid index range: start_index = " + (String)start_index_ + ", end_index = " + (String)end_index_);
144        }
145
146        String ret = "[";
147        for(unsigned int i = start_index_; i < (end_index_-1); ++i)
148        {
149            ret += (String)array_[i] + delimiters_;
150        }
151        ret += (String)array_[end_index_-1] + "]";
152        return ret;
153    }
154
155    template<class T> String arrayToString(const T* array_, unsigned int num_elements_)
156    {
157        return arrayToString(array_, 0, num_elements_, ", ");
158    }
159
160    template<class T> String arrayToString(const T* array_, unsigned int start_index_, unsigned int end_index_)
161    {
162        return arrayToString(array_, start_index_, end_index_);
163    }
164
165    template<class T> String vectorToString(
166        const vector<T>& vector_, unsigned int start_index_, unsigned int end_index_,
167        const String& delimiters_
168        )
169    {
170        // Ensure end_index_ >= start_index_ + 1, or if the vector is empty
171        if((end_index_ <= start_index_) || (end_index_ > vector_.size()))
172        {
173            // If the vector is empty, return empty array
174            if (vector_.size() == 0)
175                return "[]";
176
177            throw("Invalid index range: start_index = " + (String)start_index_ + ", end_index = " + (String)end_index_);
178        }
179
180        String ret = "[";
181        for(unsigned int i = start_index_; i < (end_index_-1); ++i)
182        {
183            ret += (String)vector_[i] + delimiters_;
184        }
185        ret += (String)vector_[end_index_-1] + "]";
186        return ret;
187    }
188
189    template<class T> String vectorToString(const vector<T>& vector_)
190    {
191        return vectorToString(vector_, 0, vector_.size(), ", ");
192    }
193
194    template<class T> String vectorToString(const vector<T>& vector_, unsigned int num_elements_)
195    {
196        return vectorToString(vector_, 0, num_elements_, ", ");
197    }
198
199    template<class T> String vectorToString(const vector<T>& vector_, unsigned int start_index_, unsigned int end_index_)
200    {
201        return vectorToString(vector_, start_index_, end_index_);
202    }
203
204    template<class T> vector<T> castStringVector(const vector<String>& vector_)
205    {
206        vector<T> ret_vector;
207        for(unsigned int i = 0; i < vector_.size(); ++i)
208        {
209            ret_vector.push_back((T)vector_[i]);
210        }
211        return ret_vector;
212    }
213
214    std::istream& safeGetline(std::istream& is_, String& str_);
215} // namespace LibUtil
216
217#endif // __STRING_H__
218
219