LCOV - code coverage report
Current view: top level - include/utils - MooseStringUtils.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 35 35 100.0 %
Date: 2025-07-17 01:28:37 Functions: 33 45 73.3 %
Legend: Lines: hit not hit

          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   642810000 : trim(const std::string & str, const std::string & white_space = " \t\n\v\f\r")
      28             : {
      29   642810000 :   const auto begin = str.find_first_not_of(white_space);
      30   642810000 :   if (begin == std::string::npos)
      31    60677072 :     return ""; // no content
      32   582132928 :   const auto end = str.find_last_not_of(white_space);
      33   582132928 :   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   675372122 : tokenize(const std::string & str,
      45             :          std::vector<T> & elements,
      46             :          unsigned int min_len = 1,
      47             :          const std::string & delims = "/")
      48             : {
      49   675372122 :   elements.clear();
      50             : 
      51   675372122 :   std::string::size_type last_pos = str.find_first_not_of(delims, 0);
      52   675372122 :   std::string::size_type pos = str.find_first_of(delims, std::min(last_pos + min_len, str.size()));
      53             : 
      54  1297941533 :   while (last_pos != std::string::npos)
      55             :   {
      56  1292525999 :     elements.push_back(str.substr(last_pos, pos - last_pos));
      57             :     // skip delims between tokens
      58  1292525999 :     last_pos = str.find_first_not_of(delims, pos);
      59  1292525999 :     if (last_pos == std::string::npos)
      60   669956588 :       break;
      61   622569411 :     pos = str.find_first_of(delims, std::min(last_pos + min_len, str.size()));
      62             :   }
      63   675372122 : }
      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      159973 : tokenizeAndConvert(const std::string & str,
      72             :                    std::vector<T> & tokenized_vector,
      73             :                    const std::string & delimiter = " \t\n\v\f\r")
      74             : {
      75      159973 :   std::vector<std::string> tokens;
      76      159973 :   MooseUtils::tokenize(str, tokens, 1, delimiter);
      77      159973 :   tokenized_vector.resize(tokens.size());
      78      765314 :   for (unsigned int j = 0; j < tokens.size(); ++j)
      79             :   {
      80      302779 :     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      302779 :     if ((ss >> tokenized_vector[j]).fail() || !ss.eof())
      84         217 :       return false;
      85             :   }
      86      159756 :   return true;
      87      159973 : }
      88             : 
      89             : /**
      90             :  * Convert supplied string to upper case.
      91             :  * @params name The string to convert upper case.
      92             :  */
      93             : inline std::string
      94   977156226 : toUpper(const std::string & name)
      95             : {
      96   977156226 :   std::string upper(name);
      97   977156226 :   std::transform(upper.begin(), upper.end(), upper.begin(), ::toupper);
      98   977156226 :   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     3269309 : toLower(const std::string & name)
     107             : {
     108     3269309 :   std::string lower(name);
     109     3269309 :   std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
     110     3269309 :   return lower;
     111             : }
     112             : 
     113             : }

Generated by: LCOV version 1.14