Skip to content

Commit a02558b

Browse files
committed
fix bug and styles not fixed yet
1 parent 33110ae commit a02558b

File tree

2 files changed

+67
-94
lines changed

2 files changed

+67
-94
lines changed

Lib/test/test_binascii.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,6 @@ def test_hex_separator(self):
268268
expected1 = s.hex(':').encode('ascii')
269269
self.assertEqual(binascii.b2a_hex(self.type2test(s), ':'), expected1)
270270

271-
# TODO: RUSTPYTHON
272-
@unittest.expectedFailure
273271
def test_qp(self):
274272
type2test = self.type2test
275273
a2b_qp = binascii.a2b_qp

stdlib/src/binascii.rs

Lines changed: 67 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ mod decl {
181181
}
182182
Ok((*c - 0x20) & 0x3f)
183183
}
184+
184185
#[derive(FromArgs)]
185186
struct A2bQpArgs {
186187
#[pyarg(any)]
@@ -194,7 +195,7 @@ mod decl {
194195
let header = args.header;
195196
s.with_ref(|buffer| {
196197
let len = buffer.len();
197-
let mut out_data = Vec::<u8>::with_capacity(len);
198+
let mut out_data = Vec::with_capacity(len);
198199

199200
let mut idx = 0;
200201

@@ -308,56 +309,37 @@ mod decl {
308309
if (linelen + 3) >= 76 {
309310
// MAXLINESIZE = 76
310311
linelen = 0;
311-
if crlf {
312-
delta += 3;
313-
} else {
314-
delta += 2;
315-
}
312+
delta += if crlf { 3 } else { 2 };
316313
}
317314
linelen += 3;
318315
delta += 3;
319316
inidx += 1;
317+
} else if istext
318+
&& ((buf[inidx] == 0x0a)
319+
|| ((inidx + 1 < buflen)
320+
&& (buf[inidx] == 0x0d)
321+
&& (buf[inidx + 1] == 0x0a)))
322+
{
323+
linelen = 0;
324+
if (inidx != 0) && ((buf[inidx - 1] == 0x20) || (buf[inidx - 1] == 0x09)) {
325+
delta += 2;
326+
}
327+
delta += if crlf { 2 } else { 1 };
328+
inidx += if buf[inidx] == 0x0d { 2 } else { 1 };
320329
} else {
321-
if istext
322-
&& ((buf[inidx] == 0x0a)
323-
|| ((inidx + 1 < buflen)
324-
&& (buf[inidx] == 0x0d)
325-
&& (buf[inidx + 1] == 0x0a)))
326-
{
330+
if (inidx + 1 != buflen) && (buf[inidx + 1] != 0x0a) && (linelen + 1) >= 76 {
331+
// MAXLINESIZE
327332
linelen = 0;
328-
if (inidx != 0) && ((buf[inidx - 1] == 0x20) || (buf[inidx - 1] == 0x09)) {
329-
delta += 2;
330-
}
331-
if crlf {
332-
delta += 2;
333-
} else {
334-
delta += 2;
335-
}
336-
if buf[inidx] == 0x0d {
337-
inidx += 2;
338-
} else {
339-
inidx += 1;
340-
}
341-
} else {
342-
if (inidx + 1 != buflen) && (buf[inidx + 1] != 0x0a) && (linelen + 1) >= 76
343-
{
344-
// MAXLINESIZE
345-
linelen = 0;
346-
if crlf {
347-
delta += 3;
348-
} else {
349-
delta += 2;
350-
}
351-
}
352-
linelen += 1;
353-
delta += 1;
354-
inidx += 1;
333+
delta += if crlf { 3 } else { 2 };
355334
}
335+
linelen += 1;
336+
delta += 1;
337+
inidx += 1;
356338
}
357339
odatalen += delta;
358340
}
359341

360-
let mut out_data = Vec::<u8>::with_capacity(odatalen);
342+
let mut out_data = Vec::with_capacity(odatalen);
361343
inidx = 0;
362344
outidx = 0;
363345
linelen = 0;
@@ -395,79 +377,72 @@ mod decl {
395377
outidx += 1;
396378

397379
ch = hex_nibble(buf[inidx] >> 4);
398-
if ch >= 0x61 && ch <= 0x66 {
380+
if (0x61..=0x66).contains(&ch) {
399381
ch -= 0x20;
400382
}
401383
out_data.push(ch);
402384
ch = hex_nibble(buf[inidx] & 0xf);
403-
if ch >= 0x61 && ch <= 0x66 {
385+
if (0x61..=0x66).contains(&ch) {
404386
ch -= 0x20;
405387
}
406388
out_data.push(ch);
407389

408390
outidx += 2;
409391
inidx += 1;
410392
linelen += 3;
411-
} else {
412-
if istext
413-
&& ((buf[inidx] == 0x0a)
414-
|| ((inidx + 1 < buflen)
415-
&& (buf[inidx] == 0x0d)
416-
&& (buf[inidx + 1] == 0x0a)))
393+
} else if istext
394+
&& ((buf[inidx] == 0x0a)
395+
|| ((inidx + 1 < buflen)
396+
&& (buf[inidx] == 0x0d)
397+
&& (buf[inidx + 1] == 0x0a)))
398+
{
399+
linelen = 0;
400+
if (outidx != 0)
401+
&& ((out_data[outidx - 1] == 0x20) || (out_data[outidx - 1] == 0x09))
417402
{
418-
linelen = 0;
419-
if (outidx != 0)
420-
&& ((out_data[outidx - 1] == 0x20) || (out_data[outidx - 1] == 0x09))
421-
{
422-
ch = hex_nibble(out_data[outidx - 1] >> 4);
423-
if ch >= 0x61 && ch <= 0x66 {
424-
ch -= 0x20;
425-
}
426-
out_data.push(ch);
427-
ch = hex_nibble(out_data[outidx - 1] & 0xf);
428-
if ch >= 0x61 && ch <= 0x66 {
429-
ch -= 0x20;
430-
}
431-
out_data.push(ch);
432-
out_data[outidx - 1] = 0x3d;
433-
outidx += 2;
403+
ch = hex_nibble(out_data[outidx - 1] >> 4);
404+
if (0x61..=0x66).contains(&ch) {
405+
ch -= 0x20;
434406
}
407+
out_data.push(ch);
408+
ch = hex_nibble(out_data[outidx - 1] & 0xf);
409+
if (0x61..=0x66).contains(&ch) {
410+
ch -= 0x20;
411+
}
412+
out_data.push(ch);
413+
out_data[outidx - 1] = 0x3d;
414+
outidx += 2;
415+
}
435416

417+
if crlf {
418+
out_data.push(0x0d);
419+
outidx += 1;
420+
}
421+
out_data.push(0x0a);
422+
outidx += 1;
423+
inidx += if buf[inidx] == 0x0d { 2 } else { 1 };
424+
} else {
425+
if (inidx + 1 != buflen) && (buf[inidx + 1] != 0x0a) && (linelen + 1) >= 76 {
426+
// MAXLINESIZE = 76
427+
out_data.push(0x3d);
428+
outidx += 1;
436429
if crlf {
437430
out_data.push(0x0d);
438431
outidx += 1;
439432
}
440433
out_data.push(0x0a);
441434
outidx += 1;
442-
if buf[inidx] == 0x0d {
443-
inidx += 2;
444-
} else {
445-
inidx += 1;
446-
}
435+
linelen = 0;
436+
}
437+
linelen += 1;
438+
if header && buf[inidx] == 0x20 {
439+
out_data.push(0x5f);
440+
outidx += 1;
441+
inidx += 1;
447442
} else {
448-
if (inidx + 1 != buflen) && (buf[inidx + 1] != 0x0a) && (linelen + 1) >= 76
449-
{
450-
// MAXLINESIZE = 76
451-
out_data.push(0x3d);
452-
outidx += 1;
453-
if crlf {
454-
out_data.push(0x0d);
455-
outidx += 1;
456-
}
457-
out_data.push(0x0a);
458-
outidx += 1;
459-
linelen = 0;
460-
}
461-
linelen += 1;
462-
if header && buf[inidx] == 0x20 {
463-
out_data.push(0x5f);
464-
outidx += 1;
465-
inidx += 1;
466-
} else {
467-
out_data.push(buf[inidx]);
468-
outidx += 1;
469-
inidx += 1;
470-
}
443+
out_data.push(buf[inidx]);
444+
outidx += 1;
445+
inidx += 1;
471446
}
472447
}
473448
}

0 commit comments

Comments
 (0)