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 makeUpper(const std::string& s) {
00015     std::string ans(s.size(),'#');
00016     unsigned int i=s.size();
00017     while(i--!=0)
00018       ans[i]=::toupper(s[i]);
00019     return ans;
00020   }
00021 
00022   std::string removePrefix(const std::string& str, const std::string& pre) {
00023     if(str.compare(0,pre.size(),pre)==0)
00024       return str.substr(pre.size());
00025     return std::string();
00026   }
00027 
00028   bool parseArgs(const std::string& input, std::vector<std::string>& args, std::vector<unsigned int>& offsets) {
00029     std::string cur;
00030     bool isDoubleQuote=false;
00031     bool isSingleQuote=false;
00032     args.clear();
00033     offsets.clear();
00034     unsigned int begin=-1U;
00035     for(unsigned int i=0; i<input.size(); i++) {
00036       char c=input[i];
00037       if(begin==-1U && !isspace(c))
00038         begin=i;
00039       switch(c) {
00040       case ' ':
00041       case '\n':
00042       case '\r':
00043       case '\t':
00044       case '\v':
00045       case '\f':
00046         if(isSingleQuote || isDoubleQuote)
00047           cur+=c;
00048         else if(cur.size()!=0) {
00049           args.push_back(cur);
00050           offsets.push_back(begin);
00051           cur.clear();
00052           begin=-1U;
00053         }
00054         break;
00055       case '\\':
00056         if(i==input.size()-1) { //escaped line break
00057           return false;
00058         } else
00059           cur.push_back(input[++i]);
00060         break;
00061       case '"':
00062         if(isSingleQuote)
00063           cur.push_back(c);
00064         else
00065           isDoubleQuote=!isDoubleQuote;
00066         break;
00067       case '\'':
00068         if(isDoubleQuote)
00069           cur+=c;
00070         else
00071           isSingleQuote=!isSingleQuote;
00072         break;
00073       default:
00074         cur+=c;
00075         break;
00076       }
00077     }
00078     if(cur.size()>0) {
00079       args.push_back(cur);
00080       offsets.push_back(begin);
00081     }
00082     return !isDoubleQuote && !isSingleQuote;
00083   }
00084 
00085 }
00086 
00087 /*! @file
00088  * @brief Implements some useful functions for string manipulation in the string_util namespace
00089  * @author ejt (Creator)
00090  *
00091  * $Author: ejt $
00092  * $Name: tekkotsu-2_2_2 $
00093  * $Revision: 1.3 $
00094  * $State: Exp $
00095  * $Date: 2004/01/14 20:45:28 $
00096  */

Tekkotsu v2.2.2
Generated Tue Jan 4 15:43:15 2005 by Doxygen 1.4.0