Line data Source code
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 : { 23 : /** 24 : * Standard scripting language trim function 25 : */ 26 : inline std::string 27 668976768 : trim(const std::string & str, const std::string & white_space = " \t\n\v\f\r") 28 : { 29 668976768 : const auto begin = str.find_first_not_of(white_space); 30 668976768 : if (begin == std::string::npos) 31 63202065 : return ""; // no content 32 605774703 : const auto end = str.find_last_not_of(white_space); 33 605774703 : return str.substr(begin, end - begin + 1); 34 : } 35 : 36 : /** 37 : * This function will split the passed in string on a set of delimiters appending the substrings 38 : * to the passed in vector. The delimiters default to "/" but may be supplied as well. In 39 : * addition if min_len is supplied, the minimum token length will be >= than the supplied 40 : * value. T should be std::string or a MOOSE derived string class. 41 : */ 42 : template <typename T> 43 : void 44 713671742 : tokenize(const std::string & str, 45 : std::vector<T> & elements, 46 : unsigned int min_len = 1, 47 : const std::string & delims = "/") 48 : { 49 713671742 : elements.clear(); 50 : 51 713671742 : std::string::size_type last_pos = str.find_first_not_of(delims, 0); 52 713671742 : std::string::size_type pos = str.find_first_of(delims, std::min(last_pos + min_len, str.size())); 53 : 54 1373232290 : while (last_pos != std::string::npos) 55 : { 56 1367492152 : elements.push_back(str.substr(last_pos, pos - last_pos)); 57 : // skip delims between tokens 58 1367492152 : last_pos = str.find_first_not_of(delims, pos); 59 1367492152 : if (last_pos == std::string::npos) 60 707931604 : break; 61 659560548 : pos = str.find_first_of(delims, std::min(last_pos + min_len, str.size())); 62 : } 63 713671742 : } 64 : 65 : /** 66 : * tokenizeAndConvert splits a string using delimiter and then converts to type T. 67 : * If the conversion fails tokenizeAndConvert returns false, otherwise true. 68 : */ 69 : template <typename T> 70 : bool 71 174489 : tokenizeAndConvert(const std::string & str, 72 : std::vector<T> & tokenized_vector, 73 : const std::string & delimiter = " \t\n\v\f\r") 74 : { 75 174489 : std::vector<std::string> tokens; 76 174489 : MooseUtils::tokenize(str, tokens, 1, delimiter); 77 174489 : tokenized_vector.resize(tokens.size()); 78 835352 : for (unsigned int j = 0; j < tokens.size(); ++j) 79 : { 80 330549 : 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 330549 : if ((ss >> tokenized_vector[j]).fail() || !ss.eof()) 84 235 : return false; 85 : } 86 174254 : return true; 87 174489 : } 88 : 89 : /** 90 : * Convert supplied string to upper case. 91 : * @params name The string to convert upper case. 92 : */ 93 : inline std::string 94 1033661443 : toUpper(const std::string & name) 95 : { 96 1033661443 : std::string upper(name); 97 1033661443 : std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper); 98 1033661443 : return upper; 99 : } 100 : 101 : /** 102 : * Convert supplied string to lower case. 103 : * @params name The string to convert upper case. 104 : */ 105 : inline std::string 106 3524985 : toLower(const std::string & name) 107 : { 108 3524985 : std::string lower(name); 109 3524985 : std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower); 110 3524985 : return lower; 111 : } 112 : 113 : }