Labels

Wednesday, October 31, 2018

Powerful string processor with c++

/*MYSTR.h*/
 #ifndef _mystr_
 #define _mystr_
 #include<string>
 #include<dirent.h>
 #include<vector>
 
 /* strim remove space from front/end of string */
 /* sreduce remove all redundancy space */
 /* sremove_spaces remove all of space in string */
 std::string strim(const std::string& str, const std::string& whitespace = " \t");
 std::string sreduce(const std::string& str,const std::string& fill = " ",const std::string& whitespace = " \t");
 std::string inline sremove_spaces(std::string str);
 std::vector lisidir(const char *path);
 #endif



/*MYSTR.cc*/
#include <iostream>
#include <string>
#include "MYSTR.h"
#include<algorithm>
/*https://stackoverflow.com/questions/1798112/removing-leading-and-trailing-spaces-from-a-string
 *example 
 *foo = "    too much\t   \tspace\t\t\t  "
 *bar = "one\ntwo"
 *strim(foo) -> [too much               space]  
 *sreduce(foo," ") -> [too much space]  
 *sreduce(foo,"-"," ") -> [too-much-space]  
 *sremove_spaces(bar) -> [onetwo]
*/

/*http://www.cplusplus.com/forum/beginner/145368/
 * find nth str position from string
 */

/*
 * split string
 */

std::string strim(const std::string& str, const std::string& whitespace)
{
    const auto strBegin = str.find_first_not_of(whitespace);
    if (strBegin == std::string::npos)
        return ""; // no content

    const auto strEnd = str.find_last_not_of(whitespace);
    const auto strRange = strEnd - strBegin + 1;

    return str.substr(strBegin, strRange);
}


std::string sreduce(const std::string& str,const std::string& fill,const std::string& whitespace)
{
    // trim first
    auto result = strim(str, whitespace);

    // replace sub ranges
    auto beginSpace = result.find_first_of(whitespace);
    while (beginSpace != std::string::npos)
    {
        const auto endSpace = result.find_first_not_of(whitespace, beginSpace);
        const auto range = endSpace - beginSpace;

        result.replace(beginSpace, range, fill);

        const auto newStart = beginSpace + fill.length();
        beginSpace = result.find_first_of(whitespace, newStart);
    }

    return result;
}


/* remove all space */
std::string inline sremove_spaces(std::string str) 
{
    str.erase(std::remove_if(str.begin(), str.end(), isspace), str.end());
    return str;
}
 
/* store all filename into vector */
std::vector listdir(const char *path)
{
    struct dirent *entry;
    DIR *dp;
    dp = opendir(path);
    if(dp==NULL)
    {
        perror("opendir");
        exit(1);
    }

    std::vector filename;
    while((entry=readdir(dp)))
        filename.push_back(entry->d_name);
    closedir(dp);
    return filename;
}

/* find nth str in string */
std::size_t find_nth_str(const std::string& s, std::size_t nth, string val)
{
    std::size_t pos = 0;
    unsigned occurrence = 0;

//    while (occurrence != nth && (pos = s.find(val, pos) != std::string::npos))
//        ++occurrence;
      while (occurrence != nth)
      {
          pos = s.find(val,  pos);
          if(pos != std::string::npos){
          ++occurrence;
          ++pos;
          }
      }

    return pos-1;
}

/* rfind nth str in string */
std::size_t rfind_nth_str(const std::string& s, std::size_t nth, string val)
{
    std::size_t pos = s.length();
    unsigned occurrence = 0;

//    while (occurrence != nth && (pos = s.find(val, pos) != std::string::npos))
//        ++occurrence;
      while (occurrence != nth)
      {
          pos = s.rfind(val,  pos);
          if(pos != std::string::npos){
          ++occurrence;
          --pos;
          }
      }

    return pos+1;
}


/* split string */
const vector split(const string& s, const char& c)
{
    string buff{""};
    vector v;
        
     for(auto n:s)
     {
         if(n != c) buff+=n; else
             if(n == c && buff != "") { v.push_back(buff); buff = ""; }
     }
     if(buff != "") v.push_back(buff);

     return v;
}

/* append string to end of the line */
void apped_line(string filename,vector str)
{
    ifstream rf(filename.c_str(),std::ios::in);
    if(rf.good())
    {
        std::stringstream bufer;
        std::string line;
        int i=0;
        while(getline(rf,line)){
            bufer< str)
{   
    deque text;
    string line;
    ifstream rf(file_name);
    if(rf.good()){
        int i=0;
        while(getline(rf,line)){
            text.push_back(line+delimiter+str.at(i));
            i++;
        }
    }
    else{
        cerr<<"can't open "<::iterator it = text.begin(); it!=text.end(); it++)
        wf<<*it<<'\n';
    wf.close();
}

#if 0
int main()
{
    std::vector<string> fn = listdir(".");
    std::cout<<fn[0]<<endl;
#endif

No comments:

Post a Comment