Skip to content

Instantly share code, notes, and snippets.

@mattsan
Created August 12, 2011 02:06
Show Gist options
  • Save mattsan/1141284 to your computer and use it in GitHub Desktop.
Save mattsan/1141284 to your computer and use it in GitHub Desktop.
#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
#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