Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_MatrixMarket_split.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
43 #include <algorithm>
44 #include <cctype> // tolower
45 #include <utility> // std::pair
46 
47 
48 namespace Teuchos {
49  namespace MatrixMarket {
50  namespace details {
51 
52  std::string
53  trim (const std::string& in)
54  {
55  size_t start = in.find_first_not_of(" \t");
56  size_t end = in.find_last_not_of(" \t");
57  if (start == std::string::npos)
58  return std::string("");
59  else
60  return in.substr (start, end-start+1);
61  }
62 
63  std::string
64  lowercase (const std::string& in)
65  {
66  std::string out (in);
67  std::transform (in.begin(), in.end(), out.begin(), tolower);
68  return out;
69  }
70 
71  std::string
72  trim_and_lowercase (const std::string& in)
73  {
74  std::string out = trim (in);
75  std::string out2 (out);
76  std::transform (out.begin(), out.end(), out2.begin(), tolower);
77  return out2;
78  }
79 
81  static bool
82  endToken (const std::pair<size_t, size_t>& range)
83  {
84  return range.first == std::string::npos && range.second == std::string::npos;
85  }
86 
92  static std::pair<size_t, size_t>
93  nextToken (const std::string& str,
94  const std::string& delimiters,
95  const size_t start,
96  const size_t size)
97  {
98  using std::make_pair;
99  using std::string;
100 
101  if (start >= size)
102  return make_pair (string::npos, string::npos);
103 
104  // First index of a non-delimiter character
105  const size_t first = str.find_first_not_of (delimiters, start);
106  if (first == string::npos)
107  // There are only delimiter characters left
108  return make_pair (string::npos, string::npos);
109  else if (first == size-1)
110  // There's only one non-delimiter character left
111  return make_pair (first, 1);
112  else
113  { // Next index of a delimiter character
114  // Search for the next delimiting token from "first" (instead of
115  // "start+1" as originally coded and commented out below.)
116  // const size_t next = str.find_first_of (delimiters, start+1);
117  const size_t next = str.find_first_of (delimiters, first);
118  return make_pair (first, next - first);
119  }
120  }
121 
122  std::vector<std::string>
123  split (const std::string& str, const std::string& delimiters, const size_t start)
124  {
125  size_t curStart = start;
126  size_t size = str.size();
127  std::vector<std::string> tokens;
128  while (true) {
129  std::pair<size_t, size_t> token = nextToken (str, delimiters, curStart, size);
130  if (endToken (token)) {
131  break;
132  }
133  tokens.push_back (str.substr (token.first, token.second));
134  curStart = token.first + token.second;
135  }
136  return tokens;
137  }
138  } // namespace details
139  } // namespace MatrixMarket
140 } // namespace Teuchos
std::string lowercase(const std::string &in)
Return lowercase version of the given string.
std::vector< std::string > split(const std::string &str, const std::string &delimiters, const size_t start)
Split the given string using the given set of delimiters.
std::string trim(const std::string &in)
Trim whitespace from both sides of the given string.
static std::pair< size_t, size_t > nextToken(const std::string &str, const std::string &delimiters, const size_t start, const size_t size)
static bool endToken(const std::pair< size_t, size_t > &range)
Does range signify &quot;no more tokens&quot;?
std::string trim_and_lowercase(const std::string &in)
Trim whitespace from both sides, and make lowercase.