SST/macro
basic_string_tokenizer.h
Go to the documentation of this file.
1 /*
2  * This file is part of SST/macroscale:
3  * The macroscale architecture simulator from the SST suite.
4  * Copyright (c) 2009 Sandia Corporation.
5  * This software is distributed under the BSD License.
6  * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7  * the U.S. Government retains certain rights in this software.
8  * For more information, see the LICENSE file in the top
9  * SST/macroscale directory.
10  */
11 
12 // A bare-bones implementation of a -*-C++-*- std::string tokenizer.
13 // Based on a verison I found on the web some time ago - no licence attached.
14 
15 #ifndef SPROCKIT_COMMON_BASICSTRINGTOKENIZER_H_INCLUDED
16 #define SPROCKIT_COMMON_BASICSTRINGTOKENIZER_H_INCLUDED
17 
18 #include <string>
19 #include <deque>
20 
21 namespace sprockit {
22 inline std::string trim_str(const std::string& Src, const std::string& c =
23  " \r\n")
24 {
25  size_t p2 = Src.find_last_not_of(c);
26  if (p2 == std::string::npos) {
27  return std::string();
28  }
29  size_t p1 = Src.find_first_not_of(c);
30  if (p1 == std::string::npos) {
31  p1 = 0;
32  }
33  return Src.substr(p1, (p2 - p1) + 1);
34 }
35 }
36 namespace pst {
37 
38 /**
39  * This nested namespace contains basic functions for std::string parsing.
40  */
41 namespace BasicStringTokenizer {
42 
43 /**
44  * A utility function to tokenize a std::string.
45  * Original version gleamed off the web some years ago
46  * (no licensing or copyright information attached).
47  *
48  * Part of the Particle Simulation Toolkit (PST)
49  *
50  * Helgi
51  * Nov. 27, 2001.
52  */
53 
54 //typedef void (*parsefun_t)(const std::deque<std::string>&);
55 //typedef std::map<std::string, parsefun_t> CallMap_t;
56 
57 
58 template <class CharType, class Traits, class StrAlloc, class DqAlloc>
59 void tokenize
60 (const typename std::basic_string<CharType, Traits, StrAlloc>& str,
61  typename std::deque<
62  typename std::basic_string<CharType, Traits, StrAlloc>, DqAlloc
63  >& tok,
64  const typename std::basic_string<CharType, Traits, StrAlloc>& space
65  = " \t\n")
66 {
67  typedef typename std::basic_string<CharType, Traits, StrAlloc> StringType;
68 
69  typename StringType::size_type walk_pos = 0;
70 
71  while(walk_pos != StringType::npos && walk_pos < str.length()) {
72  typename StringType::size_type token_pos = 0, token_len = 0;
73  typename StringType::size_type delim_pos
74  = str.find_first_of( space, walk_pos );
75 
76  if( delim_pos == StringType::npos ) {
77  // no more spaces, a token starts at walk_pos
78  token_pos = walk_pos;
79  token_len = str.length() - token_pos;
80 
81  walk_pos += token_len;
82  }
83  else if( delim_pos > walk_pos ) {
84  // more tokens / delims left, but a token starts at walk_pos
85  token_pos = walk_pos;
86  token_len = delim_pos - token_pos;
87 
88  walk_pos = delim_pos;
89  }
90  else if( delim_pos == walk_pos ) {
91  // delimiters start at walk_pos
92  walk_pos = str.find_first_not_of( space, walk_pos );
93 
94  if( walk_pos == StringType::npos ) {
95  // only spaces left in str, no more tokens
96  break;
97  }
98  else {
99  // more tokens left
100  continue;
101  }
102  }
103 
104  tok.push_back( str.substr( token_pos, token_len ) );
105  }
106 }
107 
108 /**
109  * A utility function to trim spaces off a std::string.
110  * Taken from the same web page as the simple std::string tokenizer above.
111  *
112  * Part of the Particle Simulation Toolkit (PST)
113  *
114  * Helgi
115  * Nov. 27, 2001.
116  */
117 template <class CharType, class Traits, class Alloc>
118 typename std::basic_string<CharType, Traits, Alloc>
119 trim(const typename std::basic_string<CharType, Traits, Alloc>& s,
120  const typename std::basic_string<CharType, Traits, Alloc>& spaces
121  = " \t")
122 {
123  typedef typename std::basic_string<CharType, Traits, Alloc> StringType;
124 
125  if(s.length() == 0) {
126  return s;
127  }
128  int b = s.find_first_not_of(spaces);
129  int e = s.find_last_not_of(spaces);
130  if(b == -1) { // No non-spaces
131  return "";
132  }
133 
134  return StringType(s, b, e - b + 1);
135 }
136 
137 } // end of namespace pst::BasicStringTokenizer
138 
139 } // end of namespace pst
140 
141 #endif
142 
143 
void tokenize(const typename std::basic_string< CharType, Traits, StrAlloc > &str, typename std::deque< typename std::basic_string< CharType, Traits, StrAlloc >, DqAlloc > &tok, const typename std::basic_string< CharType, Traits, StrAlloc > &space=" \t\n")
A utility function to tokenize a std::string.
std::string trim_str(const std::string &Src, const std::string &c=" \r\n")
std::basic_string< CharType, Traits, Alloc > trim(const typename std::basic_string< CharType, Traits, Alloc > &s, const typename std::basic_string< CharType, Traits, Alloc > &spaces=" \t")
A utility function to trim spaces off a std::string.