NEML2 2.0.0
Loading...
Searching...
No Matches
string_utils.h
1// Copyright 2024, UChicago Argonne, LLC
2// All Rights Reserved
3// Software Name: NEML2 -- the New Engineering material Model Library, version 2
4// By: Argonne National Laboratory
5// OPEN SOURCE LICENSE (MIT)
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23// THE SOFTWARE.
24
25#pragma once
26
27#include <string>
28#include <vector>
29#include <sstream>
30
31namespace neml2::utils
32{
33template <typename T, typename... Args>
34void stream_all(std::ostringstream & ss, T && val, Args &&... args);
35
37std::string demangle(const char * name);
38
39template <typename T>
40std::string stringify(const T & t);
41
42std::string join(const std::vector<std::string> & strs, const std::string & delim);
43
44std::vector<std::string> split(const std::string & str, const std::string & delims);
45
46std::string trim(const std::string & str, const std::string & white_space = " \t\n\v\f\r");
47
48bool start_with(std::string_view str, std::string_view prefix);
49
50bool end_with(std::string_view str, std::string_view suffix);
51} // namespace neml2::utils
52
54// Implementation
56
57namespace neml2::utils
58{
59template <typename T, typename... Args>
60void
61stream_all(std::ostringstream & ss, T && val, Args &&... args)
62{
63 ss << val;
64 if constexpr (sizeof...(args) > 0)
65 stream_all(ss, std::forward<Args>(args)...);
66}
67
68template <typename T>
69std::string
70stringify(const T & t)
71{
72 std::ostringstream os;
73 os << t;
74 return os.str();
75}
76
77template <>
78inline std::string
79stringify(const bool & t)
80{
81 return t ? "true" : "false";
82}
83}
Definition Parser.cxx:36
std::string trim(const std::string &str, const std::string &white_space)
Definition string_utils.cxx:73
std::string stringify(const T &t)
Definition string_utils.h:70
std::string demangle(const char *name)
Demangle a piece of cxx abi type information.
Definition string_utils.cxx:32
std::string join(const std::vector< std::string > &strs, const std::string &delim)
Definition string_utils.cxx:39
std::vector< std::string > split(const std::string &str, const std::string &delims)
Definition string_utils.cxx:52
bool start_with(std::string_view str, std::string_view prefix)
Definition string_utils.cxx:83
bool end_with(std::string_view str, std::string_view suffix)
Definition string_utils.cxx:89
void stream_all(std::ostringstream &ss, T &&val, Args &&... args)
Definition string_utils.h:61
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30