Skip to content
Snippets Groups Projects
string.cpp 2.72 KiB
Newer Older
Jan Trávníček's avatar
Jan Trávníček committed
 * string.cpp
 *
 * Created on: Apr 1, 2013
 * Author: Jan Travnicek
 */

#include "string.hpp"
#include <sstream>

std::string to_string ( const std::string & value ) {
	return value;
std::string to_string ( int value ) {
	return std::to_string ( value );
}

std::string to_string ( bool value ) {
	return std::to_string ( value );
}

std::string to_string ( long value ) {
	return std::to_string ( value );
}

std::string to_string ( long long value ) {
	return std::to_string ( value );
}

std::string to_string ( unsigned value ) {
	return std::to_string ( value );
}

std::string to_string ( unsigned long value ) {
	return std::to_string ( value );
}

std::string to_string ( unsigned long long value ) {
	return std::to_string ( value );
}

std::string to_string ( double value ) {
	return std::to_string ( value );
std::string to_string ( char value ) {
	return std::string ( 1, value );
}

template < >
std::string from_string ( const std::string & value ) {
	return value;
template < >
int from_string ( const std::string & value ) {
	return std::stoi ( value.c_str() );
template < >
bool from_string ( const std::string & value ) {
	if ( value == "true" || value == "1" )
		return true;
	else
		return false;
}

long from_string ( const std::string & value ) {
	return std::stol ( value.c_str() );
long long from_string ( const std::string & value ) {
	return std::stoll ( value.c_str() );
template < >
unsigned from_string ( const std::string & value ) {
	return std::stoul ( value.c_str() );
unsigned long from_string ( const std::string & value ) {
	return std::stoul ( value.c_str() );
unsigned long long from_string ( const std::string & value ) {
	return std::stoull ( value.c_str() );
Jan Trávníček's avatar
Jan Trávníček committed
template < >
double from_string ( const std::string & value ) {
	return std::stod ( value.c_str() );
std::string cstringToString ( char * param ) {
	std::string res ( param );

	free ( param );
	return res;
ext::vector < std::string > explode ( const std::string & source, const std::string & delimiter ) {
	ext::vector < std::string > res;
	size_t start_pos = 0;
	size_t end_pos;

	while ( ( end_pos = source.find ( delimiter, start_pos ) ) != std::string::npos ) {
		res.push_back ( source.substr ( start_pos, end_pos - start_pos ) );
		start_pos = end_pos + delimiter.size ( );
	}

	res.push_back ( source.substr ( start_pos, source.size ( ) ) );

	return res;
}

std::string implode ( const std::vector < std::string > & source, const std::string & delimiter ) {
	std::stringstream ss;
	bool first = true;
	for ( const std::string & str : source ) {
		if ( first )
			first = false;
		else
			ss << delimiter;

		ss << str;
	}

	return ss.str ( );
}

} /* namespace ext */