00001
00002
00027 #ifndef CXXUTILS_STRINGUTILS_H
00028 #define CXXUTILS_STRINGUTILS_H
00029
00030 #include <string>
00031 #include <ostream>
00032 #include <boost/regex.hpp>
00033
00034 namespace CxxUtils {
00035
00036 namespace StringUtils {
00037
00038 typedef std::string::size_type size_type;
00039
00040 const std::string beginNormal = "\033[0m";
00041 const std::string beginBoldWhite = "\033[1m";
00042 const std::string beginBoldPink = "\033[1;35m";
00043 const std::string beginBoldBlue = "\033[1;34m";
00044 const std::string beginBoldYellow = "\033[1;33m";
00045 const std::string beginBoldGreen = "\033[1;32m";
00046 const std::string beginBoldRed = "\033[1;31m";
00047 const std::string beginPink = "\033[35m";
00048 const std::string beginBlue = "\033[34m";
00049 const std::string beginYellow = "\033[33m";
00050 const std::string beginGreen = "\033[32m";
00051 const std::string beginRed = "\033[31m";
00052
00053
00054 const std::string letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
00055 const std::string whitespace = " \t\n\r";
00056
00057 const boost::regex generic_latex_regex("\\\\([abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]+)[ ]*");
00058 const boost::regex generic_xml_regex("<\\s*([abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]+)([^>])*>");
00059 const boost::regex generic_html_entity_regex("&[abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]+;");
00060
00061 enum SPECIALSCRIPT {
00062 NORMALSCRIPT = 0,
00063 SUPERSCRIPT = 1,
00064 SUBSCRIPT = 2
00065 };
00066
00067 enum FORMAT {
00068 ASCII = 0,
00069 UNICODE = 1,
00070 LATEX = 2,
00071 HTML = 3
00072 };
00073
00074 size_t getStringWidth(const std::string& str);
00075 void writeFixedWidth(std::ostream& os, const std::string& input, size_t width, const std::string& align);
00076
00077 size_type findParenthesisMatch (const std::string& str,
00078 size_type nextpos,
00079 const std::string& paropen,
00080 const std::string& parclose);
00081 size_type rfindParenthesisMatch(const std::string& str,
00082 size_type nextpos,
00083 const std::string& paropen,
00084 const std::string& parclose);
00085
00086 size_type findFree (const std::string& haystack,
00087 const std::string& needle,
00088 const std::string& paropen,
00089 const std::string& parclose,
00090 size_type startpos = 0);
00091 size_type rfindFree(const std::string& haystack,
00092 const std::string& needle,
00093 const std::string& paropen,
00094 const std::string& parclose,
00095 size_type startpos);
00096
00097 size_type findFreeOf (const std::string& haystack,
00098 const std::string& needles,
00099 const std::string& paropen,
00100 const std::string& parclose,
00101 size_type startpos = 0);
00102 size_type rfindFreeOf(const std::string& haystack,
00103 const std::string& needles,
00104 const std::string& paropen,
00105 const std::string& parclose,
00106 size_type startpos);
00107
00108 std::string replaceSymbols(const std::string& str, StringUtils::FORMAT inputFormat, StringUtils::FORMAT outputFormat);
00109 size_type findBeginSpecialScript(const std::string& str,
00110 StringUtils::SPECIALSCRIPT scripttype,
00111 size_type pos = 0);
00112 size_type findEndSpecialScript(const std::string& str,
00113 StringUtils::SPECIALSCRIPT scripttype,
00114 size_type pos = 0);
00115
00116 StringUtils::FORMAT guessFormat(const std::string& input);
00117 std::string convertText(const std::string& input, StringUtils::FORMAT inputFormat, StringUtils::FORMAT outputFormat);
00118 std::string convertText(const std::string& input, StringUtils::FORMAT outputFormat);
00119
00120 std::string stripUnprintableCharacters(const std::string& str, bool allowNonAscii = true);
00121 std::string replaceSpecialScript(const std::string& str, StringUtils::SPECIALSCRIPT inputType, StringUtils::SPECIALSCRIPT outputType);
00122
00123 inline bool isOnlyWhitespace(const std::string&str){
00124 return (str.find_first_not_of(StringUtils::whitespace) == std::string::npos);
00125 }
00126
00127 inline std::string trim(const std::string& str){
00128 size_t start = str.find_first_not_of(StringUtils::whitespace);
00129 size_t end = str.find_last_not_of(StringUtils::whitespace);
00130 return str.substr(start,end-start+1);
00131 }
00132 }
00133
00134 }
00135
00136 #endif //CXXUTILS_STRINGUTILS_H