forked from CyberShadow/DFeed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrap.d
147 lines (123 loc) · 3.78 KB
/
wrap.d
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* Copyright (C) 2011, 2012 Vladimir Panteleev <vladimir@thecybershadow.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/// RFC 2646. May be upgraded to RFC 3676 for international text.
module wrap;
import std.string;
import std.utf;
import ae.utils.text;
struct Paragraph
{
dstring quotePrefix, text;
}
Paragraph[] unwrapText(string text, bool flowed, bool delsp)
{
auto lines = text.toUTF32().splitLines();
Paragraph[] paragraphs;
foreach (line; lines)
{
dstring quotePrefix;
while (line.startsWith(">"d))
{
int l = 1;
// This is against standard, but many clients
// (incl. Web-News and M$ Outlook) don't give a damn:
if (line.startsWith("> "d))
l = 2;
quotePrefix ~= line[0..l];
line = line[l..$];
}
// Remove space-stuffing
if (flowed && line.startsWith(" "d))
line = line[1..$];
if (paragraphs.length>0
&& paragraphs[$-1].quotePrefix==quotePrefix
&& paragraphs[$-1].text.endsWith(" "d)
&& !line.startsWith(" "d)
&& line.length
&& line != "-- "
&& paragraphs[$-1].text != "-- "d
&& (flowed || quotePrefix.length))
{
if (delsp)
paragraphs[$-1].text = paragraphs[$-1].text[0..$-1];
paragraphs[$-1].text ~= line;
}
else
paragraphs ~= Paragraph(quotePrefix, line);
}
return paragraphs;
}
enum DEFAULT_WRAP_LENGTH = 66;
string wrapText(Paragraph[] paragraphs, int margin = DEFAULT_WRAP_LENGTH)
{
dstring[] lines;
void addLine(dstring quotePrefix, dstring line)
{
line = quotePrefix ~ line;
// Add space-stuffing
if (line.startsWith(" "d) ||
line.startsWith("From "d) ||
(line.startsWith(">"d) && quotePrefix.length==0))
{
line = " " ~ line;
}
lines ~= line;
}
foreach (paragraph; paragraphs)
{
dstring line = paragraph.text;
auto cutPoint = margin - paragraph.quotePrefix.length;
while (line.length && line[$-1] == ' ')
line = line[0..$-1];
if (!line.length)
{
addLine(paragraph.quotePrefix, null);
continue;
}
while (line.length > cutPoint)
{
auto i = line[0..cutPoint].lastIndexOf(' ');
if (i < 0)
{
i = cutPoint + line[cutPoint..$].indexOf(' ');
if (i < cutPoint)
break;
}
i++;
addLine(paragraph.quotePrefix, line[0..i]);
line = line[i..$];
}
if (line.length)
addLine(paragraph.quotePrefix, line);
}
return lines.join("\n"d).toUTF8();
}
unittest
{
// Space-stuffing
assert(wrapText(unwrapText(" Hello", false, false)) == " Hello");
// Don't rewrap user input
assert(wrapText(unwrapText("Line 1 \nLine 2 ", false, false)) == "Line 1\nLine 2");
// ...but rewrap quoted text
assert(wrapText(unwrapText("> Line 1 \n> Line 2 ", false, false)) == "> Line 1 Line 2");
// Wrap long lines
assert(wrapText(unwrapText(std.array.replicate("abcde ", 20), false, false)).split("\n").length > 1);
// Wrap by character count, not UTF-8 code-unit count. TODO: take into account surrogates and composite characters.
enum str = "Это очень очень очень очень очень очень очень длинная строка";
static assert(str.toUTF32().length < DEFAULT_WRAP_LENGTH);
static assert(str.length > DEFAULT_WRAP_LENGTH);
assert(wrapText(unwrapText(str, false, false)).split("\n").length == 1);
}