Skip to content

Commit e4e0d63

Browse files
committed
feat(format): len & expired
新增len&expired 优化&=键值对识别对嵌套的影响
1 parent cacc31a commit e4e0d63

File tree

5 files changed

+60
-11
lines changed

5 files changed

+60
-11
lines changed

Dice/DiceFormatter.cpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class MarkCaseNode : public MarkNode {
172172
ptr<MarkNode> other;
173173
public:
174174
MarkCaseNode(const std::string_view& s) :MarkNode(s), field(s.substr(0,s.find('?'))) {
175-
auto paras{ splitPairs(string(s.substr(s.find('?') + 1)),'=','&') };
175+
auto paras{ splitPairs(s.substr(s.find('?') + 1),'=','&') };
176176
for (auto& [key, val] : paras) {
177177
if (key == "else")other = buildFormatter(val);
178178
else cases[key] = buildFormatter(val);
@@ -191,8 +191,9 @@ class MarkGradeNode : public MarkNode {
191191
grad_map<double, ptr<MarkNode>> grades;
192192
public:
193193
MarkGradeNode(const std::string_view& s) :MarkNode(s), field(s.substr(0, s.find('?'))) {
194-
auto paras{ splitPairs(string(s.substr(s.find('?') + 1)),'=','&') };
194+
auto paras{ splitPairs(s.substr(s.find('?') + 1),'=','&') };
195195
for (auto& [key, val] : paras) {
196+
DD::debugLog("split=" + val);
196197
if (key == "else")grades.set_else(buildFormatter(val));
197198
else if (isNumeric(key))grades.set_step(stod(key), buildFormatter(val));
198199
}
@@ -224,9 +225,27 @@ class MarkPyNode : public MarkNode {
224225
return {};
225226
}
226227
};
228+
class MarkLenNode : public MarkNode {
229+
MarkReference field;
230+
public:
231+
MarkLenNode(const std::string_view& s) :MarkNode(s), field(s) {}
232+
AttrVar format(const AttrObject& context, bool isTrust = true, const dict_ci<string>& global = {})const override {
233+
auto res{ field.concat(context, isTrust, global) };
234+
return (long long)res.len();
235+
}
236+
};
237+
class MarkExpiredNode : public MarkNode {
238+
MarkReference field;
239+
public:
240+
MarkExpiredNode(const std::string_view& s) :MarkNode(s), field(s) {}
241+
AttrVar format(const AttrObject& context, bool isTrust = true, const dict_ci<string>& global = {})const override{
242+
auto res{ field.concat(context, isTrust, global)};
243+
return !res.is_numberic() || res.to_int() < time(nullptr);
244+
}
245+
};
227246

228-
static enumap<string> methods{ "var","print","help","sample","case","vary","grade","at","ran","wait","js","py","len" };
229-
enum class FmtMethod { Var, Print, Help, Sample, Case, Vary, Grade, At, Ran, Wait, JS, Py, Len };
247+
static enumap<string> methods{ "var","print","help","sample","case","vary","grade","at","ran","wait","js","py","len","expired" };
248+
enum class FmtMethod { Var, Print, Help, Sample, Case, Vary, Grade, At, Ran, Wait, JS, Py, Len, Expired };
230249
//Formatter
231250
static size_t find_close_brace(const std::string_view& s, size_t pos) {
232251
int diff = 1;
@@ -332,6 +351,16 @@ ptr<MarkNode> buildFormatter(const std::string_view& exp) {
332351
cur = &(*cur)->next;
333352
continue;
334353
}
354+
else if (method == "len") {
355+
*cur = std::static_pointer_cast<MarkNode>(std::make_shared<MarkLenNode>(para));
356+
cur = &(*cur)->next;
357+
continue;
358+
}
359+
else if (method == "expired") {
360+
*cur = std::static_pointer_cast<MarkNode>(std::make_shared<MarkExpiredNode>(para));
361+
cur = &(*cur)->next;
362+
continue;
363+
}
335364
}
336365
if (auto func = strFuncs.find(string(field)); func != strFuncs.end()) {
337366
*cur = std::make_shared<MarkGlobalIndexNode>(field);

Dice/GlobalVar.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ const dict_ci<string> PlainMsg
442442
{"strSelfName", "" },
443443
{"strSelfNick", "&strSelfName" },
444444
{"self", "&strSelfCall"},
445-
{"strBotHeader", "标准型 " },
445+
{"strBotHeader", "试验型 " },
446446
{"strBotMsg", "\n使用.help 查看{self}帮助文档"},
447447
{"strHlpMsg", R"(请使用.dismiss ID(或后四位) 使{self}退群退讨论组
448448
.bot on/off ID(或at或后四位) //开启或关闭指令
@@ -536,6 +536,7 @@ const dict_ci<string> GlobalComment{
536536
};
537537
const dict_ci<> HelpDoc = {
538538
{"更新",R"(
539+
667:format新增len&expired
539540
666:变更内部字符编码
540541
665:webui依赖本地化
541542
664:新增监听BlackInfoWarned

Dice/GlobalVar.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
* Please Do not modify Dice_Build, Dice_Ver_Without_Build, DiceRequestHeader or Dice_Ver
3838
* To costum version info, please modify const Dice_Short_Ver or Dice_Full_Ver
3939
*/
40-
constexpr unsigned short Dice_Build = 666u;
41-
inline const std::string Dice_Ver_Without_Build = "2.7.0hotfix";
42-
constexpr auto DiceRequestHeader = "Dice/2.7.0";
40+
constexpr unsigned short Dice_Build = 667u;
41+
inline const std::string Dice_Ver_Without_Build = "2.7.1beta1";
42+
constexpr auto DiceRequestHeader = "Dice/2.7.1";
4343
inline const std::string Dice_Ver = Dice_Ver_Without_Build + "(" + std::to_string(Dice_Build) + ")";
4444
extern std::string Dice_Short_Ver;
4545
constexpr bool isDev = true;

Dice/StrExtern.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int count_char(const string& s, char ch);
5050
vector<string> getLines(const string& s, char delim = '\n');
5151
vector<string> split(const string&, const string&);
5252
string splitOnce(string& str, const string& sep = ".");
53-
fifo_dict<string> splitPairs(const std::string&, char delim = '=', char br = '\n');
53+
fifo_dict<string> splitPairs(std::string_view, char delim = '=', char br = '\n');
5454

5555
template<typename Con>
5656
void splitID(const string& str , Con& list) {

Dice/strExtern.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,31 @@ string splitOnce(string& str, const string& sep) {
8787
}
8888
return head;
8989
}
90-
fifo_dict<> splitPairs(const string& s, char delim, char br) {
90+
91+
size_t find_close_brace(const std::string_view& s, size_t start = 0) {
92+
int brace_count = 0;
93+
for (size_t i = start; i < s.size(); ++i) {
94+
if (s[i] == '{') {
95+
brace_count++;
96+
}
97+
else if (s[i] == '}') {
98+
if (--brace_count == 0) {
99+
return i;
100+
}
101+
}
102+
}
103+
return std::string::npos; // 如果没有找到匹配的右花括号,返回npos
104+
}
105+
106+
fifo_dict<> splitPairs(string_view sv, char delim, char br) {
91107
fifo_dict<>dict;
92-
string_view sv(s),line;
108+
string_view line;
93109
size_t posDe{ 0 }, posBr{ 0 }, p{ 0 };
94110
while (posBr != string::npos) {
95111
posBr = sv.find(br);
112+
if (auto l{ sv.rfind('{', posBr) };l < posBr) {
113+
posBr = sv.find(br, find_close_brace(sv, l));
114+
}
96115
line = sv.substr(p = sv.find_first_not_of(space_char, p), sv.find_last_not_of(space_char, posBr - 1) - p + 1);
97116
if ((posDe = line.find(delim)) != string::npos) {
98117
if(posDe)dict[string(line.substr(0, posDe))] = line.substr(posDe + 1);

0 commit comments

Comments
 (0)