<commit_before>/*
 * Copyright (c) 2016 CartoDB. All rights reserved.
 * Copying and using this code is allowed only according
 * to license terms, as given in https://cartodb.com/terms/
 */

#ifndef _CARTO_MAPNIKVT_VALUEPARSER_H_
#define _CARTO_MAPNIKVT_VALUEPARSER_H_

#include "Value.h"

#include <memory>
#include <functional>

#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/karma_symbols.hpp>
#include <boost/spirit/include/karma_alternative.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_bind.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_function.hpp>

namespace carto { namespace mvt {
    namespace valgenimpl {
        namespace phx = boost::phoenix;
        namespace karma = boost::spirit::karma;
        namespace encoding = boost::spirit::iso8859_1;

        template <typename OutputIterator>
        struct Grammar : karma::grammar<OutputIterator, Value()> {
            Grammar() : Grammar::base_type(value) {
                using karma::_pass;
                using karma::_val;
                using karma::_1;

                esc_char.add('\a', "\\a")('\b', "\\b")('\f', "\\f")('\n', "\\n")
                            ('\r', "\\r")('\t', "\\t")('\v', "\\v")('\\', "\\\\")
                            ('\'', "\\\'")('"', "\\\"");

                string %=
                    '\'' << *(esc_char | karma::print | "\\x" << karma::hex) << '\'';

                value =
                      karma::lit("null")        [_pass = phx::bind(&isNullValue, _val)]
                    | karma::bool_                [_pass = phx::bind(&getBoolValue, _val, _1)]
                    | karma::long_long            [_pass = phx::bind(&getLongValue, _val, _1)]
                    | karma::double_            [_pass = phx::bind(&getDoubleValue, _val, _1)]
                    | string                    [_pass = phx::bind(&getStringValue, _val, _1)]
                    ;
            }

            karma::symbols<char, const char *> esc_char;
            karma::rule<OutputIterator, std::string()> string;
            karma::rule<OutputIterator, Value()> value;

        private:
            static bool isNullValue(const Value& val) {
                if (boost::get<boost::blank>(&val)) {
                    return true;
                }
                return false;
            }

            static bool getBoolValue(const Value& val, bool& result) {
                if (auto b = boost::get<bool>(&val)) {
                    result = *b;
                    return true;
                }
                return false;
            }

            static bool getLongValue(const Value& val, long long& result) {
                if (auto i = boost::get<long long>(&val)) {
                    result = *i;
                    return true;
                }<commit_msg>Formatting<commit_after>/*
 * Copyright (c) 2016 CartoDB. All rights reserved.
 * Copying and using this code is allowed only according
 * to license terms, as given in https://cartodb.com/terms/
 */

#ifndef _CARTO_MAPNIKVT_VALUEPARSER_H_
#define _CARTO_MAPNIKVT_VALUEPARSER_H_

#include "Value.h"

#include <memory>
#include <functional>

#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/karma_symbols.hpp>
#include <boost/spirit/include/karma_alternative.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_bind.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_function.hpp>

namespace carto { namespace mvt {
    namespace valgenimpl {
        namespace phx = boost::phoenix;
        namespace karma = boost::spirit::karma;
        namespace encoding = boost::spirit::iso8859_1;

        template <typename OutputIterator>
        struct Grammar : karma::grammar<OutputIterator, Value()> {
            Grammar() : Grammar::base_type(value) {
                using karma::_pass;
                using karma::_val;
                using karma::_1;

                esc_char.add('\a', "\\a")('\b', "\\b")('\f', "\\f")('\n', "\\n")
                            ('\r', "\\r")('\t', "\\t")('\v', "\\v")('\\', "\\\\")
                            ('\'', "\\\'")('"', "\\\"");

                string %=
                    '\'' << *(esc_char | karma::print | "\\x" << karma::hex) << '\'';

                value =
                      karma::lit("null")        [_pass = phx::bind(&isNullValue, _val)]
                    | karma::bool_                [_pass = phx::bind(&getBoolValue, _val, _1)]
                    | karma::long_long            [_pass = phx::bind(&getLongValue, _val, _1)]
                    | karma::double_            [_pass = phx::bind(&getDoubleValue, _val, _1)]
                    | string                    [_pass = phx::bind(&getStringValue, _val, _1)]
                    ;
            }

            karma::symbols<char, const char*> esc_char;
            karma::rule<OutputIterator, std::string()> string;
            karma::rule<OutputIterator, Value()> value;

        private:
            static bool isNullValue(const Value& val) {
                if (boost::get<boost::blank>(&val)) {
                    return true;
                }
                return false;
            }

            static bool getBoolValue(const Value& val, bool& result) {
                if (auto b = boost::get<bool>(&val)) {
                    result = *b;
                    return true;
                }
                return false;
            }

            static bool getLongValue(const Value& val, long long& result) {
                if (auto i = boost::get<long long>(&val)) {
                    result = *i;
                    return true;
                }
{'fraction_non_alphanumeric': 0.14782908339076498, 'fraction_numerical': 0.004824259131633356, 'mean_word_length': 1.6949860724233983, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 37, 'https://': 2, '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': '5556801', 'n_tokens_mistral': 1773, 'n_tokens_neox': 1665, 'n_words': 346}