<commit_before>// The MIT License (MIT)
// capi Fast CSV Streams 0.5.0 Beta
// Copyright (C) 2017, by <NAME> (<EMAIL>)
//
// http://opensource.org/licenses/MIT
//
// version 0.5.0  : First Commit
// version 0.5.1  : Fix Input Stream reading char exception
// version 0.5.2  : Add NChar class and its unit tests. Remove CHAR_AS_ASCII macro.

#ifndef CSV_STREAMS_H
	#define CSV_STREAMS_H

#include <string>
#include <cstdio>
#include <algorithm>
#include <stdexcept>

#ifdef USE_BOOST_SPIRIT_QI
	#include <boost/spirit/include/qi.hpp>
#endif

#define NEWLINE '\n'

#ifdef _WIN32
#define MY_FUNC_SIG __FUNCSIG__
#else
#define MY_FUNC_SIG __PRETTY_FUNCTION__
#endif

namespace capi
{
	namespace csv
	{
		struct NChar
		{
			explicit NChar(char& ch_) : ch(ch_) {}
			const char& getChar() const { return ch; }
			char& getChar() { return ch; }
			void setChar(char ch_) { ch = ch_; }
		private:
			char& ch;
		};

#ifdef USE_BOOST_SPIRIT_QI
		template<typename string_type>
		inline bool str_to_value(const string_type& src, double& dest)
		{
			namespace qi = boost::spirit::qi;

			return qi::parse(std::cbegin(src), std::cend(src), qi::double_, dest);
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, float& dest)
		{
			namespace qi = boost::spirit::qi;

			return qi::parse(std::cbegin(src), std::cend(src), qi::float_, dest);
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, long long& dest)
		{
			namespace qi = boost::spirit::qi;

			return qi::parse(std::cbegin(src), std::cend(src), qi::long_long, dest);
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, unsigned long long& dest)
		{
			namespace qi = boost::spirit::qi;

			return qi::parse(std::cbegin(src), std::cend(src), qi::ulong_long, dest);
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, int& dest)
		{
			namespace qi = boost::spirit::qi;

			return qi::parse(std::cbegin(src), std::cend(src), qi::int_, dest);
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, unsigned int& dest)
		{
			namespace qi = boost::spirit::qi;

			return qi::parse(std::cbegin(src), std::cend(src), qi::uint_, dest);
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, short& dest)
		{
			namespace qi = boost::spirit::qi;

			return qi::parse(std::cbegin(src), std::cend(src), qi::short_, dest);
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, unsigned short& dest)
		{
			namespace qi = boost::spirit::qi;

			return qi::parse(std::cbegin(src), std::cend(src), qi::ushort_, dest);
		}
#else
		template<typename string_type>
		inline bool str_to_value(const string_type& src, double& dest)
		{
			try
			{
				dest = std::stod(src);
			}
			catch (std::invalid_argument&)
			{
				return false;
			}
			catch (std::out_of_range&)
			{
				return false;
			}
			return true;
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, float& dest)
		{
			try
			{
				dest = std::stof(src);
			}
			catch (std::invalid_argument&)
			{
				return false;
			}
			catch (std::out_of_range&)
			{
				return false;
			}
			return true;
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, long long& dest)
		{
			try
			{
				dest = std::stoll(src);
			}
			catch (std::invalid_argument&)
			{
				return false;
			}
			catch (std::out_of_range&)
			{
				return false;
			}
			return true;
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, unsigned long long& dest)<commit_msg>Use Google double-conversion if USE_GOOGLE_DCONV is defined and USE_BOOST_SPIRIT_QI not defined<commit_after>// The MIT License (MIT)
// capi Fast CSV Streams 0.5.0 Beta
// Copyright (C) 2017, by <NAME> (<EMAIL>)
//
// http://opensource.org/licenses/MIT
//
// version 0.5.0  : First Commit
// version 0.5.1  : Fix Input Stream reading char exception
// version 0.5.2  : Add NChar class and its unit tests. Remove CHAR_AS_ASCII macro.

#ifndef CSV_STREAMS_H
	#define CSV_STREAMS_H

#include <string>
#include <cstdio>
#include <algorithm>
#include <stdexcept>

#ifdef USE_BOOST_SPIRIT_QI
	#include <boost/spirit/include/qi.hpp>
#endif

#ifdef USE_GOOGLE_DCONV
	#include "double_conversion/double-conversion.h"
#endif

#define NEWLINE '\n'

#ifdef _WIN32
#define MY_FUNC_SIG __FUNCSIG__
#else
#define MY_FUNC_SIG __PRETTY_FUNCTION__
#endif

namespace capi
{
	namespace csv
	{
		struct NChar
		{
			explicit NChar(char& ch_) : ch(ch_) {}
			const char& getChar() const { return ch; }
			char& getChar() { return ch; }
			void setChar(char ch_) { ch = ch_; }
		private:
			char& ch;
		};

#ifdef USE_BOOST_SPIRIT_QI
		template<typename string_type>
		inline bool str_to_value(const string_type& src, double& dest)
		{
			namespace qi = boost::spirit::qi;

			return qi::parse(std::cbegin(src), std::cend(src), qi::double_, dest);
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, float& dest)
		{
			namespace qi = boost::spirit::qi;

			return qi::parse(std::cbegin(src), std::cend(src), qi::float_, dest);
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, long long& dest)
		{
			namespace qi = boost::spirit::qi;

			return qi::parse(std::cbegin(src), std::cend(src), qi::long_long, dest);
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, unsigned long long& dest)
		{
			namespace qi = boost::spirit::qi;

			return qi::parse(std::cbegin(src), std::cend(src), qi::ulong_long, dest);
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, int& dest)
		{
			namespace qi = boost::spirit::qi;

			return qi::parse(std::cbegin(src), std::cend(src), qi::int_, dest);
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, unsigned int& dest)
		{
			namespace qi = boost::spirit::qi;

			return qi::parse(std::cbegin(src), std::cend(src), qi::uint_, dest);
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, short& dest)
		{
			namespace qi = boost::spirit::qi;

			return qi::parse(std::cbegin(src), std::cend(src), qi::short_, dest);
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, unsigned short& dest)
		{
			namespace qi = boost::spirit::qi;

			return qi::parse(std::cbegin(src), std::cend(src), qi::ushort_, dest);
		}
#else
#ifdef USE_GOOGLE_DCONV
		template<typename string_type>
		inline bool str_to_value(const string_type& src, double& dest)
		{
			StringToDoubleConverter conv(StringToDoubleConverter::NO_FLAGS, 0.0, NAN, "infinity", "nan");
			int processed_characters_count = 0;
			dest = conv.StringToDouble(src.c_str(), src.size(), &processed_characters_count);

			return true;
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, float& dest)
		{
			StringToDoubleConverter conv(StringToDoubleConverter::NO_FLAGS, 0.0, NAN, "infinity", "nan");
			int processed_characters_count = 0;
			dest = conv.StringToFloat(src.c_str(), src.size(), &processed_characters_count);

			return true;
		}
#else
		template<typename string_type>
		inline bool str_to_value(const string_type& src, double& dest)
		{
			try
			{
				dest = std::stod(src);
			}
			catch (std::invalid_argument&)
			{
				return false;
			}
			catch (std::out_of_range&)
			{
				return false;
			}
			return true;
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, float& dest)
		{
			try
			{
				dest = std::stof(src);
			}
			catch (std::invalid_argument&)
			{
				return false;
			}
			catch (std::out_of_range&)
			{
				return false;
			}
			return true;
		}
#endif
		template<typename string_type>
		inline bool str_to_value(const string_type& src, long long& dest)
		{
			try
			{
				dest = std::stoll(src);
			}
			catch (std::invalid_argument&)
			{
				return false;
			}
			catch (std::out_of_range&)
			{
				return false;
			}
			return true;
		}

		template<typename string_type>
		inline bool str_to_value(const string_type& src, unsigned long long& dest)
{'fraction_non_alphanumeric': 0.12984192108121154, 'fraction_numerical': 0.005068179075660673, 'mean_word_length': 4.047503045066992, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, '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': '4646574', 'n_tokens_mistral': 3483, 'n_tokens_neox': 2971, 'n_words': 710}