00001 #ifndef DVUTIL_STRINGS_H 00002 #define DVUTIL_STRINGS_H 00003 // $Id: strings.h,v 1.3 2008/10/16 16:51:01 dvermeir Exp $ 00004 00005 #include <stdexcept> 00006 #include <string> 00007 #include <vector> 00008 00009 namespace Dv { 00010 namespace String { 00011 /** Split a string consisting of substrings separated by a separator string. 00012 * The substrings will be appended to the output vector. 00013 * @param input string, e.g. 'abcXYZdefXYZ1234' (with 'XYZ' the separator string) 00014 * @param separator string, e.g. 'XYZ' in the above example 00015 * @param output vector, e.g. { 'abc', 'def', '1234' } 00016 * @return the number of strings added to output, e.g. 3 00017 * 00018 * Some borderline cases (each time with '/' as separator): 00019 * @code 00020 * '/abc/def' yields { '', 'abc', 'def' } 00021 * 'abc/def/1234' yields { 'abc', 'def', '1234' } 00022 * 'abc/def/' yields { 'abc', 'def', '' } 00023 * @endcode 00024 */ 00025 size_t split(const std::string& input, const std::string& separator, 00026 std::vector<std::string>& output) throw(); 00027 /** Split and trim the results. 00028 * Split as in Dv::String::split and, in addition, trim 00029 * the results before appending them to the vector. 00030 * @param input string, e.g. 'abcXYZdefXYZ1234' (with 'XYZ' the separator string) 00031 * @param separator string, e.g. 'XYZ' in the above example 00032 * @param output vector, e.g. { 'abc', 'def', '1234' } 00033 * @return the number of strings added to output, e.g. 3 00034 */ 00035 size_t split_and_trim(const std::string& input, const std::string& separator, 00036 std::vector<std::string>& output) throw(); 00037 00038 /** Change (in-place) all chars in string to lower case. 00039 * @param s string to convert. 00040 * @return reference to first argument. 00041 */ 00042 std::string& tolower(std::string& s) throw (); 00043 00044 /** Change (in-place) all chars in string to upper case. 00045 * @param s string to convert. 00046 * @return reference to first argument. 00047 */ 00048 std::string& toupper(std::string& s) throw (); 00049 00050 /** Remove leading and trailing white noise (as defined by isspace(3)) 00051 * The argument is changed and returned 00052 * @param s string to trim 00053 * @return reference to the argument 00054 */ 00055 std::string& trim(std::string& s) throw (); 00056 /** Remove leading and trailing white noise (as defined by isspace(3)) 00057 * The argument is changed and returned 00058 * @param s string to trim 00059 * @return trimmed version of s 00060 */ 00061 std::string trim(const std::string& s) throw (); 00062 00063 /** Replace each space character in @a s by a given character. 00064 * A character @a x is a space character is @a isspace(x) is true. 00065 * @param s string to despace 00066 * @param c replacement char 00067 * @return s 00068 */ 00069 std::string& despace(std::string& s, char c = '-') throw (); 00070 00071 /** Remove leading and trailing white noise (' ','\\n','\\t') from C string. 00072 * @param pc C-string to trim. 00073 * @return pointer to first non-white-space character in the argument. 00074 * @warning The argument is changed. Since the return value points to the 00075 * first non-white-space char in the argument, the following is 00076 * a recepy for disaster: 00077 * @code 00078 * char* pc = new char[10]; 00079 * .. 00080 * pc = trim(pc); 00081 * delete[] pc; 00082 * @endcode 00083 */ 00084 char* trim(char* pc) throw (); 00085 } 00086 } 00087 00088 00089 #endif
dvutil-1.0.10 | [ 5 December, 2009] |