Loading...
Searching...
No Matches
ITHACAstring.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ██╗████████╗██╗ ██╗ █████╗ ██████╗ █████╗ ███████╗██╗ ██╗
3 ██║╚══██╔══╝██║ ██║██╔══██╗██╔════╝██╔══██╗ ██╔════╝██║ ██║
4 ██║ ██║ ███████║███████║██║ ███████║█████╗█████╗ ██║ ██║
5 ██║ ██║ ██╔══██║██╔══██║██║ ██╔══██║╚════╝██╔══╝ ╚██╗ ██╔╝
6 ██║ ██║ ██║ ██║██║ ██║╚██████╗██║ ██║ ██║ ╚████╔╝
7 ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═╝ ╚═══╝
8
9 * In real Time Highly Advanced Computational Applications for Finite Volumes
10 * Copyright (C) 2017 by the ITHACA-FV authors
11-------------------------------------------------------------------------------
12
13License
14 This file is part of ITHACA-FV
15
16 ITHACA-FV is free software: you can redistribute it and/or modify
17 it under the terms of the GNU Lesser General Public License as published by
18 the Free Software Foundation, either version 3 of the License, or
19 (at your option) any later version.
20
21 ITHACA-FV is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU Lesser General Public License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with ITHACA-FV. If not, see <http://www.gnu.org/licenses/>.
28\*---------------------------------------------------------------------------*/
29
30#include "ITHACAstring.H"
31
34
35// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
36
37namespace ITHACAutilities
38{
39
40std::string str_trim(std::string const& s)
41{
42 auto const first{ s.find_first_not_of(' ') };
43 if (first == std::string::npos) return {};
44 auto const last{ s.find_last_not_of(' ') };
45 return s.substr(first, (last - first + 1));
46}
47
48// io format function for files name
49void str_format_io(std::string const& s, unsigned int nMax)
50{
51 if ( nMax > s.length() )
52 {
53 for (unsigned int n=0; n<s.length(); n++) std::cout << s[n];
54 for (unsigned int n=s.length(); n<nMax; n++) std::cout << " ";
55 }
56 else
57 {
58 for (unsigned int n=0; n<nMax-3; n++) std::cout << s[n];
59 for (unsigned int n=nMax-3; n<nMax; n++) std::cout << ".";
60 }
61}
62
63std::string double2ConciseString(const double& d)
64{
65 std::string s = std::to_string(d);
66
67 // Removing trailling zeros
68 int dot_pos = s.find_first_of('.');
69 if(dot_pos != std::string::npos)
70 {
71 int ipos = s.size()-1;
72 while((s[ipos]=='0' || s[ipos]=='.') && ipos>dot_pos-1)
73 {
74 --ipos;
75 }
76 s.erase(ipos + 1, std::string::npos);
77 }
78 return s;
79}
80
81bool containsSubstring(std::string contain, std::string contained)
82{
83 std::transform(contain.begin(), contain.end(), contain.begin(), ::tolower);
84 std::transform(contained.begin(), contained.end(), contained.begin(), ::tolower);
85 return contain.find(contained) != std::string::npos;
86}
87
88std::vector<int> extractIntFromString(std::string input)
89{
90 std::stringstream ss;
91 std::vector<int> numbers;
92 int num;
93
94 for (char c : input)
95 {
96 if (isdigit(c)) ss << c;
97 else ss << ' ';
98 }
99 while (ss >> num)
100 numbers.push_back(num);
101 return numbers;
102}
103
104}
Header file of the ITHACAstring file.
Namespace to implement some useful assign operation of OF fields.
std::string str_trim(std::string const &s)
Trim a string cf.
bool containsSubstring(std::string contain, std::string contained)
Returns true if contained is a substring of contain, false otherwise.
std::string double2ConciseString(const double &d)
Returns the double d in string format without keeping all the extra 0.
void str_format_io(std::string const &s, unsigned int nMax)
Format a string.
std::vector< int > extractIntFromString(std::string input)
Returns an array storing all the integers in the input string.