<commit_before>// Copyright (c) 2013, <NAME>. All rights reserved.

#include "exception.hpp"
#include "array_utilities.hpp"
#include "assert.hpp"
#include "log.hpp"
#include <boost/static_assert.hpp>
#include <cstring>
#include <iostream>
#include <string>

using jewel::num_elements;
using std::endl;
using std::strlen;
using std::strncat;
using std::strncpy;
using std::string;

namespace jewel
{


Exception::Exception() throw()
{
}

Exception::Exception(char const* p_message) throw():
	m_message(p_message)
{
	if (m_message.size() > max_message_size()) truncate_message();
}

Exception::Exception(Exception const& rhs) throw():
	m_message(rhs.m_message)
{
	if (m_message.size() > max_message_size()) truncate_message();
}

Exception::~Exception() throw()
{
}

CappedString<Exception::truncation_flag_capacity>
Exception::truncation_flag()
{
	CappedString<truncation_flag_capacity> const ret("[TRUNCATED]");
	JEWEL_ASSERT (!ret.is_truncated());
	JEWEL_ASSERT (ret.size() == truncation_flag_capacity);
	return ret;
}

char const* Exception::what() const throw()
{
	return m_message.c_str();
}

void
Exception::truncate_message()
{
	// TODO This concatenation would be better handled within
	// CappedString.
	char buf[message_capacity + 1];
	size_t const truncation_flag_size = truncation_flag().size();
	size_t const truncated_message_size =
		message_capacity - truncation_flag_size;
	strncpy(buf, m_message.c_str(), truncated_message_size);
	buf[truncated_message_size] = '\0';
	JEWEL_ASSERT
	(	truncation_flag_size <=
		(message_capacity - truncated_message_size)
	);
	strcat(buf, truncation_flag().c_str());
	buf[message_capacity] = 1;  // Doesn't matter as long as it's not '\0';
	// We still want m_message to be marked as truncated!
	m_message = CappedString<message_capacity>(buf);
	return;
}

size_t Exception::max_message_size() throw()
{
	return message_capacity - truncation_flag_capacity;
}







<commit_msg>Used CappedString iterators and std::copy algorithm to simplify implementation of jewel::Exception.<commit_after>// Copyright (c) 2013, <NAME>. All rights reserved.

#include "exception.hpp"
#include "array_utilities.hpp"
#include "assert.hpp"
#include "log.hpp"
#include <boost/static_assert.hpp>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>

using jewel::num_elements;
using std::copy;
using std::endl;

namespace jewel
{


Exception::Exception() throw()
{
}

Exception::Exception(char const* p_message) throw():
	m_message(p_message)
{
	if (m_message.size() > max_message_size()) truncate_message();
}

Exception::Exception(Exception const& rhs) throw():
	m_message(rhs.m_message)
{
	if (m_message.size() > max_message_size()) truncate_message();
}

Exception::~Exception() throw()
{
}

CappedString<Exception::truncation_flag_capacity>
Exception::truncation_flag()
{
	CappedString<truncation_flag_capacity> const ret("[TRUNCATED]");
	JEWEL_ASSERT (!ret.is_truncated());
	JEWEL_ASSERT (ret.size() == truncation_flag_capacity);
	return ret;
}

char const* Exception::what() const throw()
{
	return m_message.c_str();
}

void
Exception::truncate_message()
{
	CappedString<truncation_flag_capacity> const trunc_flag =
		truncation_flag();
	CappedString<message_capacity>::iterator out_it = copy
	(	trunc_flag.begin(),
		trunc_flag.end(),
		m_message.begin() + (message_capacity - trunc_flag.size())
	);
	*out_it = '\0';
	return;
}

size_t Exception::max_message_size() throw()
{
	return message_capacity - truncation_flag_capacity;
}








{'fraction_non_alphanumeric': 0.13967437874892888, 'fraction_numerical': 0.0037132247929163096, 'mean_word_length': 6.596529284164859, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 25, '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': '1400570', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1177, 'n_words': 249}