Homepage
Demos
Overview
Downloads
Dev. Resources
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   std::string trim(const std::string& str) {
00029     if(str.size()==0)
00030       return str;
00031     unsigned int b=0;
00032     unsigned int e=str.size()-1;
00033     while(b<str.size() && isspace(str[b]))
00034       b++;
00035     while(b<e && isspace(str[e]))
00036       e--;
00037     return str.substr(b,e-b+1);
00038   }
00039   
00040   bool parseArgs(const std::string& input, std::vector<std::string>& args, std::vector<unsigned int>& offsets) {
00041     std::string cur;
00042     bool isDoubleQuote=false;
00043     bool isSingleQuote=false;
00044     args.clear();
00045     offsets.clear();
00046     unsigned int begin=-1U;
00047     for(unsigned int i=0; i<input.size(); i++) {
00048       char c=input[i];
00049       if(begin==-1U && !isspace(c))
00050         begin=i;
00051       switch(c) {
00052       case ' ':
00053       case '\n':
00054       case '\r':
00055       case '\t':
00056       case '\v':
00057       case '\f':
00058         if(isSingleQuote || isDoubleQuote)
00059           cur+=c;
00060         else if(cur.size()!=0) {
00061           args.push_back(cur);
00062           offsets.push_back(begin);
00063           cur.clear();
00064           begin=-1U;
00065         }
00066         break;
00067       case '\\':
00068         if(i==input.size()-1) { //escaped line break
00069           return false;
00070         } else
00071           cur.push_back(input[++i]);
00072         break;
00073       case '"':
00074         if(isSingleQuote)
00075           cur.push_back(c);
00076         else
00077           isDoubleQuote=!isDoubleQuote;
00078         break;
00079       case '\'':
00080         if(isDoubleQuote)
00081           cur+=c;
00082         else
00083           isSingleQuote=!isSingleQuote;
00084         break;
00085       default:
00086         cur+=c;
00087         break;
00088       }
00089     }
00090     if(cur.size()>0) {
00091       args.push_back(cur);
00092       offsets.push_back(begin);
00093     }
00094     return !isDoubleQuote && !isSingleQuote;
00095   }
00096 
00097 }
00098 
00099 /*! @file
00100  * @brief Implements some useful functions for string manipulation in the string_util namespace
00101  * @author ejt (Creator)
00102  *
00103  * $Author: ejt $
00104  * $Name: tekkotsu-2_4_1 $
00105  * $Revision: 1.4 $
00106  * $State: Exp $
00107  * $Date: 2005/06/01 05:47:49 $
00108  */

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:49 2005 by Doxygen 1.4.4