@@ -154,6 +154,7 @@ pub const quotable_printable = struct {
154154 // Start at 0...75 = 76 for the RFC Max line length.
155155 const MAX_LINE_LENGTH = 75 ;
156156
157+ /// States in the encoding process.
157158 const State = enum {
158159 seen_space ,
159160 seen_r ,
@@ -164,6 +165,7 @@ pub const quotable_printable = struct {
164165 start ,
165166 };
166167
168+ /// Uses the writer as the output to print the encoded slice.
167169 pub fn encodeWriter (out : * Writer , slice : []const u8 ) Writer.Error ! void {
168170 var current_len : usize = 0 ;
169171 var index : usize = 0 ;
@@ -226,34 +228,24 @@ pub const quotable_printable = struct {
226228 index += 1 ;
227229 },
228230 },
229- .seen_r = > {
230- if (index + 1 == trimmed .len ) {
231+ .seen_r = > switch (trimmed [index + 1 ]) {
232+ '\n ' = > {
233+ state = .seen_rn ;
234+ continue ;
235+ },
236+ else = > {
237+ const available = MAX_LINE_LENGTH - current_len ;
238+ if (available > 3 ) {
239+ current_len += 2 ;
240+ } else {
241+ try out .writeAll ("=\r \n " );
242+ current_len = 2 ;
243+ }
244+
231245 try out .print ("={X:02}" , .{trimmed [index ]});
232246 state = .start ;
233247 index += 1 ;
234-
235- continue ;
236- }
237-
238- switch (trimmed [index + 1 ]) {
239- '\n ' = > {
240- state = .seen_rn ;
241- continue ;
242- },
243- else = > {
244- const available = MAX_LINE_LENGTH - current_len ;
245- if (available > 3 ) {
246- current_len += 2 ;
247- } else {
248- try out .writeAll ("=\r \n " );
249- current_len = 2 ;
250- }
251-
252- try out .print ("={X:02}" , .{trimmed [index ]});
253- state = .start ;
254- index += 1 ;
255- },
256- }
248+ },
257249 },
258250 .seen_rn = > {
259251 if (current_len != MAX_LINE_LENGTH ) {
@@ -267,37 +259,27 @@ pub const quotable_printable = struct {
267259 state = .start ;
268260 index += 2 ;
269261 },
270- .seen_space = > {
271- if (index + 1 == trimmed .len ) {
272- try out .print ("={X:02}" , .{trimmed [index ]});
273- state = .start ;
274- index += 1 ;
275-
262+ .seen_space = > switch (trimmed [index + 1 ]) {
263+ '\r ' = > {
264+ state = .seen_r_space ;
276265 continue ;
277- }
278-
279- switch (trimmed [index + 1 ]) {
280- '\r ' = > {
281- state = .seen_r_space ;
282- continue ;
283- },
284- '\n ' = > {
285- state = .seen_n_space ;
286- continue ;
287- },
288- else = > {
289- if (current_len != MAX_LINE_LENGTH ) {
290- current_len += 1 ;
291- } else {
292- try out .writeAll ("=\r \n " );
293- current_len = 0 ;
294- }
266+ },
267+ '\n ' = > {
268+ state = .seen_n_space ;
269+ continue ;
270+ },
271+ else = > {
272+ if (current_len != MAX_LINE_LENGTH ) {
273+ current_len += 1 ;
274+ } else {
275+ try out .writeAll ("=\r \n " );
276+ current_len = 0 ;
277+ }
295278
296- try out .writeByte (trimmed [index ]);
297- state = .start ;
298- index += 1 ;
299- },
300- }
279+ try out .writeByte (trimmed [index ]);
280+ state = .start ;
281+ index += 1 ;
282+ },
301283 },
302284 .seen_r_space = > {
303285 if (index + 2 == trimmed .len ) {
0 commit comments