Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing for user-defined string type in lexer/parser #1009

Merged
merged 1 commit into from
Mar 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions include/nlohmann/detail/input/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class lexer
using number_integer_t = typename BasicJsonType::number_integer_t;
using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
using number_float_t = typename BasicJsonType::number_float_t;
using string_t = typename BasicJsonType::string_t;

public:
/// token types for the parser
Expand Down Expand Up @@ -1130,7 +1131,7 @@ class lexer
}

/// return current string value (implicitly resets the token; useful only once)
std::string&& move_string()
string_t&& move_string()
{
return std::move(token_buffer);
}
Expand Down Expand Up @@ -1260,7 +1261,7 @@ class lexer
std::vector<char> token_string {};

/// buffer for variable-length tokens (numbers, strings)
std::string token_buffer {};
string_t token_buffer {};

/// a description of occurred lexer errors
const char* error_message = "";
Expand Down
3 changes: 2 additions & 1 deletion include/nlohmann/detail/input/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class parser
using number_integer_t = typename BasicJsonType::number_integer_t;
using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
using number_float_t = typename BasicJsonType::number_float_t;
using string_t = typename BasicJsonType::string_t;
using lexer_t = lexer<BasicJsonType>;
using token_type = typename lexer_t::token_type;

Expand Down Expand Up @@ -175,7 +176,7 @@ class parser
}

// parse values
std::string key;
string_t key;
BasicJsonType value;
while (true)
{
Expand Down
8 changes: 5 additions & 3 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,7 @@ class lexer
using number_integer_t = typename BasicJsonType::number_integer_t;
using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
using number_float_t = typename BasicJsonType::number_float_t;
using string_t = typename BasicJsonType::string_t;

public:
/// token types for the parser
Expand Down Expand Up @@ -2969,7 +2970,7 @@ class lexer
}

/// return current string value (implicitly resets the token; useful only once)
std::string&& move_string()
string_t&& move_string()
{
return std::move(token_buffer);
}
Expand Down Expand Up @@ -3099,7 +3100,7 @@ class lexer
std::vector<char> token_string {};

/// buffer for variable-length tokens (numbers, strings)
std::string token_buffer {};
string_t token_buffer {};

/// a description of occurred lexer errors
const char* error_message = "";
Expand Down Expand Up @@ -3155,6 +3156,7 @@ class parser
using number_integer_t = typename BasicJsonType::number_integer_t;
using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
using number_float_t = typename BasicJsonType::number_float_t;
using string_t = typename BasicJsonType::string_t;
using lexer_t = lexer<BasicJsonType>;
using token_type = typename lexer_t::token_type;

Expand Down Expand Up @@ -3298,7 +3300,7 @@ class parser
}

// parse values
std::string key;
string_t key;
BasicJsonType value;
while (true)
{
Expand Down
1 change: 1 addition & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CPPFLAGS += -I ../single_include -I . -I thirdparty/catch -I thirdparty/fifo_map
SOURCES = src/unit.cpp \
src/unit-algorithms.cpp \
src/unit-allocator.cpp \
src/unit-alt-string.cpp \
src/unit-capacity.cpp \
src/unit-cbor.cpp \
src/unit-class_const_iterator.cpp \
Expand Down
29 changes: 23 additions & 6 deletions test/src/unit-alt-string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class alt_string
using value_type = std::string::value_type;

alt_string(const char* str): str_impl(str) {}
alt_string(const char* str, size_t count): str_impl(str, count) {}
alt_string(const char* str, std::size_t count): str_impl(str, count) {}
alt_string(size_t count, char chr): str_impl(count, chr) {}
alt_string() = default;

Expand Down Expand Up @@ -70,17 +70,17 @@ class alt_string
return str_impl != op;
}

size_t size() const noexcept
std::size_t size() const noexcept
{
return str_impl.size();
}

void resize (size_t n)
void resize (std::size_t n)
{
str_impl.resize(n);
}

void resize (size_t n, char c)
void resize (std::size_t n, char c)
{
str_impl.resize(n, c);
}
Expand All @@ -101,12 +101,12 @@ class alt_string
return str_impl.c_str();
}

char& operator[](int index)
char& operator[](std::size_t index)
{
return str_impl[index];
}

const char& operator[](int index) const
const char& operator[](std::size_t index) const
{
return str_impl[index];
}
Expand All @@ -121,6 +121,16 @@ class alt_string
return str_impl.back();
}

void clear()
{
str_impl.clear();
}

const value_type* data()
{
return str_impl.data();
}

private:
std::string str_impl;
};
Expand Down Expand Up @@ -192,4 +202,11 @@ TEST_CASE("alternative string type")
CHECK(dump == R"({"list":[1,0,2]})");
}
}

SECTION("parse")
{
auto doc = alt_json::parse("{\"foo\": \"bar\"}");
alt_string dump = doc.dump();
CHECK(dump == R"({"foo":"bar"})");
}
}