stringutil.cpp
Go to the documentation of this file.
00001 #include "tfd_modules/opl/stringutil.h"
00002 #include <algorithm>
00003 #include <locale>
00004 #include <stdio.h>
00005 #include <iostream>
00006 
00007 namespace StringUtil
00008 {
00009 
00010    bool startsWith(const std::string & str, const std::string substr)
00011    {
00012       return (str.find(substr) == 0);
00013    }
00014 
00015    bool endsWith(const std::string & str, const std::string substr)
00016    {
00017       size_t pos = str.rfind(substr);
00018       if(pos == std::string::npos) // doesnt even contain it
00019          return false;
00020 
00021       size_t len = str.length();
00022       size_t elen = substr.length();
00023       // at end means: Pos found + length of end equal length of full string.
00024       if( pos + elen == len ) {
00025          return true;
00026       }
00027 
00028       // not at end
00029       return false;
00030    }
00031 
00032    std::string toLower(const std::string & s)
00033    {
00034       std::string ret;
00035       std::transform(s.begin(), s.end(), back_inserter(ret), (int(*)(int)) std::tolower);
00036       return ret;
00037    }
00038 
00039    std::string toUpper(const std::string & s)
00040    {
00041       std::string ret;
00042       std::transform(s.begin(), s.end(), back_inserter(ret), (int(*)(int)) std::toupper);
00043       return ret;
00044    }
00045 
00046    std::string trim(const std::string & s)
00047    {
00048       if(s.length() == 0)
00049          return s;
00050       size_t b = s.find_first_not_of(" \t\r\n");
00051       size_t e = s.find_last_not_of(" \t\r\n");
00052       if(b == std::string::npos)
00053          return "";
00054       return std::string(s, b, e - b + 1);
00055    }
00056 
00057    std::vector<std::string> split(const std::string & s, const char* delim)
00058    {
00059       std::vector<std::string> elems;
00060       std::string::size_type lastPos = 0;
00061       std::string::size_type pos     = 0;
00062 
00063       do {
00064          pos = s.find_first_of(delim, lastPos);
00065          // ignore empty tokens from two consecutive delimiter
00066          // or a delimiter at the end of the string
00067          if (pos != lastPos && lastPos < s.length())
00068          {
00069              elems.push_back(s.substr(lastPos, pos - lastPos));
00070          }
00071          lastPos = pos + 1;
00072       } while(std::string::npos != pos);
00073 
00074       return elems;
00075    }
00076 
00077    std::string createFromNumber(int value)
00078    {
00079        char buffer[100];
00080        snprintf(buffer, 100, "%d", value);
00081        return std::string(buffer);
00082    }
00083 
00084    std::string createFromNumber(double value)
00085    {
00086        char buffer[100];
00087        snprintf(buffer, 100, "%f", value);
00088 //       std::cout << buffer << std::endl;
00089 //       printf(buffer);
00090        return std::string(buffer);
00091    }
00092 };
00093 
00094 
00095 


tfd_modules
Author(s): Maintained by Christian Dornhege (see AUTHORS file).
autogenerated on Mon Oct 6 2014 07:52:06