File: StringUtil.hh

package info (click to toggle)
tango 7.2.6%2Bdfsg-14
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 20,720 kB
  • sloc: cpp: 122,899; sh: 11,304; ansic: 1,079; makefile: 843; java: 215; python: 55
file content (107 lines) | stat: -rw-r--r-- 3,547 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
//
// StringUtil.hh
//
// Copyright (C) :  2000 - 2002
//					LifeLine Networks BV (www.lifeline.nl). All rights reserved.
//					Bastiaan Bakker. All rights reserved.   
//					
//					2004,2005,2006,2007,2008,2009,2010
//					Synchrotron SOLEIL
//                	L'Orme des Merisiers
//                	Saint-Aubin - BP 48 - France
//
// This file is part of log4tango.
//
// Log4ango is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Log4tango 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 Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Log4Tango.  If not, see <http://www.gnu.org/licenses/>.

#ifndef _LOG4TANGO_STRINGUTIL_H
#define _LOG4TANGO_STRINGUTIL_H

#include "PortabilityImpl.hh"
#include <string>
#include <vector>
#include <climits>
#include <stdarg.h>

namespace log4tango {

class StringUtil 
{
public:

  /**
  Returns a string contructed from the a format specifier
  and a va_list of arguments, analogously to vprintf(3).
  @param format the format specifier.
  @param args the va_list of arguments.
  **/
  static std::string vform(const char* format, va_list args);

  /**
  Returns a string identical to the given string but without leading
  or trailing HTABs or spaces.
  **/
  static std::string trim(const std::string& s);

  /**
  splits a string into a vector of string segments based on the
  given delimiter.
  @param v The vector in which the segments will be stored. The vector
  will be emptied before storing the segments
  @param s The string to split into segments.
  @param delimiter The delimiter character
  @param maxSegments the maximum number of segments. Upon return
  v.size() <= maxSegments.  The string is scanned from left to right
  so v[maxSegments - 1] may contain a string containing the delimiter
  character.
  @return The actual number of segments (limited by maxSegments).
  **/
  static unsigned int split(std::vector<std::string>& v, 
				                    const std::string& s, char delimiter,
				                    unsigned int maxSegments = INT_MAX);

  /**
  splits a string into string segments based on the given delimiter
  and assigns the segments through an output_iterator.
  @param output The output_iterator through which to assign the string
  segments. Typically this will be a back_insertion_iterator.
  @param s The string to split into segments.
  @param delimiter The delimiter character
  @param maxSegments The maximum number of segments.
  @return The actual number of segments (limited by maxSegments).
  **/
  template<typename T> 
  static unsigned int split(T& output,
                            const std::string& s, char delimiter,
                            unsigned int maxSegments = INT_MAX) 
  {
    std::string::size_type left = 0;
    unsigned int i;
    for (i = 1; i < maxSegments; i++) {
      std::string::size_type right = s.find(delimiter, left);
      if (right == std::string::npos) {
          break;
      }
      *output++ = s.substr(left, right - left);
      left = right + 1;
    }
    *output++ = s.substr(left);
    return i;
  }
};

} // namespace log4tango

#endif // _LOG4TANGO_STRINGUTIL_HH