🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
ValueView.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "AstUtil/StringView.hpp"
25#include "AstUtil/ParseFormat.hpp"
26#include "AstUtil/Color.hpp"
27#include "AstUtil/GenericValue.hpp"
28#include "AstUtil/StringSplit.hpp"
29#include "AstUtil/StringUtil.hpp"
30#include "AstUtil/Math.hpp"
31
32AST_NAMESPACE_BEGIN
33
34class ByCommaAndRepeatedWhitespace;
35struct SkipBracket;
36
41{
42public:
43 ValueView() = default;
44 // explicit
46 : value_(value){}
47 explicit ValueView(const char* value)
48 : value_(value){}
49 // explicit
50 ValueView(const std::string& value)
51 : value_(value){}
52 // explicit
53 ValueView(const GenericValue& value)
54 : value_(value.value()) {}
55 ValueView(std::nullptr_t)
56 : value_() {}
57
59 const StringView& value() const { return value_; }
60 StringView& value() { return value_; }
61
63 int toInt() const { return aParseInt(value_); }
64 errc_t toInt(int& value) const { return aParseInt(value_, value); }
65
67 double toDouble() const { return aParseDouble(value_); }
68 errc_t toDouble(double& value) const { return aParseDouble(value_, value); }
69
72 angle_d toAngleRad() const {return deg2rad(toDouble());}
73 errc_t toAngleRad(angle_d& value) const {errc_t rc= aParseDouble(value_, value); value=deg2rad(value); return rc;}
74
76 double toFortranDouble() const { return aParseFortranDouble(value_); }
77 errc_t toFortranDouble(double& value) const { return aParseFortranDouble(value_, value); }
78
80 bool toBool() const { return aParseBool(value_); }
81 errc_t toBool(bool& value) const { return aParseBool(value_, value); }
82
84 Color toColor() const { return aParseColor(value_); }
85 errc_t toColor(Color& value) const { return aParseColor(value_, value); }
86
88 std::string toString() const { return std::string(value_); }
89 void toString(std::string& value) const { value = std::string(value_); }
90
92 std::vector<ValueView> toVector() const;
93 void toVector(std::vector<ValueView>& value) const;
94
96 std::vector<double> toFortranDoubleVector() const;
97 errc_t toFortranDoubleVector(std::vector<double>& value) const;
98
100 std::vector<double> toDoubleVector() const;
101 errc_t toDoubleVector(std::vector<double>& value) const;
102
104 std::vector<int> toIntVector() const;
105 errc_t toIntVector(std::vector<int>& value) const;
106
108 const StringView& toStringView() const { return value_; }
109
111 GenericValue toValue() const { return GenericValue(value_); }
112
113 operator StringView() const { return value_; }
114
115 const char* data() const { return value_.data(); }
116 size_t size() const { return value_.size(); }
117
118 using SplitterType = Splitter<ByCommaAndRepeatedWhitespace, SkipBracket>;
119 SplitterType split() const;
120
121 template<typename Delimiter>
122 inline strings_internal::Splitter<typename strings_internal::SelectDelimiter<Delimiter>::type, strings_internal::AllowEmpty, StringView>
123 split(Delimiter delimiter) const {
124 using DelimiterType = typename strings_internal::SelectDelimiter<Delimiter>::type;
125 return strings_internal::Splitter<DelimiterType, strings_internal::AllowEmpty, StringView>(
126 value_, DelimiterType(delimiter), strings_internal::AllowEmpty());
127 }
128public:
129 StringView value_;
130};
131
132
133inline std::vector<ValueView> ValueView::toVector() const
134{
135 std::vector<ValueView> value;
136 this->toVector(value);
137 return value;
138}
139
140
143public:
144 StringView Find(StringView text, size_t pos) const {
145 while (pos < text.size()) {
146 char c = text[pos];
147 // 情况1:当前字符是空白
148 if (std::isspace(static_cast<unsigned char>(c))) {
149 size_t whitespace_start = pos;
150 // 跳过连续空白
151 while (pos < text.size() && std::isspace(static_cast<unsigned char>(text[pos]))) {
152 ++pos;
153 }
154 // 检查空白之后是否是逗号
155 if (pos < text.size() && text[pos] == ',') {
156 // 空白后紧跟逗号,继续跳过逗号后的空白
157 ++pos; // 跳过逗号
158 while (pos < text.size() && std::isspace(static_cast<unsigned char>(text[pos]))) {
159 ++pos;
160 }
161 }
162 return text.substr(whitespace_start, pos - whitespace_start);
163 }
164
165 // 情况2:当前字符是逗号
166 if (c == ',') {
167 size_t comma_start = pos;
168 ++pos; // 跳过逗号
169 // 跳过逗号后的空白
170 while (pos < text.size() && std::isspace(static_cast<unsigned char>(text[pos]))) {
171 ++pos;
172 }
173 // 返回从逗号开始到其后空白结束的序列
174 return text.substr(comma_start, pos - comma_start);
175 }
176
177 // 其他字符,继续向后查找
178 ++pos;
179 }
180 // 未找到任何分隔符,返回文本末尾的空视图
181 return StringView(text.data() + text.size(), 0);
182 }
183};
184
187{
188 bool operator()(StringView& text) const
189 {
190 if (text.size() == 1) {
191 if (text[0] == '(' || text[0] == ')') {
192 return false;
193 }
194 }
195 return true;
196 }
197};
198
199inline void ValueView::toVector(std::vector<ValueView>& value) const
200{
201 value = split().operator std::vector<ValueView>();
202}
203
204inline std::vector<double> ValueView::toFortranDoubleVector() const
205{
206 std::vector<double> value;
207 this->toFortranDoubleVector(value);
208 return value;
209}
210
211inline errc_t ValueView::toFortranDoubleVector(std::vector<double> &value) const
212{
213 for (auto& v : split()) {
214 double d;
215 if (errc_t rc = ValueView(v).toFortranDouble(d)) {
216 return rc;
217 }
218 value.push_back(d);
219 }
220 return eNoError;
221}
222
223inline std::vector<double> ValueView::toDoubleVector() const
224{
225 std::vector<double> value;
226 this->toDoubleVector(value);
227 return value;
228}
229
230inline errc_t ValueView::toDoubleVector(std::vector<double> &value) const
231{
232 for (auto& v : split()) {
233 double d;
234 if (errc_t rc = ValueView(v).toDouble(d)) {
235 return rc;
236 }
237 value.push_back(d);
238 }
239 return eNoError;
240}
241
242inline std::vector<int> ValueView::toIntVector() const
243{
244 std::vector<int> value;
245 this->toIntVector(value);
246 return value;
247}
248
249inline errc_t ValueView::toIntVector(std::vector<int>& value) const
250{
251 for (auto& v : split()) {
252 int i;
253 if (errc_t rc = ValueView(v).toInt(i)) {
254 return rc;
255 }
256 value.push_back(i);
257 }
258 return eNoError;
259}
260
261inline ValueView::SplitterType ValueView::split() const
262{
263 return aStrSplit(aStripAsciiWhitespace(value_), ByCommaAndRepeatedWhitespace(), SkipBracket());
264}
265
266AST_NAMESPACE_END
逗号和重复空格分隔符
定义 ValueView.hpp:142
颜色类
定义 Color.hpp:87
通用值类
定义 GenericValue.hpp:34
值视图类
定义 ValueView.hpp:41
double toDouble() const
转换为浮点数,不支持 Fortran 格式浮点数
定义 ValueView.hpp:67
GenericValue toValue() const
转换为通用值
定义 ValueView.hpp:111
std::string toString() const
转换为字符串
定义 ValueView.hpp:88
Color toColor() const
转换为颜色值
定义 ValueView.hpp:84
int toInt() const
转换为整数
定义 ValueView.hpp:63
const StringView & toStringView() const
转换为字符串视图
定义 ValueView.hpp:108
angle_d toAngleRad() const
转换为弧度
定义 ValueView.hpp:72
const StringView & value() const
获取值视图
定义 ValueView.hpp:59
double toFortranDouble() const
转换为浮点数,支持 Fortran 格式浮点数,例如 "1.23D2","-1.23d3"
定义 ValueView.hpp:76
bool toBool() const
转换为布尔值
定义 ValueView.hpp:80
errc_t aParseFortranDouble(StringView sv, double &value)
解析Fortran格式的双精度浮点数,例如"1.23D-4"、"1.23d-4"等
定义 ParseFormat.cpp:268
errc_t aParseColor(StringView str, Color &value)
将字符串转换为颜色值
定义 ParseFormat.cpp:409
errc_t aParseDouble(StringView str, double &value)
将字符串转换为双精度浮点数
定义 ParseFormat.cpp:253
errc_t aParseBool(StringView str, bool &value)
将字符串转换为布尔值
定义 ParseFormat.cpp:50
errc_t aParseInt(StringView str, int &value)
将字符串转换为整数
定义 ParseFormat.cpp:72
StringView aStripAsciiWhitespace(StringView str) noexcept
移除字符串首尾的ASCII空白字符
定义 StringUtil.hpp:82
strings_internal::Splitter< typename strings_internal::SelectDelimiter< Delimiter >::type, strings_internal::AllowEmpty, StringView > aStrSplit(StringView text, Delimiter delimiter)
字符串分割函数
定义 StringSplit.hpp:42
A_ALWAYS_INLINE double deg2rad(double x)
将角度转换为弧度
定义 MathDegree.hpp:45
跳过括号
定义 ValueView.hpp:187