29 #include "NCWordWrapper.h"
32 #define DEFAULT_LINE_WIDTH 78
40 _lineWidth( DEFAULT_LINE_WIDTH ),
60 if ( width != _lineWidth )
72 _lineWidth = DEFAULT_LINE_WIDTH;
106 normalized.reserve( orig.size() );
107 bool skippingWhitespace =
false;
109 for (
wchar_t c: orig )
121 if ( ! normalized.empty() )
122 skippingWhitespace =
true;
132 if ( skippingWhitespace )
136 skippingWhitespace =
false;
148 _wrappedText.clear();
149 _wrappedText.reserve( unwrapped.size() );
152 while ( ! unwrapped.empty() )
154 wstring line =
nextLine( unwrapped );
156 #ifdef WORD_WRAPPER_TESTER
157 wcout <<
"Line: \"" << line <<
"\" length: " << line.size() << endl;
158 wcout <<
"Rest: \"" << unwrapped <<
"\"\n" << endl;
161 if ( ! _wrappedText.empty() )
162 _wrappedText += L
'\n';
164 _wrappedText += line;
176 #ifdef WORD_WRAPPER_TESTER
177 wcout <<
"nextLine( \"" << unwrapped <<
"\" )" << endl;
180 if ( (
int) unwrapped.size() <= _lineWidth )
193 int pos = _lineWidth;
195 while ( pos > 0 && unwrapped[ pos ] != L
' ' )
198 if ( unwrapped[ pos ] == L
' ' )
200 line = unwrapped.substr( 0, pos );
201 unwrapped.erase( 0, pos + 1 );
209 pos = _lineWidth - 1;
211 while ( pos > 0 && iswalnum( unwrapped[ pos ] ) )
214 if ( ! iswalnum( unwrapped[ pos ] ) )
216 #ifdef WORD_WRAPPER_TESTER
217 wcout <<
"iswalnum wrap" << endl;
220 line = unwrapped.substr( 0, pos + 1 );
221 unwrapped.erase( 0, pos + 1 );
232 #ifdef WORD_WRAPPER_TESTER
233 wcout <<
"desperation wrap" << endl;
236 pos = _lineWidth - 1;
237 line = unwrapped.substr( 0, pos + 1 );
238 unwrapped.erase( 0, pos + 1 );
264 #ifdef WORD_WRAPPER_TESTER
267 int main(
int argc,
char *argv[] )
273 std::cerr <<
"\nUsage: " << argv[0] <<
" \"text to wrap\" <line-length>\n" << endl;
277 std::string src( argv[1] );
278 wstring input( src.begin(), src.end() );
279 int lineWidth = atoi( argv[2] );
281 wcout <<
"Wrapping to " << lineWidth <<
" columns:\n\"" << input <<
"\"\n" << endl;
287 wcout <<
" 10 20 30 40 50" << endl;
288 wcout <<
"12345678901234567890123456789012345678901234567890" << endl;
290 wcout <<
"-- Wrapped lines: " << wrapper.
lines() << endl;