summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Holesovsky <kendy@suse.cz>2012-07-27 21:44:16 +0200
committerJan Holesovsky <kendy@suse.cz>2012-07-27 21:44:16 +0200
commitd34727b185d8f6579e4757ba29086a0100ae8e40 (patch)
tree3f2b8e45db33b6e7b9e8117ef733ac6a3c07828c
parentf206149e641e9b50df8bbbc02b2179ec4380018e (diff)
Update the tabs -> space rewriting algoritm to what Norbert did.
[so that it produces the same results as the onegit migration]
-rw-r--r--filter.cxx46
-rw-r--r--filter.hxx9
2 files changed, 36 insertions, 19 deletions
diff --git a/filter.cxx b/filter.cxx
index 05158d8..6bbbc39 100644
--- a/filter.cxx
+++ b/filter.cxx
@@ -29,7 +29,8 @@ struct Tabs {
static Tabs tabs;
Filter::Filter( const string& fname_ )
- : tabs_to_spaces( true ),
+ : column( 0 ),
+ spaces_to_write( 0 ),
type( NO_FILTER )
{
data.reserve( 16384 );
@@ -45,20 +46,35 @@ Filter::Filter( const string& fname_ )
}
}
-inline void addDataLoop( char*& dest, char what, bool& tabs_to_spaces, int no_spaces )
+inline void addDataLoop( char*& dest, char what, int& column, int& spaces_to_write, int no_spaces )
{
- if ( what == '\t' && tabs_to_spaces )
+ if ( what == '\t' )
{
- for ( int i = 0; i < no_spaces; ++i )
- *dest++ = ' ';
- return;
+ const int tab_size = no_spaces - ( column % no_spaces );
+ column += tab_size;
+ spaces_to_write += tab_size;
}
else if ( what == '\n' )
- tabs_to_spaces = true;
- else if ( what != ' ' )
- tabs_to_spaces = false;
+ {
+ *dest++ = what;
+ column = 0;
+ spaces_to_write = 0;
+ }
+ else if ( what == ' ' )
+ {
+ ++column;
+ ++spaces_to_write;
+ }
+ else
+ {
+ // write out any spaces that we need
+ for ( int i = 0; i < spaces_to_write; ++i )
+ *dest++ = ' ';
- *dest++ = what;
+ *dest++ = what;
+ ++column;
+ spaces_to_write = 0;
+ }
}
void Filter::addData( const char* data_, size_t len_ )
@@ -70,12 +86,12 @@ void Filter::addData( const char* data_, size_t len_ )
}
// type == FILTER_TABS
- char tmp[4*len_];
+ char tmp[tabs.spaces*len_];
char *dest = tmp;
- // convert the leading tabs to N spaces (according to tabs.spaces)
+ // convert the tabs to spaces (according to tabs.spaces)
for ( const char* it = data_; it < data_ + len_; ++it )
- addDataLoop( dest, *it, tabs_to_spaces, tabs.spaces );
+ addDataLoop( dest, *it, column, spaces_to_write, tabs.spaces );
data.append( tmp, dest - tmp );
}
@@ -89,12 +105,12 @@ void Filter::addData( const string& data_ )
}
// type == FILTER_TABS
- char *tmp = new char[4*data_.size()];
+ char *tmp = new char[tabs.spaces*data_.size()];
char *dest = tmp;
// convert the leading tabs to N spaces (according to tabs.spaces)
for ( const char* it = data_.data(), *end = data_.data() + data_.size(); it < end; ++it )
- addDataLoop( dest, *it, tabs_to_spaces, tabs.spaces );
+ addDataLoop( dest, *it, column, spaces_to_write, tabs.spaces );
data.append( tmp, dest - tmp );
diff --git a/filter.hxx b/filter.hxx
index b2e91f8..df541b1 100644
--- a/filter.hxx
+++ b/filter.hxx
@@ -15,10 +15,11 @@ class Filter
{
std::string data;
- /// Should we convert?
- ///
- /// We have to remember it between addData() calls.
- bool tabs_to_spaces;
+ /// Current column in the output (resets with every \n).
+ int column;
+
+ /// In order to strip trailing spaces, we do not write them immediately.
+ int spaces_to_write;
enum FilterType { NO_FILTER, FILTER_TABS };