Parse simple strings

This commit is contained in:
Benoit Blanchon
2014-10-15 23:39:25 +02:00
parent 3d92531ad3
commit 241ca79114
3 changed files with 37 additions and 0 deletions

View File

@ -70,6 +70,11 @@ bool JsonParser::isSpace()
return *_ptr == ' ' || *_ptr == '\t' || *_ptr == '\n' || *_ptr == '\r'; return *_ptr == ' ' || *_ptr == '\t' || *_ptr == '\n' || *_ptr == '\r';
} }
bool JsonParser::isString()
{
return *_ptr == '\"';
}
void JsonParser::skipOneChar() void JsonParser::skipOneChar()
{ {
_ptr++; _ptr++;
@ -99,6 +104,9 @@ JsonNode* JsonParser::parseAnything()
if (isNull()) if (isNull())
return parseNull(); return parseNull();
if (isString())
return parseString();
return 0; return 0;
} }
@ -168,3 +176,15 @@ JsonNode* JsonParser::parseNull()
return _buffer->createStringNode(0); return _buffer->createStringNode(0);
} }
JsonNode* JsonParser::parseString()
{
const char* s = ++_ptr;
while (*_ptr != '\"')
_ptr++;
*_ptr = 0;
_ptr++;
return _buffer->createStringNode(s);
}

View File

@ -29,6 +29,7 @@ private:
inline bool isLong(); inline bool isLong();
inline bool isNull(); inline bool isNull();
inline bool isSpace(); inline bool isSpace();
inline bool isString();
inline void skipOneChar(); inline void skipOneChar();
inline void skipSpaces(); inline void skipSpaces();
@ -37,6 +38,7 @@ private:
inline JsonNode* parseBoolean(); inline JsonNode* parseBoolean();
inline JsonNode* parseLong(); inline JsonNode* parseLong();
inline JsonNode* parseNull(); inline JsonNode* parseNull();
inline JsonNode* parseString();
JsonNode *parseDouble(); JsonNode *parseDouble();
}; };

View File

@ -45,6 +45,11 @@ protected:
EXPECT_EQ(expected, static_cast<T>(_array[index])); EXPECT_EQ(expected, static_cast<T>(_array[index]));
} }
void elementAtIndexMustBe(int index, const char* expected)
{
EXPECT_STREQ(expected, static_cast<const char*>(_array[index]));
}
StaticJsonBuffer<42> _jsonBuffer; StaticJsonBuffer<42> _jsonBuffer;
JsonArray _array; JsonArray _array;
char _jsonString[256]; char _jsonString[256];
@ -147,4 +152,14 @@ TEST_F(JsonArray_Parser_Tests, TwoNulls)
sizeMustBe(2); sizeMustBe(2);
firstElementMustBe(nullCharPtr); firstElementMustBe(nullCharPtr);
secondElementMustBe(nullCharPtr); secondElementMustBe(nullCharPtr);
}
TEST_F(JsonArray_Parser_Tests, TwoStrings)
{
whenInputIs("[\"hello\",\"world\"]");
parseMustSucceed();
sizeMustBe(2);
firstElementMustBe("hello");
secondElementMustBe("world");
} }