GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
string_util.h
[詳解]
1
3
4// Copyright(c) 2023-2025 Design Engineering Laboratory, Saitama University
5// Released under the MIT license
6// https://opensource.org/licenses/mit-license.php
7
8#ifndef DESIGNLAB_STRING_UTIL_H_
9#define DESIGNLAB_STRING_UTIL_H_
10
11#include <string>
12#include <vector>
13
14#include <magic_enum.hpp>
15
16
20{
21
28std::vector<std::string> Split(const std::string& str, const std::string& delim);
29
53template <typename T>
55{
56 // 型チェック Tが enum型であることをチェックする.
57 static_assert(std::is_enum<T>::value, "引数は enum,あるいは enum classである必要があります.");
58
59 std::string str = static_cast<std::string>(magic_enum::enum_name(enum_value));
60
61 if (str.size() > 0 && str[0] == 'k')
62 {
63 // 先頭のkを削除する.
64 str.erase(0, 1);
65 }
66
67 return str;
68}
69
74template <typename T>
75std::string EnumValuesToString(const std::string separator)
76{
77 // 型チェック Tが enum型であることをチェックする.
78 static_assert(std::is_enum<T>::value, "引数は enum,あるいは enum classである必要があります.");
79
80 std::string str;
81 bool is_first = true;
82
83 for (const auto& e : magic_enum::enum_values<T>())
84 {
85 if (!is_first)
86 {
87 str += separator;
88 }
89 else
90 {
91 is_first = false;
92 }
93
94 str += static_cast<std::string>(magic_enum::enum_name(e));
95 }
96
97 return str;
98}
99
100
106std::string EnumEntriesToString(const std::string separator)
107{
108 std::string str;
109 using enum_type = typename std::underlying_type<T>::type;
110 bool is_first = true;
111
112 for (const auto& e : magic_enum::enum_values<T>())
113 {
114 if (!is_first)
115 {
116 str += separator;
117 }
118 else
119 {
120 is_first = false;
121 }
122
123 str += static_cast<std::string>(magic_enum::enum_name(e));
124 str += " = ";
125 str += std::to_string(static_cast<enum_type>(e));
126 }
127
128 return str;
129}
130
131
132template <typename T>
133std::string GetTypeName(const T& type)
134{
135 std::string str = typeid(type).name();
136
137 std::vector<std::string> eliminate
138 {
139 "class ", "struct ", "designlab::", ",void"
140 };
141
142 for (const auto& e : eliminate)
143 {
144 while (true)
145 {
146 auto pos = str.find(e);
147
148 if (pos == std::string::npos)
149 {
150 break;
151 }
152
153 str.erase(pos, e.size());
154 }
155 }
156 return str;
157}
158
159} // namespace designlab::string_util
160
161
162#endif // DESIGNLAB_STRING_UTIL_H_
文字列操作に関する関数を提供する名前空間.
std::string EnumEntriesToString(const std::string separator)
enum型を渡すと,その要素と値を変換したものを列挙した文字列を返す関数.
std::string GetTypeName(const T &type)
std::vector< std::string > Split(const std::string &str, const std::string &separator)
文字列を分割する関数.指定した文字で文字列を分割する. 分割した結果,空白が含まれる場合や文字列がない場合は,そのまま返す. 最後が区切り文字で終わる場合は,それを無視する.
std::string EnumToStringRemoveTopK(const T &enum_value)
enumを文字列に変換する関数. Google C++ coding style だと enumの要素は 先頭にkをつけてキャメルケースで書くことが推奨されている. 例えば,
Definition string_util.h:54
std::string EnumValuesToString(const std::string separator)
enum型を渡すと,その要素を列挙した文字列を返す関数.
Definition string_util.h:75