Line data Source code
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 : #include <c10/util/Type.h>
26 :
27 : #include "neml2/misc/string_utils.h"
28 :
29 : namespace neml2::utils
30 : {
31 : std::string
32 485869 : demangle(const char * name)
33 : {
34 : // c10 already has an implementation, let's not reinvent the wheels
35 485869 : return c10::demangle(name);
36 : }
37 :
38 : std::string
39 0 : join(const std::vector<std::string> & strs, const std::string & delim)
40 : {
41 0 : std::string result;
42 0 : for (size_t i = 0; i < strs.size(); i++)
43 : {
44 0 : result += strs[i];
45 0 : if (i < strs.size() - 1)
46 0 : result += delim;
47 : }
48 0 : return result;
49 0 : }
50 :
51 : std::vector<std::string>
52 4796 : split(const std::string & str, const std::string & delims)
53 : {
54 4796 : std::vector<std::string> tokens;
55 :
56 4796 : std::string::size_type last_pos = str.find_first_not_of(delims, 0);
57 4796 : std::string::size_type pos = str.find_first_of(delims, std::min(last_pos + 1, str.size()));
58 :
59 11790 : while (last_pos != std::string::npos)
60 : {
61 11771 : tokens.push_back(str.substr(last_pos, pos - last_pos));
62 : // skip delims between tokens
63 11771 : last_pos = str.find_first_not_of(delims, pos);
64 11771 : if (last_pos == std::string::npos)
65 4777 : break;
66 6994 : pos = str.find_first_of(delims, std::min(last_pos + 1, str.size()));
67 : }
68 :
69 4796 : return tokens;
70 0 : }
71 :
72 : std::string
73 16244 : trim(const std::string & str, const std::string & white_space)
74 : {
75 16244 : const auto begin = str.find_first_not_of(white_space);
76 16244 : if (begin == std::string::npos)
77 36 : return ""; // no content
78 16226 : const auto end = str.find_last_not_of(white_space);
79 16226 : return str.substr(begin, end - begin + 1);
80 : }
81 :
82 : bool
83 36948 : start_with(std::string_view str, std::string_view prefix)
84 : {
85 36948 : return str.size() >= prefix.size() && 0 == str.compare(0, prefix.size(), prefix);
86 : }
87 :
88 : bool
89 1983 : end_with(std::string_view str, std::string_view suffix)
90 : {
91 3966 : return str.size() >= suffix.size() &&
92 3966 : 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix);
93 : }
94 : } // namespace neml2::utils
|