Skip to content

Commit

Permalink
Optimize the implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Waz committed Nov 14, 2022
1 parent 07ff395 commit 794f085
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions c_src/exml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,13 +481,6 @@ static ERL_NIF_TERM parse_next(ErlNifEnv *env, int argc,
if (!parser->copy_buffer(env, argv[1]))
return enif_make_badarg(env);

// Raise an exception when null character is found.
const char *data = reinterpret_cast<const char*>(&Parser::buffer[0]);
if (std::strlen(data) != Parser::buffer.size() - 1)
return enif_make_tuple2(
env, atom_error,
enif_make_string(env, "null character found in buffer", ERL_NIF_LATIN1));

// Skip initial whitespace even if we don't manage to parse anything.
// Also needed for has_stream_closing_tag to recognize the tag.
std::size_t offset = 0;
Expand Down Expand Up @@ -559,6 +552,13 @@ static ERL_NIF_TERM parse_next(ErlNifEnv *env, int argc,
enif_make_string(env, result.error_message.c_str(), ERL_NIF_LATIN1));
}

// Raise an exception when null character is found.
std::size_t rest_size = &Parser::buffer.back() - result.rest;
if (std::strlen(reinterpret_cast<const char*>(result.rest)) != rest_size)
return enif_make_tuple2(
env, atom_error,
enif_make_string(env, "null character found in buffer", ERL_NIF_LATIN1));

return enif_make_tuple3(
env, atom_ok, element,
enif_make_uint64(env, result.rest - Parser::buffer.data()));
Expand Down

0 comments on commit 794f085

Please sign in to comment.