move some methods
This commit is contained in:
@@ -388,7 +388,7 @@ static const char* knownFieldNames[] = {
|
|||||||
/** the number of known field names. */
|
/** the number of known field names. */
|
||||||
static const size_t knownFieldCount = sizeof(knownFieldNames) / sizeof(char*);
|
static const size_t knownFieldCount = sizeof(knownFieldNames) / sizeof(char*);
|
||||||
|
|
||||||
std::pair<string, int> makeField(const string& name, bool isField) {
|
std::pair<string, int> MqttReplacer::makeField(const string& name, bool isField) {
|
||||||
if (!isField) {
|
if (!isField) {
|
||||||
return {name, -1};
|
return {name, -1};
|
||||||
}
|
}
|
||||||
@@ -400,7 +400,7 @@ std::pair<string, int> makeField(const string& name, bool isField) {
|
|||||||
return {name, knownFieldCount};
|
return {name, knownFieldCount};
|
||||||
}
|
}
|
||||||
|
|
||||||
void addPart(ostringstream& stack, int inField, vector<std::pair<string, int>>& parts) {
|
void MqttReplacer::addPart(ostringstream& stack, int inField) {
|
||||||
string str = stack.str();
|
string str = stack.str();
|
||||||
if (inField == 1 && str == "_") {
|
if (inField == 1 && str == "_") {
|
||||||
inField = 0; // single "%_" pattern to reduce to "_"
|
inField = 0; // single "%_" pattern to reduce to "_"
|
||||||
@@ -412,12 +412,12 @@ void addPart(ostringstream& stack, int inField, vector<std::pair<string, int>>&
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
stack.str("");
|
stack.str("");
|
||||||
if (inField == 0 && !parts.empty() && parts[parts.size()-1].second < 0) {
|
if (inField == 0 && !m_parts.empty() && m_parts[m_parts.size()-1].second < 0) {
|
||||||
// append constant to previous constant
|
// append constant to previous constant
|
||||||
parts[parts.size()-1].first += str;
|
m_parts[m_parts.size()-1].first += str;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
parts.push_back(makeField(str, inField > 0));
|
m_parts.push_back(makeField(str, inField > 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MqttReplacer::parse(const string& templateStr, bool onlyKnown, bool noKnownDuplicates, bool emptyIfMissing) {
|
bool MqttReplacer::parse(const string& templateStr, bool onlyKnown, bool noKnownDuplicates, bool emptyIfMissing) {
|
||||||
@@ -431,24 +431,24 @@ bool MqttReplacer::parse(const string& templateStr, bool onlyKnown, bool noKnown
|
|||||||
inField = 0;
|
inField = 0;
|
||||||
stack << ch;
|
stack << ch;
|
||||||
} else {
|
} else {
|
||||||
addPart(stack, inField, m_parts);
|
addPart(stack, inField);
|
||||||
inField = 1;
|
inField = 1;
|
||||||
}
|
}
|
||||||
} else if (ch == '{' && inField == 1 && empty) {
|
} else if (ch == '{' && inField == 1 && empty) {
|
||||||
inField = 2;
|
inField = 2;
|
||||||
} else if (ch == '}' && inField == 2) {
|
} else if (ch == '}' && inField == 2) {
|
||||||
addPart(stack, 1, m_parts);
|
addPart(stack, 1);
|
||||||
inField = 0;
|
inField = 0;
|
||||||
} else {
|
} else {
|
||||||
if (inField > 0 && !((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_')) {
|
if (inField > 0 && !((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_')) {
|
||||||
// invalid field character
|
// invalid field character
|
||||||
addPart(stack, inField, m_parts);
|
addPart(stack, inField);
|
||||||
inField = 0;
|
inField = 0;
|
||||||
}
|
}
|
||||||
stack << ch;
|
stack << ch;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
addPart(stack, inField, m_parts);
|
addPart(stack, inField);
|
||||||
if (onlyKnown || noKnownDuplicates) {
|
if (onlyKnown || noKnownDuplicates) {
|
||||||
int foundMask = 0;
|
int foundMask = 0;
|
||||||
int knownCount = knownFieldCount;
|
int knownCount = knownFieldCount;
|
||||||
|
|||||||
@@ -180,6 +180,22 @@ class MqttReplacer {
|
|||||||
/** true when the complete result is supposed to be empty when at least one referenced variable
|
/** true when the complete result is supposed to be empty when at least one referenced variable
|
||||||
* is empty or not defined. */
|
* is empty or not defined. */
|
||||||
bool m_emptyIfMissing;
|
bool m_emptyIfMissing;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a named field or constant.
|
||||||
|
* @param name the plain string or the name of the field.
|
||||||
|
* @param isField true when it is a field.
|
||||||
|
* @return the created pair.
|
||||||
|
*/
|
||||||
|
static std::pair<string, int> makeField(const string& name, bool isField);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a part to the list of parts.
|
||||||
|
* @param stack the parsing stack.
|
||||||
|
* @param inField 1 after '%', 2 after '%{', 0 otherwise.
|
||||||
|
*/
|
||||||
|
void addPart(ostringstream& stack, int inField);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user