<commit_before>/*
 * ConstString.h
 *
 *  Created on: Jun 20, 2016
 *      Author: balszun
 */

#ifndef LIBS_MART_COMMON_EXPERIMENTAL_CONSTSTR_H_
#define LIBS_MART_COMMON_EXPERIMENTAL_CONSTSTR_H_
#pragma once

/* ######## INCLUDES ######### */
/* Standard Library Includes */
#include <algorithm>
#include <cassert>
#include "../cpp_std/utility.h"
#include "../cpp_std/memory.h"

/* Proprietary Library Includes */

/* Project Includes */
#include "StringView.h"

namespace mart {

/**
 * ConstString is a ref-counted String implementation, that doesn't allow the modification of the underlying storage at all.
 *
 * One particular property is that when it is constructed from a "const char [N]" argument it is assumed, that this represents
 * a string litteral, in which case ConstString doesn't perform any copy or dynamic memory allocation and also
 * copying the ConstString will not result in any copyies or refcount updates.
 *
 * This header also provides a function that can efficently concatenate multiple string like objects, because it
 * needs only a single dynamic memory allocation
 *
 */
class ConstString : public StringView {
public:
	/* #################### CTORS ########################## */
	//Default ConstString points at empty string
	ConstString() :
		StringView(mart::EmptyStringView)
	{};

	explicit ConstString(StringView other)
	{
		_copyFrom(other);
	}

	// don't accept c-strings in the form of pointer
	// if you need to create a const_string from a c string use the <const_string(const char*, size_t)> constructor
	// if you need to create a ConstString form a string literal, use the <ConstString(const char(&other)[N])> constructor
	template<class T>
	ConstString(T const * const& other) = delete;

	//NOTE: Use only for string literals (arrays with static storage duration)!!!
	template<size_t N>
	ConstString(const char(&other)[N]) noexcept :
		StringView(other)
		//we don't have to initialize the shared_ptr to anything as string litterals already have static lifetime
	{
		static_assert(N >= 1, "");
	}
protected:
	/** private constructor, that takes ownership of a buffer and a size (used in _copyFrom and _concat_impl)
	 *
	 * Implementation notes:
	 * - creating from shared_ptr doesn't bring any advantage, as you can't use std::make_shared for an array
	 * - This automatically stores the correct deleter in the shared_ptr (by default shared_ptr<const char> would use delete instead of delete[]
	 */
	ConstString(std::unique_ptr<const char[]> data, size_t size) :
		StringView(data.get(), size),
		_data(std::move(data))
	{
		assert(_start != nullptr);<commit_msg>Make char litteral constructor of ConstString constexpr<commit_after>/*
 * ConstString.h
 *
 *  Created on: Jun 20, 2016
 *      Author: balszun
 */

#ifndef LIBS_MART_COMMON_EXPERIMENTAL_CONSTSTR_H_
#define LIBS_MART_COMMON_EXPERIMENTAL_CONSTSTR_H_
#pragma once

/* ######## INCLUDES ######### */
/* Standard Library Includes */
#include <algorithm>
#include <cassert>
#include "../cpp_std/utility.h"
#include "../cpp_std/memory.h"

/* Proprietary Library Includes */

/* Project Includes */
#include "StringView.h"

namespace mart {

/**
 * ConstString is a ref-counted String implementation, that doesn't allow the modification of the underlying storage at all.
 *
 * One particular property is that when it is constructed from a "const char [N]" argument it is assumed, that this represents
 * a string litteral, in which case ConstString doesn't perform any copy or dynamic memory allocation and also
 * copying the ConstString will not result in any copyies or refcount updates.
 *
 * This header also provides a function that can efficently concatenate multiple string like objects, because it
 * needs only a single dynamic memory allocation
 *
 */
class ConstString : public StringView {
public:
	/* #################### CTORS ########################## */
	//Default ConstString points at empty string
	ConstString() :
		StringView(mart::EmptyStringView)
	{};

	explicit ConstString(StringView other)
	{
		_copyFrom(other);
	}

	//NOTE: Use only for string literals (arrays with static storage duration)!!!
	template<size_t N>
	constexpr ConstString(const char(&other)[N]) noexcept :
		StringView(other)
		//we don't have to initialize the shared_ptr to anything as string litterals already have static lifetime
	{
		static_assert(N >= 1, "");
	}

	// don't accept c-strings in the form of pointer
	// if you need to create a const_string from a c string use the <const_string(const char*, size_t)> constructor
	// if you need to create a ConstString form a string literal, use the <ConstString(const char(&other)[N])> constructor
	template<class T>
	ConstString(T const * const& other) = delete;

protected:
	/** private constructor, that takes ownership of a buffer and a size (used in _copyFrom and _concat_impl)
	 *
	 * Implementation notes:
	 * - creating from shared_ptr doesn't bring any advantage, as you can't use std::make_shared for an array
	 * - This automatically stores the correct deleter in the shared_ptr (by default shared_ptr<const char> would use delete instead of delete[]
	 */
	ConstString(std::unique_ptr<const char[]> data, size_t size) :
		StringView(data.get(), size),
		_data(std::move(data))
	{
		assert(_start != nullptr);
{'fraction_non_alphanumeric': 0.11269208878770631, 'fraction_numerical': 0.0026560424966799467, 'mean_word_length': 4.555321390937829, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, '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': '2329678', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1479, 'n_words': 586}