https://mooseframework.inl.gov
MooseStringUtils.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include <algorithm>
13 #include <sstream>
14 #include <string>
15 #include <vector>
16 
17 /*
18  * This must stay a header-only utility! It is used in the capabilities python module and
19  * we do not want to link against any MOOSE libs.
20  */
21 namespace MooseUtils
22 {
26 inline std::string
27 trim(const std::string & str, const std::string & white_space = " \t\n\v\f\r")
28 {
29  const auto begin = str.find_first_not_of(white_space);
30  if (begin == std::string::npos)
31  return ""; // no content
32  const auto end = str.find_last_not_of(white_space);
33  return str.substr(begin, end - begin + 1);
34 }
35 
42 template <typename T>
43 void
44 tokenize(const std::string & str,
45  std::vector<T> & elements,
46  unsigned int min_len = 1,
47  const std::string & delims = "/")
48 {
49  elements.clear();
50 
51  std::string::size_type last_pos = str.find_first_not_of(delims, 0);
52  std::string::size_type pos = str.find_first_of(delims, std::min(last_pos + min_len, str.size()));
53 
54  while (last_pos != std::string::npos)
55  {
56  elements.push_back(str.substr(last_pos, pos - last_pos));
57  // skip delims between tokens
58  last_pos = str.find_first_not_of(delims, pos);
59  if (last_pos == std::string::npos)
60  break;
61  pos = str.find_first_of(delims, std::min(last_pos + min_len, str.size()));
62  }
63 }
64 
69 template <typename T>
70 bool
71 tokenizeAndConvert(const std::string & str,
72  std::vector<T> & tokenized_vector,
73  const std::string & delimiter = " \t\n\v\f\r")
74 {
75  std::vector<std::string> tokens;
76  MooseUtils::tokenize(str, tokens, 1, delimiter);
77  tokenized_vector.resize(tokens.size());
78  for (unsigned int j = 0; j < tokens.size(); ++j)
79  {
80  std::stringstream ss(trim(tokens[j]));
81  // we have to make sure that the conversion succeeded _and_ that the string
82  // was fully read to avoid situations like [conversion to Real] 3.0abc to work
83  if ((ss >> tokenized_vector[j]).fail() || !ss.eof())
84  return false;
85  }
86  return true;
87 }
88 
93 inline std::string
94 toUpper(const std::string & name)
95 {
96  std::string upper(name);
97  std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
98  return upper;
99 }
100 
105 inline std::string
106 toLower(const std::string & name)
107 {
108  std::string lower(name);
109  std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
110  return lower;
111 }
112 
113 }
std::string name(const ElemQuality q)
std::string toLower(const std::string &name)
Convert supplied string to lower case.
void tokenize(const std::string &str, std::vector< T > &elements, unsigned int min_len=1, const std::string &delims="/")
This function will split the passed in string on a set of delimiters appending the substrings to the ...
std::string toUpper(const std::string &name)
Convert supplied string to upper case.
bool tokenizeAndConvert(const std::string &str, std::vector< T > &tokenized_vector, const std::string &delimiter=" \\\)
tokenizeAndConvert splits a string using delimiter and then converts to type T.
std::string trim(const std::string &str, const std::string &white_space=" \\\)
Standard scripting language trim function.
charT const * delimiter
Definition: InfixIterator.h:34
auto min(const L &left, const R &right)