LCOV - code coverage report
Current view: top level - src/Util - String.H (source / functions) Coverage Total Hit
Test: coverage_merged.info Lines: 71.7 % 46 33
Test Date: 2025-08-05 17:56:56 Functions: - 0 0

            Line data    Source code
       1              : #ifndef UTIL_STRING_H
       2              : #define UTIL_STRING_H
       3              : 
       4              : #include <iostream>
       5              : #include <sys/ioctl.h>
       6              : #include <unistd.h>
       7              : #include <complex>
       8              : #include <stdlib.h>
       9              : 
      10              : #include <string>
      11              : 
      12              : #include "AMReX_Config.H"
      13              : #include "Set/Base.H"
      14              : #include "Util/Util.H"
      15              : 
      16              : /// \brief A collection of utility routines
      17              : namespace Util
      18              : {
      19              : /// \brief A collection of string operations
      20              : namespace String
      21              : {
      22              : AMREX_FORCE_INLINE
      23              : int ReplaceAll(std::string &str, const std::string before, const std::string after)
      24              : {
      25          682 :     size_t start_pos = 0;
      26          754 :     while((start_pos = str.find(before, start_pos)) != std::string::npos) {
      27           72 :         str.replace(start_pos, before.length(), after);
      28           72 :         start_pos += after.length();
      29              :     }
      30          682 :     return 0;
      31              : }
      32              : 
      33              : AMREX_FORCE_INLINE
      34              : int ReplaceAll(std::string &str, const char before, const std::string after)
      35              : {
      36         2686 :     size_t start_pos = 0;
      37         2686 :     while((start_pos = str.find(before, start_pos)) != std::string::npos) {
      38            0 :         str.replace(start_pos, 1, after);
      39            0 :         start_pos += after.length();
      40              :     }
      41         2686 :     return 0;
      42              : }
      43              : 
      44              : AMREX_FORCE_INLINE
      45              : std::string Wrap(std::string text, unsigned per_line)
      46              : {
      47              :     unsigned line_begin = 0;
      48              : 
      49              :     while (line_begin < text.size())
      50              :     {
      51              :         const unsigned ideal_end = line_begin + per_line ;
      52              :         unsigned line_end = ideal_end <= text.size() ? ideal_end : text.size()-1;
      53              : 
      54              :         if (line_end == text.size() - 1)
      55              :             ++line_end;
      56              :         else if (std::isspace(text[line_end]))
      57              :         {
      58              :             text[line_end] = '\n';
      59              :             ++line_end;
      60              :         }
      61              :         else    // backtrack
      62              :         {
      63              :             unsigned end = line_end;
      64              :             while ( end > line_begin && !std::isspace(text[end]))
      65              :                 --end;
      66              : 
      67              :             if (end != line_begin)                  
      68              :             {                                       
      69              :                 line_end = end;                     
      70              :                 text[line_end++] = '\n';            
      71              :             }                                       
      72              :             else                                    
      73              :                 text.insert(line_end++, 1, '\n');
      74              :         }
      75              : 
      76              :         line_begin = line_end;
      77              :     }
      78              : 
      79              :     return text;
      80              : }
      81              : 
      82              : AMREX_FORCE_INLINE
      83              : std::string Join(const std::vector<std::string> & vec, std::string separator = "_")
      84              : {
      85           20 :     std::ostringstream oss;
      86           60 :     for (size_t i = 0; i < vec.size(); ++i) {
      87           40 :         oss << vec[i];
      88           40 :         if (i < vec.size() - 1) { // Don't add separator after the last element
      89           20 :             oss << separator;
      90              :         }
      91              :     }
      92           20 :     return oss.str();
      93           20 : }
      94              : 
      95              : AMREX_FORCE_INLINE
      96              : std::string Join(const std::vector<std::string> & vec, char separator)
      97              : {
      98            0 :     return Join(vec,std::to_string(separator));
      99              : }
     100              : 
     101              : AMREX_FORCE_INLINE
     102              : std::string Join(const std::vector<double> & vec, std::string separator = " ")
     103              : {
     104              :     std::vector<std::string> strvec;
     105              :     for (auto v : vec) strvec.push_back(std::to_string(v));
     106              :     return Join(strvec,separator);
     107              : }
     108              : 
     109              : AMREX_FORCE_INLINE
     110              : std::string Join(const Set::Vector & vec, char separator = ' ')
     111              : {
     112              :     std::ostringstream oss;
     113              :     for (unsigned int i = 0; i < vec.size(); ++i) {
     114              :         oss << vec(i);
     115              :         if (i < vec.size() - 1) { // Don't add separator after the last element
     116              :             oss << separator;
     117              :         }
     118              :     }
     119              :     return oss.str();
     120              : }
     121              : 
     122              : AMREX_FORCE_INLINE
     123              : std::string Join(const Set::Matrix & vec, char separator = ' ')
     124              : {
     125            0 :     std::ostringstream oss;
     126            0 :     for (unsigned int i = 0; i < AMREX_SPACEDIM; ++i) {
     127            0 :         for (unsigned int j = 0; j < AMREX_SPACEDIM; ++j) {
     128            0 :             oss << vec(i,j);
     129            0 :             if (i < AMREX_SPACEDIM - 1 && j < AMREX_SPACEDIM - 1 ) { // Don't add separator after the last element
     130            0 :                 oss << separator;
     131              :             }
     132              :         }
     133              :     }
     134            0 :     return oss.str();
     135            0 : }
     136              : 
     137              : AMREX_FORCE_INLINE
     138              : std::vector<std::string> Split(std::string &str, const char delim = ' ')
     139              : {
     140          155 :     std::vector<std::string> ret;
     141          156 :     std::stringstream ss(str);
     142          156 :     std::string item;
     143          607 :     while (std::getline(ss, item, delim)) {
     144          451 :         ret.push_back(item);
     145              :     }
     146          312 :     return ret;
     147          156 : }
     148              : 
     149              : AMREX_FORCE_INLINE
     150              : bool Contains(std::string &str, const std::string find)
     151              : {
     152          897 :     if (str.find(find) != std::string::npos) return true;
     153          753 :     else return false;
     154              : }
     155              : 
     156              : template<class T>
     157              : AMREX_FORCE_INLINE
     158              : T Parse(std::string);
     159              : 
     160              : template<>
     161              : AMREX_FORCE_INLINE
     162              : std::complex<int> Parse(std::string input)
     163              : {
     164            1 :     int re=0, im=0;
     165              : 
     166            5 :     ReplaceAll(input, "+", " +");
     167            5 :     ReplaceAll(input, "-", " -");
     168              :     std::vector<std::string> tokens = Split(input,' ');
     169            2 :     for (unsigned int i = 0; i < tokens.size(); i++)
     170              :     {
     171            1 :         if(tokens[i]=="") continue;
     172            3 :         if(Contains(tokens[i],"i"))
     173              :         {
     174            0 :             ReplaceAll(tokens[i],"i","");
     175            0 :             im += std::stoi(tokens[i]);
     176              :         }
     177              :         else 
     178              :         {
     179            1 :             re += std::stoi(tokens[i]);
     180              :         }
     181              :     }
     182            2 :     return std::complex<int>(re,im);
     183            1 : }
     184              : 
     185              : }
     186              : 
     187              : }
     188              : #endif
        

Generated by: LCOV version 2.0-1