1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
// stringcase.hpp
//
// Copyright (c) 2009 Michael Thomas Greer
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#pragma once
#ifndef STRINGCASE_HPP
#define STRINGCASE_HPP
#include <algorithm>
#include <cctype>
#include <functional>
#include <string>
template <typename CharType, typename TraitsType, typename AllocType>
std::basic_string <CharType, TraitsType, AllocType>
uppercase( const std::basic_string <CharType, TraitsType, AllocType> & s )
{
std::basic_string <CharType, TraitsType, AllocType> result( s.length(), '\0' );
std::transform(
s.begin(),
s.end(),
result.begin(),
std::ptr_fun <int, int> ( std::toupper )
);
return result;
}
template <typename CharType, typename TraitsType, typename AllocType>
std::basic_string <CharType, TraitsType, AllocType>
lowercase( const std::basic_string <CharType, TraitsType, AllocType> & s )
{
std::basic_string <CharType, TraitsType, AllocType> result( s.length(), '\0' );
std::transform(
s.begin(),
s.end(),
result.begin(),
std::ptr_fun <int, int> ( std::tolower )
);
return result;
}
template <typename CharType, typename TraitsType, typename AllocType>
std::basic_string <CharType, TraitsType, AllocType>
titlecase( const std::basic_string <CharType, TraitsType, AllocType> & s )
{
std::basic_string <CharType, TraitsType, AllocType> result = lowercase( s );
typename std::basic_string <CharType, TraitsType, AllocType> ::iterator iter = result.begin();
while (iter != result.end())
{
*iter = toupper( *iter );
iter = std::find_if( iter, result.end(), std::not1( std::ptr_fun <int, int> ( std::isalpha ) ) );
iter = std::find_if( iter, result.end(), std::ptr_fun <int, int> ( std::isalpha ) );
}
return result;
}
#endif
// end stringcase.hpp
|