<commit_before>#ifndef RAPIDJSON_WRITER_H_
#define RAPIDJSON_WRITER_H_

#include "rapidjson.h"
#include "internal/stack.h"
#include "internal/strfunc.h"
#include <cstdio>	// snprintf() or _sprintf_s()
#include <new>		// placement new

#ifdef _MSC_VER
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant
#endif

namespace rapidjson {

//! JSON writer
/*! Writer implements the concept Handler.
	It generates JSON text by events to an output os.

	User may programmatically calls the functions of a writer to generate JSON text.

	On the other side, a writer can also be passed to objects that generates events, 

	for example Reader::Parse() and Document::Accept().

	\tparam OutputStream Type of output stream.
	\tparam SourceEncoding Encoding of source string.
	\tparam TargetEncoding Encoding of output stream.
	\tparam Allocator Type of allocator for allocating memory of stack.
	\note implements Handler concept
*/
template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename Allocator = MemoryPoolAllocator<> >
class Writer {
public:
	typedef typename SourceEncoding::Ch Ch;

	//! Constructor
	/*! \param os Output stream.
		\param allocator User supplied allocator. If it is null, it will create a private one.
		\param levelDepth Initial capacity of stack.
	*/
	Writer(OutputStream& os, Allocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) : 
		os_(&os), level_stack_(allocator, levelDepth * sizeof(Level)),
		doublePrecision_(kDefaultDoublePrecision), hasRoot_(false) {}

	void Reset(OutputStream& os) {
		os_ = &os;
		doublePrecision_ = kDefaultDoublePrecision;
		hasRoot_ = false;
		level_stack_.Clear();
	}

	bool IsComplete() const {
		return hasRoot_ && level_stack_.Empty();
	}

	//! Set the number of significant digits for \c double values
	/*! When writing a \c double value to the \c OutputStream, the number
		of significant digits is limited to 6 by default.
		\param p maximum number of significant digits (default: 6)
		\return The Writer itself for fluent API.
	*/
	Writer& SetDoublePrecision(int p = kDefaultDoublePrecision) {
		if (p < 0) p = kDefaultDoublePrecision; // negative precision is ignored
		doublePrecision_ = p;
		return *this;
	}

	//! \see SetDoublePrecision()<commit_msg>Add API doc for Writer::Reset() and Writer::IsComplete()<commit_after>#ifndef RAPIDJSON_WRITER_H_
#define RAPIDJSON_WRITER_H_

#include "rapidjson.h"
#include "internal/stack.h"
#include "internal/strfunc.h"
#include <cstdio>	// snprintf() or _sprintf_s()
#include <new>		// placement new

#ifdef _MSC_VER
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant
#endif

namespace rapidjson {

//! JSON writer
/*! Writer implements the concept Handler.
	It generates JSON text by events to an output os.

	User may programmatically calls the functions of a writer to generate JSON text.

	On the other side, a writer can also be passed to objects that generates events, 

	for example Reader::Parse() and Document::Accept().

	\tparam OutputStream Type of output stream.
	\tparam SourceEncoding Encoding of source string.
	\tparam TargetEncoding Encoding of output stream.
	\tparam Allocator Type of allocator for allocating memory of stack.
	\note implements Handler concept
*/
template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename Allocator = MemoryPoolAllocator<> >
class Writer {
public:
	typedef typename SourceEncoding::Ch Ch;

	//! Constructor
	/*! \param os Output stream.
		\param allocator User supplied allocator. If it is null, it will create a private one.
		\param levelDepth Initial capacity of stack.
	*/
	Writer(OutputStream& os, Allocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) : 
		os_(&os), level_stack_(allocator, levelDepth * sizeof(Level)),
		doublePrecision_(kDefaultDoublePrecision), hasRoot_(false) {}

	//! Reset the writer with a new stream.
	/*!
		This function reset the writer with a new stream and default settings,
		in order to make a Writer object reusable for output multiple JSONs.

		\param os New output stream.
		\code
		Writer<OutputStream> writer(os1);
		writer.StartObject();
		// ...
		writer.EndObject();

		writer.Reset(os2);
		writer.StartObject();
		// ...
		writer.EndObject();
		\endcode
	*/
	void Reset(OutputStream& os) {
		os_ = &os;
		doublePrecision_ = kDefaultDoublePrecision;
		hasRoot_ = false;
		level_stack_.Clear();
	}

	//! Checks whether the output is a complete JSON.
	/*!
		A complete JSON has a complete root object or array.
	*/
	bool IsComplete() const {
		return hasRoot_ && level_stack_.Empty();
	}

	//! Set the number of significant digits for \c double values
	/*! When writing a \c double value to the \c OutputStream, the number
		of significant digits is limited to 6 by default.
		\param p maximum number of significant digits (default: 6)
		\return The Writer itself for fluent API.
	*/
	Writer& SetDoublePrecision(int p = kDefaultDoublePrecision) {
		if (p < 0) p = kDefaultDoublePrecision; // negative precision is ignored
		doublePrecision_ = p;
		return *this;
	}

	//! \see SetDoublePrecision()
{'fraction_non_alphanumeric': 0.09075111025294458, 'fraction_numerical': 0.004247924309712299, 'mean_word_length': 4.2803261977573905, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '327997', 'n_tokens_mistral': 1652, 'n_tokens_neox': 1508, 'n_words': 572}