strutil.h

Go to the documentation of this file.
00001 
00008 
00009 //
00010 // Utilities for std::string
00011 // defined in namespace strutil
00012 // by James Fancy
00013 //
00015 
00016 #pragma once
00017 
00018 #include <string>
00019 #include <vector>
00020 #include <sstream>
00021 #include <iomanip>
00022 
00023 // declaration
00024 namespace strutil {
00025 
00026     std::string trimLeft(const std::string& str);
00027     std::string trimRight(const std::string& str);
00028     std::string trim(const std::string& str);
00029         std::string trim(const std::string& str, const std::string & delimitor);
00030     
00031         std::string toLower(const std::string& str);
00032     std::string toUpper(const std::string& str);
00033 
00034     bool startsWith(const std::string& str, const std::string& substr);
00035     bool endsWith(const std::string& str, const std::string& substr);
00036     bool equalsIgnoreCase(const std::string& str1, const std::string& str2);
00037 
00038     template<class T> T parseString(const std::string& str);
00039     template<class T> T parseHexString(const std::string& str);
00040     template<bool> bool parseString(const std::string& str);
00041 
00042     template<class T> std::string toString(const T& value);
00043     template<class T> std::string toHexString(const T& value, int width = 0);
00044     std::string toString(const bool& value);
00045 
00046     std::vector<std::string> split(const std::string& str, const std::string& delimiters);
00047 }
00048 
00049 // Tokenizer class
00050 namespace strutil {
00056     class Tokenizer
00057     {
00058     public:
00059         static const std::string DEFAULT_DELIMITERS;
00060         Tokenizer(const std::string& str);
00061         Tokenizer(const std::string& str, const std::string& delimiters);
00062 
00063         bool nextToken();
00064         bool nextToken(const std::string& delimiters);
00065         const std::string getToken() const;
00066 
00071         void reset();
00072 
00073     protected:
00074         size_t m_Offset;
00075         const std::string m_String;
00076         std::string m_Token;
00077         std::string m_Delimiters;
00078     };
00079 
00080 }
00081 
00082 // implementation of template functions
00083 namespace strutil {
00084 
00085     template<class T> T parseString(const std::string& str) {
00086         T value;
00087         std::istringstream iss(str);
00088         iss >> value;
00089         return value;
00090     }
00091 
00092     template<class T> T parseHexString(const std::string& str) {
00093         T value;
00094         std::istringstream iss(str);
00095         iss >> hex >> value;
00096         return value;
00097     }
00098 
00099     template<class T> std::string toString(const T& value) {
00100         std::ostringstream oss;
00101         oss << value;
00102         return oss.str();
00103     }
00104 
00105     template<class T> std::string toHexString(const T& value, int width) {
00106         std::ostringstream oss;
00107         oss << hex;
00108         if (width > 0) {
00109             oss << setw(width) << setfill('0');
00110         }
00111         oss << value;
00112         return oss.str();
00113     }
00114 
00115 }
00116 
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Defines