Panzer Version of the Day
Loading...
Searching...
No Matches
Panzer_String_Utilities.cpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Panzer: A partial differential equation assembly
4// engine for strongly coupled complex multiphysics systems
5//
6// Copyright 2011 NTESS and the Panzer contributors.
7// SPDX-License-Identifier: BSD-3-Clause
8// *****************************************************************************
9// @HEADER
10
12#include <sstream>
13
14namespace panzer {
15
16 void trim(std::string& str)
17 {
18 const std::string whitespace(" \t\n");
19
20 const auto strBegin = str.find_first_not_of(whitespace);
21 if (strBegin == std::string::npos) {
22 str = "";
23 return; // no content
24 }
25
26 const auto strEnd = str.find_last_not_of(whitespace);
27 const auto strRange = strEnd - strBegin + 1;
28
29 str = str.substr(strBegin, strRange);
30 return;
31 }
32
33 void StringTokenizer(std::vector<std::string>& tokens,
34 const std::string& str,
35 const std::string delimiters,bool trim)
36 {
37 using std::string;
38
39 // Skip delimiters at beginning.
40 string::size_type lastPos = str.find_first_not_of(delimiters, 0);
41 // Find first "non-delimiter".
42 string::size_type pos = str.find_first_of(delimiters, lastPos);
43
44 while (string::npos != pos || string::npos != lastPos) {
45
46 // grab token, trim if desired
47 std::string token = str.substr(lastPos,pos-lastPos);
48 if(trim)
49 panzer::trim(token);
50
51 // Found a token, add it to the vector.
52 tokens.push_back(token);
53
54 if(pos==string::npos)
55 break;
56
57 // Skip delimiters. Note the "not_of"
58 lastPos = str.find_first_not_of(delimiters, pos);
59 // Find next "non-delimiter"
60 pos = str.find_first_of(delimiters, lastPos);
61 }
62
63 }
64
65 void TokensToDoubles(std::vector<double> & values,const std::vector<std::string> & tokens)
66 {
67 // turn tokens into doubles (its a miracle!)
68 for(std::size_t i=0;i<tokens.size();i++) {
69 double value = 0.0;
70 std::stringstream ss;
71 ss << tokens[i];
72 ss >> value;
73
74 values.push_back(value);
75 }
76 }
77
78 void TokensToInts(std::vector<int> & values,const std::vector<std::string> & tokens)
79 {
80 // turn tokens into doubles (its a miracle!)
81 for(std::size_t i=0;i<tokens.size();i++) {
82 int value = 0;
83 std::stringstream ss;
84 ss << tokens[i];
85 ss >> value;
86
87 values.push_back(value);
88 }
89 }
90}
void TokensToInts(std::vector< int > &values, const std::vector< std::string > &tokens)
Turn a vector of tokens into a vector of ints.
void StringTokenizer(std::vector< std::string > &tokens, const std::string &str, const std::string delimiters, bool trim)
Tokenize a string, put tokens in a vector.
void trim(std::string &str)
Removes whitespace at beginning and end of string.
void TokensToDoubles(std::vector< double > &values, const std::vector< std::string > &tokens)
Turn a vector of tokens into a vector of doubles.