Skip to content

Commit 5b01fc7

Browse files
committed
Fixed #18 Bug in parsing element of array with colon
1 parent c01e385 commit 5b01fc7

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed

src/wsjcpp_yaml.cpp

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ WsjcppYamlParsebleLine::WsjcppYamlParsebleLine(int nLine) {
611611
m_sPrefix = "";
612612
m_bArrayItem = false;
613613
m_sComment = "";
614-
m_sName = "";
614+
m_sTagName = "";
615615
m_sValue = "";
616616
m_nNameQuotes = WSJCPP_YAML_QUOTES_NONE;
617617
m_nValueQuotes = WSJCPP_YAML_QUOTES_NONE;
@@ -665,7 +665,7 @@ bool WsjcppYamlParsebleLine::hasComment() {
665665
// ---------------------------------------------------------------------
666666

667667
std::string WsjcppYamlParsebleLine::getName() {
668-
return m_sName;
668+
return m_sTagName;
669669
}
670670

671671
// ---------------------------------------------------------------------
@@ -677,7 +677,7 @@ WsjcppYamlQuotes WsjcppYamlParsebleLine::getNameQuotes() {
677677
// ---------------------------------------------------------------------
678678

679679
bool WsjcppYamlParsebleLine::isEmptyName() {
680-
return m_sName.length() == 0;
680+
return m_sTagName.length() == 0;
681681
}
682682

683683
// ---------------------------------------------------------------------
@@ -721,7 +721,7 @@ bool WsjcppYamlParsebleLine::parseLine(const std::string &sLine, std::string &sE
721721
m_bArrayItem = false;
722722
m_sPrefix = "";
723723
m_sComment = "";
724-
m_sName = "";
724+
m_sTagName = "";
725725
m_sValue = "";
726726
m_bHasComment = false;
727727
m_nNameQuotes = WSJCPP_YAML_QUOTES_NONE;
@@ -783,9 +783,8 @@ bool WsjcppYamlParsebleLine::parseLine(const std::string &sLine, std::string &sE
783783
state = WSJCPP_YAML_PARSER_LINE_STATE_VALUE;
784784
m_sValue += c;
785785
} else if (c == ':' && state == WSJCPP_YAML_PARSER_LINE_STATE_VALUE) {
786-
std::cout << m_sValue << std::endl;
787-
if (m_sName.length() == 0) {
788-
m_sName = m_sValue;
786+
if (m_sTagName.length() == 0 && this->canTagName(m_sValue)) {
787+
m_sTagName = m_sValue;
789788
m_sValue = ""; // reset value it was param name
790789
} else {
791790
m_sValue += c;
@@ -823,14 +822,14 @@ bool WsjcppYamlParsebleLine::parseLine(const std::string &sLine, std::string &sE
823822
}
824823
}*/
825824

826-
m_sName = WsjcppCore::trim(m_sName);
827-
if (m_sName.length() > 0 && m_sName[0] == '"') {
825+
m_sTagName = WsjcppCore::trim(m_sTagName);
826+
if (m_sTagName.length() > 0 && m_sTagName[0] == '"') {
828827
m_nNameQuotes = WSJCPP_YAML_QUOTES_DOUBLE;
829-
m_sName = removeStringDoubleQuotes(m_sName);
828+
m_sTagName = removeStringDoubleQuotes(m_sTagName);
830829
}
831-
if (m_sName.length() > 0 && m_sName[0] == '\'') {
830+
if (m_sTagName.length() > 0 && m_sTagName[0] == '\'') {
832831
m_nNameQuotes = WSJCPP_YAML_QUOTES_SINGLE;
833-
m_sName = removeStringSingleQuotes(m_sName);
832+
m_sTagName = removeStringSingleQuotes(m_sTagName);
834833
}
835834

836835
m_sValue = WsjcppCore::trim(m_sValue);
@@ -844,12 +843,41 @@ bool WsjcppYamlParsebleLine::parseLine(const std::string &sLine, std::string &sE
844843
}
845844

846845
m_sComment = WsjcppCore::trim(m_sComment);
846+
847+
if (m_bArrayItem == false && m_sTagName.length() == 0 && m_sValue.length() > 0 ) {
848+
sError = "Value of name can be empty only for array-item (line: " + sLine + ")";
849+
return false;
850+
}
847851
return true;
848852
}
849853

850854
// ---------------------------------------------------------------------
851855

852856
bool WsjcppYamlParsebleLine::canTagName(const std::string &sVal) {
857+
std::string sTrim = sVal;
858+
sTrim = WsjcppCore::trim(sTrim);
859+
int nLen = sTrim.length();
860+
if (nLen == 0) {
861+
return false;
862+
}
863+
if (sTrim.length() > 0 && sTrim[0] == '"' && sTrim[nLen-1] == '"') {
864+
return true;
865+
}
866+
if (sTrim.length() > 0 && sTrim[0] == '\'' && sTrim[nLen-1] == '\'') {
867+
return true;
868+
}
869+
// check illegal char
870+
for (int i = 0; i < nLen; i++) {
871+
char c = sTrim[i];
872+
if (
873+
c != '-' && c != '_'
874+
&& (c < '0' || c > '9')
875+
&& (c < 'a' || c > 'z')
876+
&& (c < 'A' || c > 'Z')
877+
) {
878+
return false;
879+
}
880+
}
853881
return true;
854882
}
855883

src/wsjcpp_yaml.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class WsjcppYamlParsebleLine {
167167
std::string m_sPrefix;
168168
bool m_bArrayItem;
169169
std::string m_sComment;
170-
std::string m_sName;
170+
std::string m_sTagName;
171171
std::string m_sValue;
172172
WsjcppYamlQuotes m_nNameQuotes;
173173
WsjcppYamlQuotes m_nValueQuotes;

unit-tests.wsjcpp/src/unit_test_tag_names.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,23 @@ void UnitTestTagNames::executeTest() {
3636
std::string sError;
3737
// wrong
3838
compare("wrong name", yaml.loadFromString("wrong name", "./test10: one", sError), false);
39-
compare("name use quotes", yaml.loadFromString("name use quotes", "\"./test10\": one", sError), true);
39+
compare("name use quotes 1", yaml.loadFromString("name use quotes", "\"./test10\": one", sError), true);
40+
compare("name use quotes 2 - wrong", yaml.loadFromString("name use quotes", "\"./te\"st10\": one", sError), false);
41+
compare("name use quotes 3", yaml.loadFromString("name use quotes", "\"./te\\\"st10\": one", sError), true);
4042

43+
compare("array", yaml.loadFromString("array",
44+
"arr1: \n"
45+
" - ./te:11\n"
46+
" - \"./te\":11\n"
47+
, sError), true);
48+
49+
compare("arr1 is array", yaml.getRoot()->getElement("arr1")->isArray(), true);
50+
compare("arr1 size 2", yaml.getRoot()->getElement("arr1")->getLength(), 2);
51+
compare("arr1 el 0 is value", yaml.getRoot()->getElement("arr1")->getElement(0)->isValue(), true);
52+
compare("arr1 el 0 is value", yaml.getRoot()->getElement("arr1")->getElement(0)->getValue(), "./te:11");
53+
compare("arr1 el 1 is map", yaml.getRoot()->getElement("arr1")->getElement(1)->isMap(), true);
54+
compare("arr1 el 1 is map", yaml.getRoot()->getElement("arr1")->getElement(1)->hasElement("./te"), true);
55+
compare("arr1 el 1 is map", yaml.getRoot()->getElement("arr1")->getElement(1)->getElement("./te")->getValue(), "11");
4156
}
4257

4358
// ---------------------------------------------------------------------

0 commit comments

Comments
 (0)