🛰️航天仿真算法库 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
31AST_NAMESPACE_BEGIN
32
33class ByCommaAndRepeatedWhitespace;
34struct SkipBracket;
35
40{
41public:
42 ValueView() = default;
44 : value_(value){}
45 ValueView(const char* value)
46 : value_(value){}
47 ValueView(const std::string& value)
48 : value_(value){}
49 ValueView(const GenericValue& value)
50 : value_(value.value()) {}
51
53 const StringView& value() const { return value_; }
54 StringView& value() { return value_; }
55
57 int toInt() const { return aParseInt(value_); }
58 errc_t toInt(int& value) const { return aParseInt(value_, value); }
59
61 double toDouble() const { return aParseDouble(value_); }
62 errc_t toDouble(double& value) const { return aParseDouble(value_, value); }
63
65 double toFortranDouble() const { return aParseFortranDouble(value_); }
66 errc_t toFortranDouble(double& value) const { return aParseFortranDouble(value_, value); }
67
69 bool toBool() const { return aParseBool(value_); }
70 errc_t toBool(bool& value) const { return aParseBool(value_, value); }
71
73 Color toColor() const { return aParseColor(value_); }
74 errc_t toColor(Color& value) const { return aParseColor(value_, value); }
75
77 std::string toString() const { return std::string(value_); }
78 void toString(std::string& value) const { value = std::string(value_); }
79
81 std::vector<ValueView> toVector() const;
82 void toVector(std::vector<ValueView>& value) const;
83
85 std::vector<double> toFortranDoubleVector() const;
86 errc_t toFortranDoubleVector(std::vector<double>& value) const;
87
89 std::vector<double> toDoubleVector() const;
90 errc_t toDoubleVector(std::vector<double>& value) const;
91
93 std::vector<int> toIntVector() const;
94 errc_t toIntVector(std::vector<int>& value) const;
95
97 const StringView& toStringView() const { return value_; }
98
100 GenericValue toValue() const { return GenericValue(value_); }
101
102 operator StringView() const { return value_; }
103
104 const char* data() const { return value_.data(); }
105 size_t size() const { return value_.size(); }
106
107 using SplitterType = Splitter<ByCommaAndRepeatedWhitespace, SkipBracket>;
108 SplitterType split() const;
109
110 template<typename Delimiter>
111 inline strings_internal::Splitter<typename strings_internal::SelectDelimiter<Delimiter>::type, strings_internal::AllowEmpty, StringView>
112 split(Delimiter delimiter) const {
113 using DelimiterType = typename strings_internal::SelectDelimiter<Delimiter>::type;
114 return strings_internal::Splitter<DelimiterType, strings_internal::AllowEmpty, StringView>(
115 value_, DelimiterType(delimiter), strings_internal::AllowEmpty());
116 }
117public:
118 StringView value_;
119};
120
121
122inline std::vector<ValueView> ValueView::toVector() const
123{
124 std::vector<ValueView> value;
125 this->toVector(value);
126 return value;
127}
128
129
132public:
133 StringView Find(StringView text, size_t pos) const {
134 while (pos < text.size()) {
135 char c = text[pos];
136 // 情况1:当前字符是空白
137 if (std::isspace(static_cast<unsigned char>(c))) {
138 size_t whitespace_start = pos;
139 // 跳过连续空白
140 while (pos < text.size() && std::isspace(static_cast<unsigned char>(text[pos]))) {
141 ++pos;
142 }
143 // 检查空白之后是否是逗号
144 if (pos < text.size() && text[pos] == ',') {
145 // 空白后紧跟逗号,继续跳过逗号后的空白
146 ++pos; // 跳过逗号
147 while (pos < text.size() && std::isspace(static_cast<unsigned char>(text[pos]))) {
148 ++pos;
149 }
150 }
151 return text.substr(whitespace_start, pos - whitespace_start);
152 }
153
154 // 情况2:当前字符是逗号
155 if (c == ',') {
156 size_t comma_start = pos;
157 ++pos; // 跳过逗号
158 // 跳过逗号后的空白
159 while (pos < text.size() && std::isspace(static_cast<unsigned char>(text[pos]))) {
160 ++pos;
161 }
162 // 返回从逗号开始到其后空白结束的序列
163 return text.substr(comma_start, pos - comma_start);
164 }
165
166 // 其他字符,继续向后查找
167 ++pos;
168 }
169 // 未找到任何分隔符,返回文本末尾的空视图
170 return StringView(text.data() + text.size(), 0);
171 }
172};
173
176{
177 bool operator()(StringView& text) const
178 {
179 if (text.size() == 1) {
180 if (text[0] == '(' || text[0] == ')') {
181 return false;
182 }
183 }
184 return true;
185 }
186};
187
188inline void ValueView::toVector(std::vector<ValueView>& value) const
189{
190 value = split().operator std::vector<ValueView>();
191}
192
193inline std::vector<double> ValueView::toFortranDoubleVector() const
194{
195 std::vector<double> value;
196 this->toFortranDoubleVector(value);
197 return value;
198}
199
200inline errc_t ValueView::toFortranDoubleVector(std::vector<double> &value) const
201{
202 for (auto& v : split()) {
203 double d;
204 if (errc_t rc = ValueView(v).toFortranDouble(d)) {
205 return rc;
206 }
207 value.push_back(d);
208 }
209 return eNoError;
210}
211
212inline std::vector<double> ValueView::toDoubleVector() const
213{
214 std::vector<double> value;
215 this->toDoubleVector(value);
216 return value;
217}
218
219inline errc_t ValueView::toDoubleVector(std::vector<double> &value) const
220{
221 for (auto& v : split()) {
222 double d;
223 if (errc_t rc = ValueView(v).toDouble(d)) {
224 return rc;
225 }
226 value.push_back(d);
227 }
228 return eNoError;
229}
230
231inline std::vector<int> ValueView::toIntVector() const
232{
233 std::vector<int> value;
234 this->toIntVector(value);
235 return value;
236}
237
238inline errc_t ValueView::toIntVector(std::vector<int>& value) const
239{
240 for (auto& v : split()) {
241 int i;
242 if (errc_t rc = ValueView(v).toInt(i)) {
243 return rc;
244 }
245 value.push_back(i);
246 }
247 return eNoError;
248}
249
250inline ValueView::SplitterType ValueView::split() const
251{
252 return aStrSplit(aStripAsciiWhitespace(value_), ByCommaAndRepeatedWhitespace(), SkipBracket());
253}
254
255AST_NAMESPACE_END
逗号和重复空格分隔符
定义 ValueView.hpp:131
颜色类
定义 Color.hpp:87
通用值类
定义 GenericValue.hpp:34
值视图类
定义 ValueView.hpp:40
double toDouble() const
转换为浮点数,不支持 Fortran 格式浮点数
定义 ValueView.hpp:61
GenericValue toValue() const
转换为通用值
定义 ValueView.hpp:100
std::string toString() const
转换为字符串
定义 ValueView.hpp:77
Color toColor() const
转换为颜色值
定义 ValueView.hpp:73
int toInt() const
转换为整数
定义 ValueView.hpp:57
const StringView & toStringView() const
转换为字符串视图
定义 ValueView.hpp:97
const StringView & value() const
获取值视图
定义 ValueView.hpp:53
double toFortranDouble() const
转换为浮点数,支持 Fortran 格式浮点数,例如 "1.23D2","-1.23d3"
定义 ValueView.hpp:65
bool toBool() const
转换为布尔值
定义 ValueView.hpp:69
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
跳过括号
定义 ValueView.hpp:176