Homepage Demos Overview Downloads Tutorials Reference
Credits

string_util.cc

Go to the documentation of this file.
00001 #include "string_util.h"
00002 #include <ctype.h>
00003 
00004 namespace string_util {
00005 
00006   std::string makeLower(const std::string& s) {
00007     std::string ans(s.size(),'#');
00008     unsigned int i=s.size();
00009     while(i--!=0)
00010       ans[i]=::tolower(s[i]);
00011     return ans;
00012   }
00013 
00014   std::string removePrefix(const std::string& str, const std::string& pre) {
00015     if(str.compare(0,pre.size(),pre)==0)
00016       return str.substr(pre.size());
00017     return std::string();
00018   }
00019 
00020   bool parseArgs(const std::string& input, std::vector<std::string>& args, std::vector<unsigned int>& offsets) {
00021     std::string cur;
00022     bool isDoubleQuote=false;
00023     bool isSingleQuote=false;
00024     args.clear();
00025     offsets.clear();
00026     unsigned int begin=-1U;
00027     for(unsigned int i=0; i<input.size(); i++) {
00028       char c=input[i];
00029       if(begin==-1U && !isspace(c))
00030         begin=i;
00031       switch(c) {
00032       case ' ':
00033       case '\n':
00034       case '\r':
00035       case '\t':
00036       case '\v':
00037       case '\f':
00038         if(isSingleQuote || isDoubleQuote)
00039           cur+=c;
00040         else if(cur.size()!=0) {
00041           args.push_back(cur);
00042           offsets.push_back(begin);
00043           cur.clear();
00044           begin=-1U;
00045         }
00046         break;
00047       case '\\':
00048         if(i==input.size()-1) { //escaped line break
00049           return false;
00050         } else
00051           cur.push_back(input[++i]);
00052         break;
00053       case '"':
00054         if(isSingleQuote)
00055           cur.push_back(c);
00056         else
00057           isDoubleQuote=!isDoubleQuote;
00058         break;
00059       case '\'':
00060         if(isDoubleQuote)
00061           cur+=c;
00062         else
00063           isSingleQuote=!isSingleQuote;
00064         break;
00065       default:
00066         cur+=c;
00067         break;
00068       }
00069     }
00070     if(cur.size()>0) {
00071       args.push_back(cur);
00072       offsets.push_back(begin);
00073     }
00074     return !isDoubleQuote && !isSingleQuote;
00075   }
00076 
00077 }
00078 
00079 /*! @file
00080  * @brief Implements some useful functions for string manipulation in the string_util namespace
00081  * @author ejt (Creator)
00082  *
00083  * $Author: ejt $
00084  * $Name: tekkotsu-1_5 $
00085  * $Revision: 1.2 $
00086  * $State: Rel $
00087  * $Date: 2003/09/02 20:58:49 $
00088  */

Tekkotsu v1.5
Generated Fri Oct 10 15:52:00 2003 by Doxygen 1.3.4