Skip to content

Commit

Permalink
caching newline positions
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed May 9, 2016
1 parent 6d132fa commit 2a1d119
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 30 deletions.
43 changes: 28 additions & 15 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6258,14 +6258,23 @@ class basic_json
// process an opening string (brace, bracket)
const auto open = [&](const std::string & s)
{
if (s.find('\n') == std::string::npos)
// caching
static std::unordered_map<std::string, size_t> cache;
if (cache.find(s) == cache.end())
{
cache[s] = s.find('\n');
}

// output character
o << s;

// manage indentation
if (cache[s] == std::string::npos)
{
o << s;
last_newline = false;
}
else
{
o << s;
current_indent += indent_step;
last_newline = true;
}
Expand All @@ -6274,36 +6283,40 @@ class basic_json
// process a closing string (brace, bracket)
const auto close = [&](const std::string & s)
{
const auto nl_idx = s.find('\n');
// caching
static std::unordered_map<std::string, size_t> cache;
if (cache.find(s) == cache.end())
{
cache[s] = s.find('\n');
}

if (nl_idx == std::string::npos)
if (cache[s] == std::string::npos)
{
o << indent() << s;
last_newline = false;
}
else
{
o << s.substr(0, nl_idx + 1);
o << s.substr(0, cache[s] + 1);
current_indent -= indent_step;
last_newline = true;
o << indent() << s.substr(nl_idx + 1);
o << indent() << s.substr(cache[s] + 1);
last_newline = false;
}
};

// process an infix string (comma, colon)
const auto infix = [&](const std::string & s)
{
if (s.find('\n') == std::string::npos)
// caching
static std::unordered_map<std::string, size_t> cache;
if (cache.find(s) == cache.end())
{
o << s;
last_newline = false;
}
else
{
o << s;
last_newline = true;
cache[s] = s.find('\n');
}

o << s;
last_newline = (cache[s] != std::string::npos);
};

switch (m_type)
Expand Down
43 changes: 28 additions & 15 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -6258,14 +6258,23 @@ class basic_json
// process an opening string (brace, bracket)
const auto open = [&](const std::string & s)
{
if (s.find('\n') == std::string::npos)
// caching
static std::unordered_map<std::string, size_t> cache;
if (cache.find(s) == cache.end())
{
cache[s] = s.find('\n');
}

// output character
o << s;

// manage indentation
if (cache[s] == std::string::npos)
{
o << s;
last_newline = false;
}
else
{
o << s;
current_indent += indent_step;
last_newline = true;
}
Expand All @@ -6274,36 +6283,40 @@ class basic_json
// process a closing string (brace, bracket)
const auto close = [&](const std::string & s)
{
const auto nl_idx = s.find('\n');
// caching
static std::unordered_map<std::string, size_t> cache;
if (cache.find(s) == cache.end())
{
cache[s] = s.find('\n');
}

if (nl_idx == std::string::npos)
if (cache[s] == std::string::npos)
{
o << indent() << s;
last_newline = false;
}
else
{
o << s.substr(0, nl_idx + 1);
o << s.substr(0, cache[s] + 1);
current_indent -= indent_step;
last_newline = true;
o << indent() << s.substr(nl_idx + 1);
o << indent() << s.substr(cache[s] + 1);
last_newline = false;
}
};

// process an infix string (comma, colon)
const auto infix = [&](const std::string & s)
{
if (s.find('\n') == std::string::npos)
// caching
static std::unordered_map<std::string, size_t> cache;
if (cache.find(s) == cache.end())
{
o << s;
last_newline = false;
}
else
{
o << s;
last_newline = true;
cache[s] = s.find('\n');
}

o << s;
last_newline = (cache[s] != std::string::npos);
};

switch (m_type)
Expand Down

0 comments on commit 2a1d119

Please sign in to comment.