NEML2 2.0.0
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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
31#include "neml2/misc/errors.h"
32#include "neml2/misc/types.h"
33
34namespace neml2::utils
35{
36template <typename T, typename... Args>
37void stream_all(std::ostringstream & ss, T && val, Args &&... args);
38
40std::string demangle(const char * name);
41
42template <typename T>
43std::string stringify(const T & t);
44
45std::string join(const std::vector<std::string> & strs, const std::string & delim);
46
47std::vector<std::string> split(const std::string & str, const std::string & delims);
48
49std::string trim(const std::string & str, const std::string & white_space = " \t\n\v\f\r");
50
51bool start_with(std::string_view str, std::string_view prefix);
52
53bool end_with(std::string_view str, std::string_view suffix);
54} // namespace neml2::utils
55
57// Implementation
59
60namespace neml2::utils
61{
62template <typename T, typename... Args>
63void
64stream_all(std::ostringstream & ss, T && val, Args &&... args)
65{
66 ss << val;
67 if constexpr (sizeof...(args) > 0)
68 stream_all(ss, std::forward<Args>(args)...);
69}
70
71template <typename T>
72std::string
73stringify(const T & t)
74{
75 std::ostringstream os;
76 os << t;
77 return os.str();
78}
79
80template <>
81inline std::string
82stringify(const bool & t)
83{
84 return t ? "true" : "false";
85}
86}
Definition Parser.cxx:35
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:73
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:64
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30