Subversion Repositories gelsvn

Rev

Rev 375 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 375 Rev 595
-
 
1
/* ----------------------------------------------------------------------- *
-
 
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
-
 
3
 * Copyright (C) the authors and DTU Informatics
-
 
4
 * For license and list of authors, see ../../doc/intro.pdf
-
 
5
 * ----------------------------------------------------------------------- */
-
 
6
 
1
#include <string>
7
#include <string>
2
#include <list>
8
#include <list>
3
#include "string_utils.h"
9
#include "string_utils.h"
4
 
10
 
5
using namespace std;
11
using namespace std;
6
 
12
 
7
namespace Util
13
namespace Util
8
{
14
{
9
  string trim(const string& s, const string& wspaces)
15
  string trim(const string& s, const string& wspaces)
10
  {
16
  {
11
    int front = 0;
17
    int front = 0;
12
    int back = static_cast<int>(s.size() - 1);
18
    int back = static_cast<int>(s.size() - 1);
13
    if(back < front)
19
    if(back < front)
14
      return string("");
20
      return string("");
15
 
21
 
16
    while(wspaces.find(s[front]) != wspaces.npos) ++front;
22
    while(wspaces.find(s[front]) != wspaces.npos) ++front;
17
    while(wspaces.find(s[back]) != wspaces.npos) --back;
23
    while(wspaces.find(s[back]) != wspaces.npos) --back;
18
    int n = back - front + 1;
24
    int n = back - front + 1;
19
    if(n < 1)
25
    if(n < 1)
20
      return string("");
26
      return string("");
21
    else
27
    else
22
      return s.substr(front, n);    
28
      return s.substr(front, n);    
23
  }
29
  }
24
 
30
 
25
  string trim(const string& s)
31
  string trim(const string& s)
26
  {
32
  {
27
    return trim(s, " \t\n\r");
33
    return trim(s, " \t\n\r");
28
  }
34
  }
29
 
35
 
30
  void split(const string& s, list<string>& result, const string& delim)
36
  void split(const string& s, list<string>& result, const string& delim)
31
  {
37
  {
32
    size_t begin = 0;
38
    size_t begin = 0;
33
    size_t end = s.find(delim);
39
    size_t end = s.find(delim);
34
    while(end != s.npos)
40
    while(end != s.npos)
35
    {
41
    {
36
      result.push_back(s.substr(begin, end - begin));
42
      result.push_back(s.substr(begin, end - begin));
37
      begin = end + delim.size();
43
      begin = end + delim.size();
38
      end = s.find(delim, begin);
44
      end = s.find(delim, begin);
39
    }
45
    }
40
    if(s.size() > begin)
46
    if(s.size() > begin)
41
      result.push_back(s.substr(begin, s.size() - begin));    
47
      result.push_back(s.substr(begin, s.size() - begin));    
42
  }
48
  }
43
 
49
 
44
  void split(const string& s, list<string>& result)
50
  void split(const string& s, list<string>& result)
45
  {
51
  {
46
    return split(s, result, " ");
52
    return split(s, result, " ");
47
  }
53
  }
48
 
54
 
49
  void trim_split(const string& s, list<string>& result, const string& delim)
55
  void trim_split(const string& s, list<string>& result, const string& delim)
50
  {
56
  {
51
    size_t begin = 0;
57
    size_t begin = 0;
52
    size_t end = s.find(delim);
58
    size_t end = s.find(delim);
53
    while(end != s.npos)
59
    while(end != s.npos)
54
    {
60
    {
55
      result.push_back(trim(s.substr(begin, end - begin)));
61
      result.push_back(trim(s.substr(begin, end - begin)));
56
      begin = end + delim.size();
62
      begin = end + delim.size();
57
      end = s.find(delim, begin);
63
      end = s.find(delim, begin);
58
    }
64
    }
59
    if(s.size() > begin)
65
    if(s.size() > begin)
60
      result.push_back(trim(s.substr(begin, s.size() - begin)));
66
      result.push_back(trim(s.substr(begin, s.size() - begin)));
61
  }
67
  }
62
 
68
 
63
  void trim_split(const string& s, list<string>& result)
69
  void trim_split(const string& s, list<string>& result)
64
  {
70
  {
65
    trim_split(s, result, " ");
71
    trim_split(s, result, " ");
66
  }
72
  }
67
 
73
 
68
  void get_first(string& s, string& first)
74
  void get_first(string& s, string& first)
69
  {
75
  {
70
    size_t pos = s.find(" ");
76
    size_t pos = s.find(" ");
71
    first = s.substr(0, pos);
77
    first = s.substr(0, pos);
72
    if(pos == s.npos)
78
    if(pos == s.npos)
73
      s = "";
79
      s = "";
74
    else
80
    else
75
      s.erase(0, pos + 1);
81
      s.erase(0, pos + 1);
76
  }
82
  }
77
 
83
 
78
  void get_last(string& s, string& last)
84
  void get_last(string& s, string& last)
79
  {
85
  {
80
    size_t pos = s.rfind(" ");
86
    size_t pos = s.rfind(" ");
81
    last = s.substr(pos + 1);
87
    last = s.substr(pos + 1);
82
    if(pos == s.npos)
88
    if(pos == s.npos)
83
      s = "";
89
      s = "";
84
    else
90
    else
85
      s.erase(pos + 1);
91
      s.erase(pos + 1);
86
  }
92
  }
87
}
93
}
88
 
94
 
89

Generated by GNU Enscript 1.6.6.
95

Generated by GNU Enscript 1.6.6.
90
 
96
 
91
 
97
 
92
 
98