Created
August 12, 2011 02:06
-
-
Save mattsan/1141284 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef EMATTSAN_TABTOSPACE_H | |
#define EMATTSAN_TABTOSPACE_H | |
namespace emattsan | |
{ | |
template<typename InputIterator, typename OutputIterator> | |
OutputIterator tabToSpace(int tabSize, InputIterator begin, InputIterator end, OutputIterator out) | |
{ | |
int spacingSize = tabSize; | |
for(InputIterator i = begin; i != end; ++i) | |
{ | |
if(*i == '\t') | |
{ | |
while(spacingSize != 0) | |
{ | |
*out = ' '; | |
++out; | |
--spacingSize; | |
} | |
spacingSize = tabSize; | |
} | |
else | |
{ | |
*out = *i; | |
++out; | |
--spacingSize; | |
if((spacingSize == 0) || (*i == '\n') || (*i == '\r')) | |
{ | |
spacingSize = tabSize; | |
} | |
} | |
} | |
return out; | |
} | |
} // namespace emattsan | |
#endif//EMATTSAN_TABTOSPACE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "TabToSpace.h" | |
#include <iostream> | |
#include <string> | |
#include <iterator> | |
void testStdio() | |
{ | |
std::cin.unsetf(std::ios::skipws); | |
std::istream_iterator<char> begin(std::cin); | |
std::istream_iterator<char> end; | |
std::ostream_iterator<char> out(std::cout); | |
emattsan::tabToSpace(8, begin, end, out); | |
} | |
void testString() | |
{ | |
std::string source("a\n\tb\n\t\tc\n\t\td\n\t\te\n\tf\n\tg\nh\n"); | |
std::string result; | |
std::ostream_iterator<char> out(std::cout); | |
emattsan::tabToSpace(8, source.begin(), source.end(), std::back_inserter(result)); | |
std::cout << result; | |
} | |
int main(int, char*[]) | |
{ | |
testString(); | |
testStdio(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment