stringfunc.cc 5.27 KB
Newer Older
thomas.forbriger's avatar
thomas.forbriger committed
1
2
3
4
5
/*! \file stringfunc.cc
 * \brief string functions copied from former class library (implementation)
 * 
 * ----------------------------------------------------------------------------
 * 
thomas.forbriger's avatar
thomas.forbriger committed
6
 * $Id$
thomas.forbriger's avatar
thomas.forbriger committed
7
8
9
10
11
12
 * \author Thomas Forbriger
 * \date 30/06/2005
 * 
 * string functions copied from former class library (implementation)
 * 
 * Copyright (c) 2005 by Thomas Forbriger (BFO Schiltach) 
thomas.forbriger's avatar
thomas.forbriger committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 *
 * ----
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ----
thomas.forbriger's avatar
thomas.forbriger committed
29
30
31
 * 
 * REVISIONS and CHANGES 
 *  - 30/06/2005   V1.0   Thomas Forbriger
32
33
 *  - 25/11/2010   V1.1   implemented more efficient method to strip
 *                        whitespace
thomas.forbriger's avatar
thomas.forbriger committed
34
35
36
37
 * 
 * ============================================================================
 */
#define TF_STRINGFUNC_CC_VERSION \
38
  "TF_STRINGFUNC_CC   V1.1"
thomas.forbriger's avatar
thomas.forbriger committed
39
#define TF_STRINGFUNC_CC_CVSID \
thomas.forbriger's avatar
thomas.forbriger committed
40
  "$Id$"
thomas.forbriger's avatar
thomas.forbriger committed
41
42
43
44
45
46
47
48
49
50
51
52

#include <tfxx/stringfunc.h>

#include<algorithm>
#include<iterator>
#include<functional>
#include<ctype.h>

namespace tfxx {

  namespace string {

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    // remove leading and trailing whitespace
    std::string trimws(std::string s)
    {
      if (s.length()>0)
      {
        string::size_type ib=s.find_first_not_of(" ", 0);
        if (ib==string::npos)
        {
          s="";
        }
        else
        {
          string::size_type il=s.find_last_not_of(" \r", s.length());
          string::size_type n=il>=ib ? il-ib+1 : 0;
          if (n==0) { ib = 0; }
          if ((ib!=0) || (n!=s.length())) { s=s.substr(ib,n); }
        }
      }
      return(s);
    } // std::string trimws(std::string s)

    /*----------------------------------------------------------------------*/

thomas.forbriger's avatar
thomas.forbriger committed
76
77
78
    // remove leading whitespace
    std::string trimws_begin(std::string s)
    {
79
80
81
82
83
84
85
86
87
88
89
90
91
      if (s.length()>0)
      {
        string::size_type ib=s.find_first_not_of(" ", 0);
        if (ib==string::npos)
        {
          s="";
        }
        else if (ib!=0)
        {
          string::size_type n=s.length()-ib;
          s=s.substr(ib,n); 
        }
      }
thomas.forbriger's avatar
thomas.forbriger committed
92
      return(s);
93
94
95
    } // std::string trimws_begin(std::string s)

    /*----------------------------------------------------------------------*/
thomas.forbriger's avatar
thomas.forbriger committed
96
97
98
99
100
101
102
103
104
105

    // remove trailing whitespace
    // This is not as straight forward as trimws_begin as I did not manage to
    // get string::erase compiled with reverse iteraters. On the other hand
    // the only appropriate method to start a search at the end of the string
    // is to use a reverse iterator. Doing reverse twice IS ugly as it means
    // copying the string twice. Maybe I'll write some better code some
    // time... 
    std::string trimws_end(std::string s)
    {
106
107
108
109
110
111
      if (s.length()>0)
      {
        string::size_type il=s.find_last_not_of(" \r", s.length());
        string::size_type n=il>=0 ? il+1 : 0;
        if (n!=s.length()) { s=s.substr(0,n); }
      }
thomas.forbriger's avatar
thomas.forbriger committed
112
      return(s);
113
114
115
    } // std::string trimws_end(std::string s)

    /*----------------------------------------------------------------------*/
thomas.forbriger's avatar
thomas.forbriger committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

    /*! strip substring
     *
     * \param c character to find in string
     * \param delim delimiter to look for
     * \return number of c characters in string
     */
    std::string strip_string(std::string& s, const char& delim)
    {
      std::string::size_type i=s.find(delim);
      std::string result;
      if ((i>=0) && (i<s.size())) {
        result=s.substr(0,i);
        s.erase(0,i+1);
      } else {
        result=s;
        s.erase();
      }
      return(result);
    }

137
138
    /*----------------------------------------------------------------------*/

thomas.forbriger's avatar
thomas.forbriger committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    /*! count characters in string
     *
     * \param s string to look for c 
     * \param c character to find in string
     * \return number of c characters in string
     */
    int count_char(const std::string& s, const char& c)
    {
      int result=0;
      for (std::string::const_iterator i=s.begin(); i!=s.end(); i++)
      { if (*i == c) { result++; } }
      return(result);
    }

153
154
    /*----------------------------------------------------------------------*/

thomas.forbriger's avatar
thomas.forbriger committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    /*! replace pattern by string
     *
     * \param s string that contains pattern
     * \param p pattern string
     * \param r replacement for pattern
     * \return string \p s with all instances of pattern \p p 
     *                replaced by \p r
     */
    std::string patsubst(const std::string& s,
                         const std::string& p,
                         const std::string& r)
    {
      std::string retval=s;
      std::string::size_type n=retval.find(p);
      while (n !=std::string::npos)
      {
        retval.replace(n,p.length(),r);
        n=retval.find(p,n);
      }
      return(retval);
    }

thomas.forbriger's avatar
thomas.forbriger committed
177
178
179
180
181
  } // namespace string

} // namespace tfxx
 
/* ----- END OF stringfunc.cc ----- */