<commit_before>/*
 * numtext.h
 *
 *  Created on: 12.10.2013
 *      Author: ondra
 */

#ifndef LIGHTSPEED_TEXT_NUMTEXT_H_
#define LIGHTSPEED_TEXT_NUMTEXT_H_
#include "../containers/autoArray.h"
#include "../memory/smallAlloc.h"
#include "../containers/constStr.h"


namespace LightSpeed {

	///general conversion anything to string
	/** Template has two arguments
	 * @tparam T type from which convert
	 * @tparam ChT type of string. There can be either char or wchar_t, or another
	 * type representing one character
	 *
	 * Result is object, which implements ConstStringT and can be used everywhere
	 * string is requested. For char and wchar_t, strings are terminated by zero
	 * character, so it should be easy to extract string and use it in standard
	 * C library
	 *
	 * @code
	 * textOut(ToString<natural>(10));
	 * @endcode
	 *
	 * Call textOut with text containing converted number
	 *
	 * Every conversion object should have standard constructor, which accept
	 * single argument - object to convert, and optionally extended constructor
	 * where you can specify more informations
	 *
	 * 	 */
	template<typename T, typename ChT = char>
	class ToString;

	template<typename ChT, natural bufBaseSize>
	class ToStringBase: public ConstStringT<ChT> {
	protected:

		typedef AutoArrayStream<ChT, SmallAlloc<bufBaseSize> > Buffer;
		Buffer buffer;

		void commitBuffer() {
			this->len = buffer.length();
			StringBase<ChT>::writeZeroChar(buffer);
			this->refdata = buffer.data();
		}

		operator const ChT *() const {return this->data();}

	};

	template<typename NumType, typename ChT, natural bufsize>
	class ToStringUnsigned: public ToStringBase<ChT,bufsize>{
	public:
		ToStringUnsigned(NumType val, natural base, bool up) {extractFirst(val,base,up);}
	protected:
		void extractFirst(NumType val, natural base, bool up) {
			if (val == 0) this->buffer.write(ChT('0'));
			else extract(val,base,up);
			this->commitBuffer();
		}
		void extract(NumType val, natural base, bool up) {
			if (val >= base) extract(val/base,base,up);
			natural x = val % base;
			if (x > 9) this->buffer.write(ChT((up?'A':'a')+x - 10));
			else this->buffer.write(ChT('0'+x));
		}
	};

	template<typename NumType, typename ChT, natural bufsize>
	class ToStringSigned: public ToStringUnsigned<NumType,ChT,bufsize>{
	public:
		ToStringSigned(NumType val, natural base, bool up) {extractFirst(val,base,up);}
	protected:
		void extractFirst(NumType val, natural base, bool up) {
			if (val == 0) this->buffer.write(ChT('0'));
			else {
				if (val < 0) {
					this->buffer.write(ChT('-'));
					this->extract(-val,base,up);
				} else {
					this->extract(val,base,up);
				}
			}
			this->commitBuffer();
		}
	};
	template<typename ChT>
	class ToString<natural,ChT>: public ToStringUnsigned<natural,ChT,23> {
	public:
		 ToString(natural val, natural base=10, bool up = false)
			:ToStringUnsigned<natural,ChT,23>(val,base,up) {}
	};<commit_msg>fixed toString compile error for linteger<commit_after>/*
 * numtext.h
 *
 *  Created on: 12.10.2013
 *      Author: ondra
 */

#ifndef LIGHTSPEED_TEXT_NUMTEXT_H_
#define LIGHTSPEED_TEXT_NUMTEXT_H_
#include "../containers/autoArray.h"
#include "../memory/smallAlloc.h"
#include "../containers/constStr.h"


namespace LightSpeed {

	///general conversion anything to string
	/** Template has two arguments
	 * @tparam T type from which convert
	 * @tparam ChT type of string. There can be either char or wchar_t, or another
	 * type representing one character
	 *
	 * Result is object, which implements ConstStringT and can be used everywhere
	 * string is requested. For char and wchar_t, strings are terminated by zero
	 * character, so it should be easy to extract string and use it in standard
	 * C library
	 *
	 * @code
	 * textOut(ToString<natural>(10));
	 * @endcode
	 *
	 * Call textOut with text containing converted number
	 *
	 * Every conversion object should have standard constructor, which accept
	 * single argument - object to convert, and optionally extended constructor
	 * where you can specify more informations
	 *
	 * 	 */
	template<typename T, typename ChT = char>
	class ToString;

	template<typename ChT, natural bufBaseSize>
	class ToStringBase: public ConstStringT<ChT> {
	protected:

		typedef AutoArrayStream<ChT, SmallAlloc<bufBaseSize> > Buffer;
		Buffer buffer;

		void commitBuffer() {
			this->len = buffer.length();
			StringBase<ChT>::writeZeroChar(buffer);
			this->refdata = buffer.data();
		}

		operator const ChT *() const {return this->data();}

	};

	template<typename NumType, typename ChT, natural bufsize>
	class ToStringUnsigned: public ToStringBase<ChT,bufsize>{
	public:
		ToStringUnsigned(NumType val, natural base, bool up) {extractFirst(val,base,up);}
	protected:
		void extractFirst(NumType val, natural base, bool up) {
			if (val == 0) this->buffer.write(ChT('0'));
			else extract(val,base,up);
			this->commitBuffer();
		}
		void extract(NumType val, natural base, bool up) {
			if (val >= base) extract(val/base,base,up);
			natural x = val % base;
			if (x > 9) this->buffer.write(ChT((up?'A':'a')+x - 10));
			else this->buffer.write(ChT('0'+x));
		}
	};

	template<typename NumType, typename ChT, natural bufsize>
	class ToStringSigned : public  ToStringBase<ChT, bufsize>{
	public:
		ToStringSigned(NumType val, natural base, bool up) {extractFirst(val,base,up);}
	protected:
		void extractFirst(NumType val, natural base, bool up) {
			if (val == 0) this->buffer.write(ChT('0'));
			else {
				if (val < 0) {
					this->buffer.write(ChT('-'));
					this->extract(-val,base,up);
				} else {
					this->extract(val,base,up);
				}
			}
			this->commitBuffer();
		}
		void extract(NumType val, natural base, bool up) {
			if (val >= base) extract(val / base, base, up);
			natural x = val % base;
			if (x > 9) this->buffer.write(ChT((up ? 'A' : 'a') + x - 10));
			else this->buffer.write(ChT('0' + x));
		}

	};
	template<typename ChT>
	class ToString<natural,ChT>: public ToStringUnsigned<natural,ChT,23> {
	public:
		 ToString(natural val, natural base=10, bool up = false)
			:ToStringUnsigned<natural,ChT,23>(val,base,up) {}
	};
{'fraction_non_alphanumeric': 0.12856909773209332, 'fraction_numerical': 0.00881057268722467, 'mean_word_length': 4.0535861500412205, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 65, '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': '2119809', 'n_tokens_mistral': 2196, 'n_tokens_neox': 1939, 'n_words': 573}