Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
375 jrf 1
#include <string>
2
#include <list>
3
#include "string_utils.h"
4
 
5
using namespace std;
6
 
7
namespace Util
8
{
9
  string trim(const string& s, const string& wspaces)
10
  {
11
    int front = 0;
12
    int back = static_cast<int>(s.size() - 1);
13
    if(back < front)
14
      return string("");
15
 
16
    while(wspaces.find(s[front]) != wspaces.npos) ++front;
17
    while(wspaces.find(s[back]) != wspaces.npos) --back;
18
    int n = back - front + 1;
19
    if(n < 1)
20
      return string("");
21
    else
22
      return s.substr(front, n);    
23
  }
24
 
25
  string trim(const string& s)
26
  {
27
    return trim(s, " \t\n\r");
28
  }
29
 
30
  void split(const string& s, list<string>& result, const string& delim)
31
  {
32
    size_t begin = 0;
33
    size_t end = s.find(delim);
34
    while(end != s.npos)
35
    {
36
      result.push_back(s.substr(begin, end - begin));
37
      begin = end + delim.size();
38
      end = s.find(delim, begin);
39
    }
40
    if(s.size() > begin)
41
      result.push_back(s.substr(begin, s.size() - begin));    
42
  }
43
 
44
  void split(const string& s, list<string>& result)
45
  {
46
    return split(s, result, " ");
47
  }
48
 
49
  void trim_split(const string& s, list<string>& result, const string& delim)
50
  {
51
    size_t begin = 0;
52
    size_t end = s.find(delim);
53
    while(end != s.npos)
54
    {
55
      result.push_back(trim(s.substr(begin, end - begin)));
56
      begin = end + delim.size();
57
      end = s.find(delim, begin);
58
    }
59
    if(s.size() > begin)
60
      result.push_back(trim(s.substr(begin, s.size() - begin)));
61
  }
62
 
63
  void trim_split(const string& s, list<string>& result)
64
  {
65
    trim_split(s, result, " ");
66
  }
67
 
68
  void get_first(string& s, string& first)
69
  {
70
    size_t pos = s.find(" ");
71
    first = s.substr(0, pos);
72
    if(pos == s.npos)
73
      s = "";
74
    else
75
      s.erase(0, pos + 1);
76
  }
77
 
78
  void get_last(string& s, string& last)
79
  {
80
    size_t pos = s.rfind(" ");
81
    last = s.substr(pos + 1);
82
    if(pos == s.npos)
83
      s = "";
84
    else
85
      s.erase(pos + 1);
86
  }
87
}