PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // 00007 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 00037 00038 00039 /* ******************************************************* 00040 * $Id: stringutils.h 10036 2009-03-19 14:02:24Z nouiz $ 00041 * AUTHORS: Pascal Vincent 00042 * This file is part of the PLearn library. 00043 ******************************************************* */ 00044 00045 // This file contains useful functions for string manipulation 00046 // that are used in the PLearn Library 00047 00048 00051 #ifndef stringutils_INC 00052 #define stringutils_INC 00053 00054 #include <string> 00055 #include <vector> 00056 #include <map> 00057 #include <iostream> 00058 #include <iomanip> 00059 #include <sstream> 00060 // Removed inclusion of tostring because it causes circular includes; please 00061 // include tostring directly in your code now (rather than assume it gets 00062 // included through stringutils 00063 // #include <plearn/base/tostring.h> 00064 #include <plearn/base/lexical_cast.h> 00065 00066 namespace PLearn { 00067 using namespace std; 00068 00069 // THIS slash CHAR HAS TO GO AWAY!!!! 00070 #if defined(_MINGW_) 00071 #define slash "\\" 00072 #define slash_char '\\' 00073 #else 00074 #define slash "/" 00075 #define slash_char '/' 00076 #endif 00077 00079 string left(const string& s, size_t width, char padding=' '); 00080 string right(const string& s, size_t width, char padding=' '); 00081 string center(const string& s, size_t width, char padding=' '); 00082 00084 string removeblanks(const string& s); 00085 00087 string removeallblanks(const string& s); 00088 00090 string removenewline(const string& s); 00091 00094 string removequotes(const string& s); 00095 00097 string quote_string(const string& s); 00098 00100 string lowerstring(const string& s); 00101 00103 string upperstring(const string& s); 00104 00107 string pgetline(istream& in=cin); 00108 00110 bool isBlank(const string& s); 00111 00113 bool isParagraphBlank(const string& s); 00114 00125 void parseBaseAndParameters(const string& s, string& base, 00126 map<string, string>& params, 00127 map<string, string>* added = 0, 00128 map<string, string>* backup = 0, 00129 const string& delimiter = "::"); 00130 00132 string space_to_underscore(string str); 00133 00135 string underscore_to_space(string str); 00136 00138 string backslash_to_slash(string str); 00139 00141 inline bool string_begins_with(const string& s, const string& beginning) 00142 { 00143 string::size_type n = beginning.size(); 00144 return (s.size() >= n && beginning == s.substr(0, n)); 00145 } 00146 00148 inline bool string_ends_with(const string& s, const string& end) 00149 { 00150 string::size_type n = end.size(); 00151 string::size_type m = s.size(); 00152 return (m >= n && end == s.substr(m-n, n) ); 00153 } 00154 00157 int search_replace(string& text, const string& searchstr, const string& replacestr); 00158 00160 vector<string> split_from_string(const string& s, const string& delimiter); 00161 00163 vector<string> split(const string& s, char delimiter); 00164 00170 vector<string> split(const string& s, const string& delimiters=" \t\n\r", bool keepdelimiters=false); 00171 00175 vector<string> split_all(const string& s, const string& delimiters=" \t\n\r"); 00176 00186 vector<string> split_quoted_delimiter(const string& s, char delimiter, 00187 const string& double_quote); 00188 00189 vector<string> split_quoted_delimiter(const string& s, char delimiter, 00190 char double_quote); 00191 00192 vector<string> split_quoted_delimiter(const string& s, const string& delimiters, 00193 const string& double_quote); 00194 00200 void split_on_first(const string& s, const string& delimiters, string& left, string& right); 00201 00207 pair<string,string> split_on_first(const string& s, 00208 const string& delimiters=" \t\n\r"); 00209 00211 string join(const vector<string>& s, const string& separator=" "); 00212 00214 vector<string> addprepostfix(const string& prefix, const vector<string>& names, const string& postfix); 00215 00217 inline vector<string> addprefix(const string& prefix, const vector<string>& names) 00218 { return addprepostfix(prefix, names, ""); } 00219 00221 inline vector<string> addpostfix(const vector<string>& names, const string& postfix) 00222 { return addprepostfix("", names, postfix); } 00223 00226 string addprepostfix(const string& prefix, const string& text, const string& postfix); 00227 00230 inline string addprefix(const string& prefix, const string& text) 00231 { return addprepostfix(prefix, text, ""); } 00232 00235 inline string addpostfix(const string& text, const string& postfix) 00236 { return addprepostfix("", text, postfix); } 00237 00240 vector<string> stringvector(int argc, char** argv); 00241 00249 string get_option(const vector<string> &command_line, 00250 const string& option, const string& default_value); 00251 00258 bool find(const vector<string> &command_line, const string& option); 00259 00261 int findpos(const vector<string> &v, string element); 00262 00264 vector<string> remove(const vector<string> &v, string element); 00265 00266 00274 int universal_compare(const string& x, const string& y); 00275 00276 00278 00280 // TO REMOVE 00282 string extract_filename(const string& filepath); 00283 00285 string extract_directory(const string& filepath); 00286 00289 string extract_extension(const string& filepath); 00290 00292 //'.' and everything after it 00293 string remove_extension(const string& filename); 00294 00296 string remove_trailing_slash(const string& path); 00297 00299 string append_slash(const string& path); 00300 00302 string extract_filename_without_extension(const string& filepath); 00303 // END OF TO REMOVE 00305 00307 void remove_comments(string& text, const string& commentstart="#"); 00308 00310 vector<string> getNonBlankLines(const string & in); 00311 00314 string* data_filename_2_filenames(const string& filename, int& nb_files); 00315 00317 ostream& operator<<(ostream& out, const vector<string>& vs); 00318 00320 template <class U, class V> 00321 ostream& operator<<(ostream& out, const pair<U,V>& p) 00322 { 00323 return out << p.first << ':' << p.second; 00324 } 00325 } // end of namespace PLearn 00326 00327 #endif 00328 00329 00330 /* 00331 Local Variables: 00332 mode:c++ 00333 c-basic-offset:4 00334 c-file-style:"stroustrup" 00335 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00336 indent-tabs-mode:nil 00337 fill-column:79 00338 End: 00339 */ 00340 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :