File: StringUtil.h

package info (click to toggle)
esys-particle 2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,284 kB
  • sloc: cpp: 77,304; python: 5,647; makefile: 1,176; sh: 10
file content (198 lines) | stat: -rw-r--r-- 5,259 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2011 by The University of Queensland //
// Earth Systems Science Computational Centre (ESSCC)      //
// http://www.uq.edu.au/esscc                              //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.opensource.org/licenses/osl-3.0.php          //
//                                                         //
/////////////////////////////////////////////////////////////


#ifndef ESYS_LSMSTRINGUTIL_H
#define ESYS_LSMSTRINGUTIL_H

#include <string>
#include <vector>
#include <sstream>

namespace esys
{
  namespace lsm
  {
    typedef std::vector<std::string> StringVector;
    /**
     * Convenience functions for string manipulation.
     */
    namespace StringUtil
    {
      typedef esys::lsm::StringVector StringVector;
      template <class TmplIterator>
      class StdOStreamOp
      {
      public:
        std::string operator()(const TmplIterator &it) const
        {
          std::stringstream sStream;
          sStream << *it;

          return sStream.str();
        }
      };

      template<class TmplIterator, class TmplStringOperator>
      inline
      std::string join(
        TmplIterator begin,
        TmplIterator end,
        const std::string &delim,
        TmplStringOperator toStringOp = StdOStreamOp<TmplIterator>())
      {
        std::string str;
        if (begin != end)
        {
          TmplIterator it = begin;
          str = toStringOp(begin);
          for (it++; it != end; it++)
          {
            str += delim + toStringOp(it);
          }
        }
        return str;
      }

      template<class TmplContainer, class TmplStringOperator>
      inline
      std::string join(
        const TmplContainer &container,
        const std::string &delim,
        TmplStringOperator toStringOp = StdOStreamOp<typename TmplContainer::const_iterator>())
      {
        typename TmplContainer::const_iterator it  = container.begin();
        typename TmplContainer::const_iterator end = container.end();
        std::string str;
        if (it != end)
        {
          str = toStringOp(it);
          for (it++; it != end; it++)
          {
            str += delim + toStringOp(it);
          }
        }
        return str;
      }

      inline
      std::string joinStringVector(
        const StringVector &container,
        const std::string &delim
      )
      {
        StringVector::const_iterator it  = container.begin();
        StringVector::const_iterator end = container.end();
        std::string str;
        if (it != end)
        {
          str = *it;
          for (it++; it != end; it++)
          {
            str += delim + *it;
          }
        }
        return str;
      }

      template <class TmplData>
      class StdIStreamOp
      {
      public:
        TmplData operator()(const std::string &str) const
        {
          std::stringstream sStream(str);
          TmplData data;
          sStream >> data;

          return data;
        }
      };
      
      template <typename TmplData>
      TmplData to(const std::string &str)
      {
        return StdIStreamOp<TmplData>()(str);
      }
      
      template <typename TmplData>
      std::string toString(const TmplData &data)
      {
        std::stringstream sStream;
        sStream << data;
        return sStream.str();
      }

      template<class TmplData, class TmplStdStreamOp>
      inline      
      std::vector<TmplData> split(
        const std::string &str,
        const std::string &delim,
        TmplStdStreamOp fromStringOp = StdIStreamOp<TmplData>()
      )
      {
        std::vector<TmplData> vec;
        std::string::size_type prevPos = 0;
        std::string::size_type currPos = str.find(delim);
        while (currPos != std::string::npos)
        {
          vec.push_back(
            fromStringOp(str.substr(prevPos, currPos - prevPos))
          );
          prevPos = currPos + delim.length();
          currPos = str.find(delim, prevPos);
        }
        if (prevPos < str.length())
        {
          vec.push_back(
            fromStringOp(str.substr(prevPos, str.length()))
          );
        }
        else if (prevPos == str.length())
        {
          vec.push_back(fromStringOp(""));
        }

        return vec;
      }

      inline
      StringVector splitStrings(const std::string &str, const std::string &delim)
      {
        return split<std::string, StdIStreamOp<std::string> >(str, delim, StdIStreamOp<std::string>());
      }
      
      inline
      std::string trim(const std::string &str)
      {
        size_t b = 0;
        for (; (b < str.size()) && std::isspace(str.at(b)); b++)
        {
          // continue
        }

        size_t e = 0;
        if (str.size() > 0)
        {
          e = str.size()-1;
          for (; (e > b) && std::isspace(str.at(e)); e--)
          {
            // continue
          }
        }        
        return ((e >= b) ? str.substr(b, e-b+1) : "");
      }
    };
  };
};

#endif