<commit_before>////////////////////////////////////////////////////////////////////////////////
/// @brief Library to build up Jason documents.
///
/// DISCLAIMER
///
/// Copyright 2015 ArangoDB GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
///     http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author <NAME>
/// @author <NAME>
/// @author Copyright 2015, ArangoDB GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

#ifndef JASON_DUMPER_H
#define JASON_DUMPER_H 1

#include <string>
#include <cmath>
#include <functional>
#include <ostream>

#include "Jason.h"
#include "JasonBuffer.h"
#include "JasonException.h"
#include "JasonOptions.h"
#include "JasonSlice.h"
#include "JasonType.h"

namespace arangodb {
  namespace jason {

    // forward for fpconv function declared elsewhere
    int fpconv_dtoa (double fp, char dest[24]);

    // Dumps Jason into a JSON output string
    template<typename T, bool PrettyPrint = false>
    class JasonDumper {

      public:

        JasonOptions options;        
    
        enum UnsupportedTypeStrategy {
          StrategyNullify,
          StrategyFail
        };

        JasonDumper (JasonDumper const&) = delete;
        JasonDumper& operator= (JasonDumper const&) = delete;

        JasonDumper (T& buffer, UnsupportedTypeStrategy strategy = StrategyFail) 
          : _buffer(&buffer), _strategy(strategy), _indentation(0) {
        }

        ~JasonDumper () {
        }

        T* buffer () const {
          return _buffer;
        }

        friend std::ostream& operator<< (std::ostream& stream, JasonDumper const* dumper) {
          stream << *dumper->_buffer;
          return stream;
        }

        friend std::ostream& operator<< (std::ostream& stream, JasonDumper const& dumper) {
          stream << *dumper._buffer;
          return stream;
        }

        void setCallback (std::function<bool(T*, JasonSlice const*, JasonSlice const*)> const& callback) {
          _callback = callback;
        }

        void dump (JasonSlice const& slice) {
          _indentation = 0;
          internalDump(&slice, nullptr);
        }

        static void Dump (JasonSlice const& slice, T& buffer, UnsupportedTypeStrategy strategy = StrategyFail) {
          JasonDumper dumper(buffer, strategy);
          dumper.internalDump(&slice, nullptr);
        }

        static void Dump (JasonSlice const* slice, T& buffer, UnsupportedTypeStrategy strategy = StrategyFail) {
          JasonDumper dumper(buffer, strategy);
          dumper.internalDump(slice, nullptr);
        }
        
        static T Dump (JasonSlice const& slice, UnsupportedTypeStrategy strategy = StrategyFail) {
          T buffer;
          JasonDumper dumper(buffer, strategy);
          dumper.internalDump(&slice, nullptr);
          return buffer;
        }

        static T Dump (JasonSlice const* slice, UnsupportedTypeStrategy strategy = StrategyFail) {
          T buffer;
          JasonDumper dumper(buffer, strategy);
          dumper.internalDump(slice, nullptr);
          return buffer;
        }

        void reset () {
          _buffer->clear();
        }

        void append (JasonSlice const& slice) {
          internalDump(&slice, nullptr);
        }

        void append (JasonSlice const* slice) {
          internalDump(slice, nullptr);
        }

        void appendString (char const* src, JasonLength len) {
          _buffer->reserve(2 + len);
          _buffer->push_back('"');
          dumpString(src, len);
          _buffer->push_back('"');
        }

        void appendString (std::string const& str) {<commit_msg>reserve outbuffer in advance<commit_after>////////////////////////////////////////////////////////////////////////////////
/// @brief Library to build up Jason documents.
///
/// DISCLAIMER
///
/// Copyright 2015 ArangoDB GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
///     http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author <NAME>
/// @author <NAME>
/// @author Copyright 2015, ArangoDB GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

#ifndef JASON_DUMPER_H
#define JASON_DUMPER_H 1

#include <string>
#include <cmath>
#include <functional>
#include <ostream>

#include "Jason.h"
#include "JasonBuffer.h"
#include "JasonException.h"
#include "JasonOptions.h"
#include "JasonSlice.h"
#include "JasonType.h"

namespace arangodb {
  namespace jason {

    // forward for fpconv function declared elsewhere
    int fpconv_dtoa (double fp, char dest[24]);

    // Dumps Jason into a JSON output string
    template<typename T, bool PrettyPrint = false>
    class JasonDumper {

      public:

        JasonOptions options;        
    
        enum UnsupportedTypeStrategy {
          StrategyNullify,
          StrategyFail
        };

        JasonDumper (JasonDumper const&) = delete;
        JasonDumper& operator= (JasonDumper const&) = delete;

        JasonDumper (T& buffer, UnsupportedTypeStrategy strategy = StrategyFail) 
          : _buffer(&buffer), _strategy(strategy), _indentation(0) {
        }

        ~JasonDumper () {
        }

        T* buffer () const {
          return _buffer;
        }

        friend std::ostream& operator<< (std::ostream& stream, JasonDumper const* dumper) {
          stream << *dumper->_buffer;
          return stream;
        }

        friend std::ostream& operator<< (std::ostream& stream, JasonDumper const& dumper) {
          stream << *dumper._buffer;
          return stream;
        }

        void setCallback (std::function<bool(T*, JasonSlice const*, JasonSlice const*)> const& callback) {
          _callback = callback;
        }

        void dump (JasonSlice const& slice) {
          _indentation = 0;
          _buffer->reserve(slice.byteSize());
          internalDump(&slice, nullptr);
        }

        void dump (JasonSlice const* slice) {
          _indentation = 0;
          _buffer->reserve(slice->byteSize());
          internalDump(slice, nullptr);
        }

        static void Dump (JasonSlice const& slice, T& buffer, UnsupportedTypeStrategy strategy = StrategyFail) {
          JasonDumper dumper(buffer, strategy);
          dumper.dump(slice);
        }

        static void Dump (JasonSlice const* slice, T& buffer, UnsupportedTypeStrategy strategy = StrategyFail) {
          JasonDumper dumper(buffer, strategy);
          dumper.dump(slice);
        }
        
        static T Dump (JasonSlice const& slice, UnsupportedTypeStrategy strategy = StrategyFail) {
          T buffer;
          JasonDumper dumper(buffer, strategy);
          dumper.dump(slice);
          return buffer;
        }

        static T Dump (JasonSlice const* slice, UnsupportedTypeStrategy strategy = StrategyFail) {
          T buffer;
          JasonDumper dumper(buffer, strategy);
          dumper.dump(slice);
          return buffer;
        }

        void reset () {
          _buffer->clear();
        }

        void append (JasonSlice const& slice) {
          internalDump(&slice, nullptr);
        }

        void append (JasonSlice const* slice) {
          internalDump(slice, nullptr);
        }

        void appendString (char const* src, JasonLength len) {
          _buffer->reserve(2 + len);
          _buffer->push_back('"');
          dumpString(src, len);
          _buffer->push_back('"');
        }

        void appendString (std::string const& str) {
{'fraction_non_alphanumeric': 0.1389051009688339, 'fraction_numerical': 0.004318898097350298, 'mean_word_length': 2.0853438962909614, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3998565', 'n_tokens_mistral': 2334, 'n_tokens_neox': 2196, 'n_words': 698}