plcopen 0.22.0
载入中...
搜索中...
未找到
standard_functions.h
浏览该文件的文档.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <cstdint>
6#include <string>
7#include <string_view>
8#include <limits>
9
10namespace plcopen::core::st
11{
12
22
24{
25 std::string_view name;
27};
28
29inline constexpr std::array<StandardFunctionManifestEntry, 51>
31 {"ABS", true}, {"SQRT", true}, {"LN", true}, {"LOG", true},
32 {"EXP", true}, {"SIN", true}, {"COS", true}, {"TAN", true},
33 {"ASIN", true}, {"ACOS", true}, {"ATAN", true}, {"ADD", true},
34 {"SUB", true}, {"MUL", true}, {"DIV", true}, {"MOD", true},
35 {"EXPT", true}, {"MIN", true}, {"MAX", true}, {"LIMIT", true},
36 {"SEL", true}, {"MUX", true}, {"GT", true}, {"GE", true},
37 {"EQ", true}, {"LE", true}, {"LT", true}, {"NE", true},
38 {"SHL", true}, {"SHR", true}, {"ROL", true}, {"ROR", true},
39 {"LEN", true}, {"LEFT", true}, {"RIGHT", true}, {"MID", true},
40 {"CONCAT", true}, {"INSERT", true}, {"DELETE", true},
41 {"REPLACE", true}, {"FIND", true}, {"ADD_TIME", true},
42 {"ADD_TOD_TIME", true}, {"ADD_DT_TIME", true}, {"SUB_TIME", true},
43 {"SUB_DATE_DATE", true}, {"SUB_TOD_TIME", true}, {"SUB_DT_DT", true},
44 {"MULTIME", true}, {"DIVTIME", true}, {"CONCAT_DATE_TOD", true},
45 }};
46
47inline constexpr const auto &standard_function_manifest()
48{
50}
51
53{
54 std::string result;
55 for(const auto &entry : kStandardFunctionManifest) {
56 if(!result.empty()) result.push_back('\n');
57 result.append(entry.name);
58 result.append(entry.registered ? ":registered" : ":missing");
59 }
60 return result;
61}
62
63inline bool resolve_standard_function(std::string_view lower,
64 StandardFunction &function)
65{
66 for(std::size_t index = 0; index < kStandardFunctionManifest.size(); ++index) {
67 const std::string_view upper = kStandardFunctionManifest[index].name;
68 if(lower.size() != upper.size()) continue;
69 bool equal = true;
70 for(std::size_t c = 0; c < lower.size(); ++c) {
71 const char expected = upper[c] >= 'A' && upper[c] <= 'Z'
72 ? static_cast<char>(upper[c] - 'A' + 'a') : upper[c];
73 if(lower[c] != expected) { equal = false; break; }
74 }
75 if(equal) {
76 function = static_cast<StandardFunction>(index);
77 return true;
78 }
79 }
80 return false;
81}
82
83inline constexpr std::uint32_t saturating_cost_add(std::uint32_t left,
84 std::uint32_t right)
85{
86 return right > std::numeric_limits<std::uint32_t>::max() - left
87 ? std::numeric_limits<std::uint32_t>::max() : left + right;
88}
89
90inline constexpr std::uint32_t saturating_cost_mul(std::uint32_t left,
91 std::uint32_t right)
92{
93 return left != 0U &&
94 right > std::numeric_limits<std::uint32_t>::max() / left
95 ? std::numeric_limits<std::uint32_t>::max() : left * right;
96}
97
98inline constexpr std::uint32_t standard_string_cost(
99 StandardFunction function, const std::uint32_t *capacities,
100 std::uint8_t count, std::uint32_t destination_capacity)
101{
102 if(count == 0U) return 1U;
103 if(function == StandardFunction::len) {
104 return std::max(1U, saturating_cost_mul(capacities[0], 2U));
105 }
106 if(function == StandardFunction::find && count >= 2U) {
107 const std::uint32_t search = saturating_cost_mul(capacities[0],
108 capacities[1]);
109 return std::max(1U, saturating_cost_add(
110 search, saturating_cost_add(capacities[0], capacities[1])));
111 }
112 std::uint32_t total = destination_capacity;
113 for(std::uint8_t index = 0; index < count; ++index) {
114 total = saturating_cost_add(total, capacities[index]);
115 }
116 return std::max(1U, saturating_cost_mul(total, 4U));
117}
118
119} // namespace plcopen::core::st
constexpr std::uint32_t saturating_cost_mul(std::uint32_t left, std::uint32_t right)
constexpr std::array< StandardFunctionManifestEntry, 51 > kStandardFunctionManifest
constexpr std::uint32_t saturating_cost_add(std::uint32_t left, std::uint32_t right)
constexpr const auto & standard_function_manifest()
bool resolve_standard_function(std::string_view lower, StandardFunction &function)
std::string standard_function_manifest_dump()
constexpr std::uint32_t standard_string_cost(StandardFunction function, const std::uint32_t *capacities, std::uint8_t count, std::uint32_t destination_capacity)