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 <magic_enum.hpp>
12#include <string>
13#include <vector>
14
17namespace designlab::string_util {
18
25std::vector<std::string> Split(const std::string& str,
26 const std::string& delim);
27
51template <typename T>
52std::string EnumToStringRemoveTopK(const T& enum_value) {
53 // 型チェック Tが enum型であることをチェックする.
54 static_assert(std::is_enum<T>::value,
55 "引数は enum,あるいは enum classである必要があります.");
56
57 std::string str = static_cast<std::string>(magic_enum::enum_name(enum_value));
58
59 if (str.size() > 0 && str[0] == 'k') {
60 // 先頭のkを削除する.
61 str.erase(0, 1);
62 }
63
64 return str;
65}
66
71template <typename T>
72std::string EnumValuesToString(const std::string separator) {
73 // 型チェック Tが enum型であることをチェックする.
74 static_assert(std::is_enum<T>::value,
75 "引数は enum,あるいは enum classである必要があります.");
76
77 std::string str;
78 bool is_first = true;
79
80 for (const auto& e : magic_enum::enum_values<T>()) {
81 if (!is_first) {
82 str += separator;
83 } else {
84 is_first = false;
85 }
86
87 str += static_cast<std::string>(magic_enum::enum_name(e));
88 }
89
90 return str;
91}
92
99std::string EnumEntriesToString(const std::string separator) {
100 std::string str;
101 using enum_type = typename std::underlying_type<T>::type;
102 bool is_first = true;
103
104 for (const auto& e : magic_enum::enum_values<T>()) {
105 if (!is_first) {
106 str += separator;
107 } else {
108 is_first = false;
109 }
110
111 str += static_cast<std::string>(magic_enum::enum_name(e));
112 str += " = ";
113 str += std::to_string(static_cast<enum_type>(e));
114 }
115
116 return str;
117}
118
119template <typename T>
120std::string GetTypeName(const T& type) {
121 std::string str = typeid(type).name();
122
123 std::vector<std::string> eliminate{"class ", "struct ",
124 "designlab::", ",void"};
125
126 for (const auto& e : eliminate) {
127 while (true) {
128 auto pos = str.find(e);
129
130 if (pos == std::string::npos) {
131 break;
132 }
133
134 str.erase(pos, e.size());
135 }
136 }
137 return str;
138}
139
140} // namespace designlab::string_util
141
142#endif // DESIGNLAB_STRING_UTIL_H_
文字列操作に関する関数を提供する名前空間.
std::string EnumEntriesToString(const std::string separator)
enum型を渡すと,その要素と値を変換したものを列挙した文字列を返す関数.
Definition string_util.h:99
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:52
std::string EnumValuesToString(const std::string separator)
enum型を渡すと,その要素を列挙した文字列を返す関数.
Definition string_util.h:72