#ifndef __PROG_STR_H__ #define __PROG_STR_H__ #include "std.h" OPEN_NAMESPACE /* characters */ const char cr = '\r'; const char lf = '\n'; const char tab = '\t'; const char quote1 = '\''; const char quote2 = '\"'; const char backslash = '\\'; const string two_quotes = "\"\""; inline bool IsLetter (char ch) { return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch == '_'); } inline bool IsUpperCaseLetter (char ch) { return (ch >= 'A' && ch <= 'Z') || (ch == '_'); } inline bool IsDigit (char ch) { return ch >= '0' && ch <= '9'; } inline bool IsOctal (char ch) { return ch >= '0' && ch <= '7'; } inline bool IsHexDigit (char ch) { return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F'); } inline bool IsLetterOrDigit (int ch) { return IsLetter (ch) || IsDigit (ch); } inline char UpCase (char ch) { if (ch >= 'a' && ch <= 'z') return ch - 'a' + 'A'; else return ch; } inline char LoCase (char ch) { if (ch >= 'A' && ch <= 'Z') return ch - 'A' + 'a'; else return ch; } /* stl */ typedef string::size_type str_size_type; const str_size_type str_npos = string::npos; inline string Head (const string s, int i) { // first i characters return s.substr (0, i); } inline string Tail (const string s, int i) { // skip first i characters return s.substr (i); } inline string Substr (const string s, int start, int len) { // 'len' characters from 'start' position return s.substr (start, len); } inline str_size_type Find (const string s, char c) { return s.find (c); } inline str_size_type RFind (const string s, char c) { return s.rfind (c); } inline str_size_type FindSubstr (const string s, const string t) { return s.find (t); } inline bool ContainsSubstr (const string s, const string t) { return s.find (t) != string::npos; } inline string CharToStr (char c) { return string (1, c); } // convert one character to string inline string CharsToStr (const char * c) { return (c == NULL) ? "" : string (c); } // convert char pointer to string char * StrToChars (const string s); // convert string to char pointer /* strings */ string QuoteChrContent (char value); string QuoteStrContent (const string value); // convert to C-style string literal, without outer quotes string EscapeStr (const string value); // convert nonprintable characters string QuoteStr (const string value, char quote = quote2); // convert to C-style string literal string QuoteChr (char c, char quote = quote1); // convert to C-style character literal string RemoveQuotes (const string value, char quote = quote2); // convert from C-style string UpperCase (string s); // convert to upper case string LowerCase (string s); // convert to lower case string FirstUpperCase (string s); // convert first character to upper case string FirstLowerCase (string s); // convert first character to lower case bool IsValidIdent (const string value); // is it valid identifier bool IsValidNumber (const string value); // is it sequence of digits string Trim (const string s); // remove leading and trailing white spaces string TrimLeft (const string s); // remove leading white spaces string TrimRight (const string s); // remove trailing white spaces bool HasPrefix (const string value, const string prefix); bool HasPrefix (const string value, char prefix); string RemovePrefix (const string value, const string prefix); // remove prefix (if present) string RemovePrefix (const string value, char prefix); bool HasWordPrefix (const string value, const string prefix); bool HasWordPrefix (const string value, char prefix); string RemoveWordPrefix (const string value, const string prefix); string RemoveWordPrefix (const string value, char prefix); bool HasSuffix (const string value, const string suffix); bool HasSuffix (const string value, char suffix); string RemoveSuffix (const string value, const string suffix); // remove suffix (if present) string RemoveSuffix (const string value, char suffix); string ReplaceChars (string s, char c1, char c2); // replace c1 with c2 string RemoveChars (const string s, char c); // remove characters c string ReplaceSpaces (const string s); // replaces spaces with underlines string RemoveSpaces (const string s); // remove spaces string AddUnderlines (const string s); // add '_' between lower case and upper case letters string RemoveUnderlines (const string s); // remove '_' and convert next character to upper case string RemovePath (string s); // remove last '/' and preceding characters string ExtractPath (string s); // remove characters after last '/' string AddSlash (string s); // add '/' (if not already present) string RemoveLastSlash (string s); // remove last '/' (if present and length is greater than one) string AddSubdir (const string dir_name, const string file_name); // combine directory and file name string AddNamespace (string a, string b); // concatenate namespace (if present) and identifier (a::b) string JoinIdentifiers (string a, string b); // join identifiers with '_' or convert first chatacter of second parameter to upper case string ReplaceTwoColons (const string s, char c); // replace '::' with character c string RemoveAsterisk (string value); // remove trailling spaces and one asterisk string ModifyIdentifier (string s); // replace invalid characters with underlines string ModifyFileName (string s); // replace leading invalid character with '-', other with underlines string HeaderIdentifier (const string prefix, const string file_name); // identifier for ifdef directive in header file (__PREFIX_FILE_NAME__) string TrimLines (const string s); // remove leading and trailing white spaces and empty lines string NormalizeLines (const string s); // change end of line to line feed characters string NormalizePath (const string s); // remove ".", ".." and "//" void test_normalize_path (); CLOSE_NAMESPACE #endif /* __PROG_STR_H__ */