Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_MatrixMarket_split.cpp
1// @HEADER
2// *****************************************************************************
3// Tpetra: Templated Linear Algebra Services Package
4//
5// Copyright 2008 NTESS and the Tpetra contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#include "Teuchos_MatrixMarket_split.hpp"
11#include <algorithm>
12#include <cctype> // tolower
13#include <utility> // std::pair
14
15
16namespace Teuchos {
17 namespace MatrixMarket {
18 namespace details {
19
20 std::string
21 trim (const std::string& in)
22 {
23 size_t start = in.find_first_not_of(" \t");
24 size_t end = in.find_last_not_of(" \t");
25 if (start == std::string::npos)
26 return std::string("");
27 else
28 return in.substr (start, end-start+1);
29 }
30
31 std::string
32 lowercase (const std::string& in)
33 {
34 std::string out (in);
35 std::transform (in.begin(), in.end(), out.begin(), tolower);
36 return out;
37 }
38
39 std::string
40 trim_and_lowercase (const std::string& in)
41 {
42 std::string out = trim (in);
43 std::string out2 (out);
44 std::transform (out.begin(), out.end(), out2.begin(), tolower);
45 return out2;
46 }
47
49 static bool
50 endToken (const std::pair<size_t, size_t>& range)
51 {
52 return range.first == std::string::npos && range.second == std::string::npos;
53 }
54
60 static std::pair<size_t, size_t>
61 nextToken (const std::string& str,
62 const std::string& delimiters,
63 const size_t start,
64 const size_t size)
65 {
66 using std::make_pair;
67 using std::string;
68
69 if (start >= size)
70 return make_pair (string::npos, string::npos);
71
72 // First index of a non-delimiter character
73 const size_t first = str.find_first_not_of (delimiters, start);
74 if (first == string::npos)
75 // There are only delimiter characters left
76 return make_pair (string::npos, string::npos);
77 else if (first == size-1)
78 // There's only one non-delimiter character left
79 return make_pair (first, 1);
80 else
81 { // Next index of a delimiter character
82 // Search for the next delimiting token from "first" (instead of
83 // "start+1" as originally coded and commented out below.)
84 // const size_t next = str.find_first_of (delimiters, start+1);
85 const size_t next = str.find_first_of (delimiters, first);
86 return make_pair (first, next - first);
87 }
88 }
89
90 std::vector<std::string>
91 split (const std::string& str, const std::string& delimiters, const size_t start)
92 {
93 size_t curStart = start;
94 size_t size = str.size();
95 std::vector<std::string> tokens;
96 while (true) {
97 std::pair<size_t, size_t> token = nextToken (str, delimiters, curStart, size);
98 if (endToken (token)) {
99 break;
100 }
101 tokens.push_back (str.substr (token.first, token.second));
102 curStart = token.first + token.second;
103 }
104 return tokens;
105 }
106 } // namespace details
107 } // namespace MatrixMarket
108} // namespace Teuchos
Matrix Market file utilities.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
Teuchos implementation details.