File: StringUtils.cc

package info (click to toggle)
thepeg 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 9,312 kB
  • ctags: 11,509
  • sloc: cpp: 57,129; sh: 11,315; java: 3,212; lisp: 1,402; makefile: 830; ansic: 58; perl: 3
file content (142 lines) | stat: -rw-r--r-- 3,868 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
// -*- C++ -*-
//
// StringUtils.cc is a part of ThePEG - Toolkit for HEP Event Generation
// Copyright (C) 1999-2011 Leif Lonnblad
//
// ThePEG is licenced under version 2 of the GPL, see COPYING for details.
// Please respect the MCnet academic guidelines, see GUIDELINES for details.
//
//
// This is the implementation of the non-inlined, non-templated member
// functions of the StringUtils class.
//

#include "StringUtils.h"
#include "ThePEG/Interface/ClassDocumentation.h"

using namespace ThePEG;

StringUtils::StringVector StringUtils::split(string s, string ws) {
  StringVector r;
  string::size_type i = s.find_first_not_of(ws);
  while ( i != string::npos ) {
    s = s.substr(i);
    i = s.find_first_of(ws);
    r.push_back(s.substr(0, i));
    if ( i == string::npos ) return r;
    s = s.substr(i);
    i = s.find_first_not_of(ws);
  }
  return r;
}

string StringUtils::car(string s, string ws) {
  string::size_type p = s.find_first_not_of(ws);
  if ( p == string::npos ) return "";
  s = s.substr(p);
  return s.substr(0, s.find_first_of(ws));
}

string StringUtils::cdr(string s, string ws) {
  string::size_type p = s.find_first_not_of(ws);
  if ( p == string::npos ) return "";
  s = s.substr(p);
  p = s.find_first_of(ws);
  if ( p == string::npos ) return "";
  s = s.substr(p);
  p = s.find_first_not_of(ws);
  if ( p == string::npos ) return "";
  return s.substr(p);
}

string StringUtils::stripws(string str) {
  string::size_type i = str.find_first_not_of(" \t\r\n");
  if ( i != string::npos ) str = str.substr(i);
  i = str.find_last_not_of(" \t\r\n");
  return str.substr(0, i + 1);
}

map<string,string> StringUtils::
xmlAttributes(string tag, string line, string::size_type curr) {

  map<string,string> ret;

  // First find the beginning and end of the tag
  pos_t begin = line.find("<" + tag, curr);
  if ( begin == end ) return ret;
  curr = begin + tag.length() + 2;

  while ( true ) {

    pos_t close = line.find(">", curr);

    // Now skip some white space to see if we can find an attribute.
    curr = line.find_first_not_of(" \t\r\n", curr);
    if ( curr == end || curr >= close ) return ret;

    pos_t tend = line.find_first_of("= \t\r\n", curr);
    if ( tend == end || tend >= close ) return ret;

    string name = line.substr(curr, tend - curr);
    curr = line.find("=", curr) + 1;

    // OK now find the beginning and end of the atribute.
    curr = line.find("\"", curr);
    if ( curr == end || curr >= close ) return ret;
    pos_t bega = ++curr;
    curr = line.find("\"", curr);
    while ( curr != end && line[curr - 1] == '\\' )
      curr = line.find("\"", curr + 1);

    string value = line.substr(bega, curr == end? end: curr - bega);

    ret[name] = value;

    ++curr;

  }
}


string StringUtils::dirname(string file) {
  string::size_type pos = file.rfind('/');
  if ( pos == string::npos ) return "";
  return file.substr(0,pos);
}

string StringUtils::basename(string file) {
  string::size_type pos = file.rfind('/');
  if ( pos == string::npos ) return file;
  return file.substr(pos + 1);
}

string StringUtils::remsuf(string file) {
  string::size_type pos = file.rfind('.');
  if ( pos == string::npos ) return "";
  return file.substr(0,pos);
}

string StringUtils::suffix(string file) {
  string::size_type pos = file.rfind('.');
  if ( pos == string::npos ) return file;
  return file.substr(pos + 1);
}

string StringUtils::typeName(const type_info & t) {
  string ret;
  string name = t.name();
  if ( name[0] != 'N' ) return name;

  string::size_type pos = 1;
  while ( pos < name.length() && name[pos] != 'E' ) {
    int size = 0;
    while ( pos < name.length() && isdigit(name[pos]) )
      size = size*10 + int(name[pos++] - '0');
    if ( size == 0 ) return name;
    if ( !ret.empty() ) ret += "::";
    ret += name.substr(pos, size);
    pos += size;
  }
  return ret;
}