* A simple, header-only library to translate from English to Pig Latin.
*
@@ -40,7 +40,10 @@
#ifndef ANSLATORTRAY_HPP
#define ANSLATORTRAY_HPP
#if __cplusplus >= 201103L
#define ANSLATORTRAY_VERSION 0.3.0
#if __cplusplus >= 201103L//supports C++11 and later
#include<string>
#include<sstream>
@@ -50,79 +53,76 @@
//#include <iterator>
//#include <cstring>
/** \namespace anslatortray
*
* \brief Namespace containing functions to translate from English to Pig Latin.
*/
namespaceanslatortray
namespaceanslatortray//Definitions
{
/** \brief Translates a single complex English word to pig latin. (more robust)
/** \brief Translates a single complex English word to Pig Latin. (more robust)
*
* Unlike wordToPig, this function also handles punctuation (not seperated by whitespace), singular possesion ('s) and capatilizes the first letter if the original english word was capatilized.\n
* Imperfect results with plural words (s) and words with mutiple possesion (s')
* Imperfect results with plural words (ending in "s") and words with mutiple possesion (ending in "s'")
std::string::size_typewordStartIndex{englishWord.find_first_of(Characters::Letters::ALL)};//after any beginning punctuation
std::string::size_typewordEndIndex{englishWord.find_last_of(Characters::APOSTROPHE)};//try to find an ending apostrophe for possesion or a contraction, seperate from translation
if(wordEndIndex==std::string::npos)//if there is no apostrophe
wordEndIndex={englishWord.find_last_of(Characters::Letters::ALL)+1};//find the last letter in the string to use as ending
//extract it and translate
std::stringactualWord={englishWord.substr(wordStartIndex,wordEndIndex-wordStartIndex)};//2nd param is count between start and end of actual word
std::stringpig{wordToPig(actualWord)};//translate English word
//capatilization handeling
if(std::all_of(std::begin(actualWord),std::end(actualWord),isupper))//if entire original word was uppercase//fixme why no std::toupper
std::transform(std::begin(pig),std::end(pig),std::begin(pig),toupper);//make entire translated word uppercase
else
{
std::transform(std::begin(pig),std::end(pig),std::begin(pig),tolower);//make entire translated word lowercase//fixme why no std::tolower
if(std::isupper(actualWord[0]))//if original word had an uppercase first letter
pig[0]={static_cast<char>(std::toupper(pig[0]))};//new word should have uppercase first letter; have to cast int to char
}
//prefix punctuation + pigified word + suffix punctuation
std::stringresult{englishWord.substr(0,wordStartIndex)};//up to the start of the word
result+={pig};//translated word from earlier
result+={englishWord.substr(wordEndIndex)};//from end of the word to the end of the string