File: wordwrap.cpp

package info (click to toggle)
libwibble 0.1.19
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 832 kB
  • ctags: 1,940
  • sloc: cpp: 9,798; makefile: 163; perl: 84; sh: 11
file content (46 lines) | stat: -rw-r--r-- 802 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <wibble/text/wordwrap.h>
#include <cstdlib>

using namespace std;

namespace wibble {
namespace text {

string WordWrap::get(unsigned int width)
{
	if (cursor >= s.size())
		return "";

	// Find the last work break before `width'
	unsigned int brk = cursor;
	for (unsigned int j = cursor; j < s.size() && j < cursor + width; j++)
	{
		if (s[j] == '\n')
		{
			brk = j;
			break;
		} else if (!isspace(s[j]) && (j + 1 == s.size() || isspace(s[j + 1])))
			brk = j + 1;
	}
	if (brk == cursor)
		brk = cursor + width;

	string res;
	if (brk >= s.size())
	{
		res = string(s, cursor, string::npos);
		cursor = s.size();
	} else {
		res = string(s, cursor, brk - cursor);
		cursor = brk;
		while (cursor < s.size() && isspace(s[cursor]))
			cursor++;
	}
	return res;
}

}
}


// vim:set ts=4 sw=4: