Commit a9521050 a9521050ce3b8dcbf7421ac09fe553326357fa5a by Sergey Poznyakoff

(mu_string_unfold): Convert \n followed by any amount of whitespace to single space.

1 parent 13ad8cc3
...@@ -1172,13 +1172,44 @@ int ...@@ -1172,13 +1172,44 @@ int
1172 mu_string_unfold (char *text, size_t *plen) 1172 mu_string_unfold (char *text, size_t *plen)
1173 { 1173 {
1174 char *p, *q; 1174 char *p, *q;
1175 1175 enum uf_state { uf_init, uf_nl, uf_fold } state = uf_init;
1176 #define ISSPACE(c) (c == '\r' || c == ' ' || c == '\t')
1177
1176 if (!text) 1178 if (!text)
1177 return EINVAL; 1179 return EINVAL;
1178 1180
1179 for (p = q = text; *q; q++) 1181 for (p = q = text; *q; q++)
1180 if (*q != '\n') 1182 {
1181 *p++ = *q; 1183 switch (state)
1184 {
1185 case uf_init:
1186 if (*q == '\n')
1187 state = uf_nl;
1188 else
1189 *p++ = *q;
1190 break;
1191
1192 case uf_nl:
1193 if (ISSPACE (*q))
1194 state = uf_fold;
1195 else
1196 {
1197 state = uf_init;
1198 *p++ = *q;
1199 }
1200 break;
1201
1202 case uf_fold:
1203 if (!ISSPACE (*q))
1204 {
1205 *p++ = ' ';
1206 *p++ = *q;
1207 state = uf_init;
1208 }
1209 break;
1210 }
1211 }
1212
1182 *p++ = 0; 1213 *p++ = 0;
1183 if (plen) 1214 if (plen)
1184 *plen = p - text; 1215 *plen = p - text;
......