mirror of
https://github.com/uutils/coreutils
synced 2024-07-23 19:04:18 +00:00
Fix a TODO by making conv_block_unblock_helper
consume the input. (#3787)
* Fix a TODO by making `conv_block_unblock_helper` consume the input.
This commit is contained in:
parent
0dbcdde64e
commit
08d3da8f6c
|
@ -78,34 +78,25 @@ fn unblock(buf: &[u8], cbs: usize) -> Vec<u8> {
|
|||
/// settings of `mode`, this function will update the number of
|
||||
/// records truncated; that's why `rstat` is borrowed mutably.
|
||||
pub(crate) fn conv_block_unblock_helper(
|
||||
mut buf: Vec<u8>,
|
||||
buf: Vec<u8>,
|
||||
mode: &ConversionMode,
|
||||
rstat: &mut ReadStat,
|
||||
) -> Vec<u8> {
|
||||
// TODO This function has a mutable input `buf` but also returns a
|
||||
// completely new `Vec`; that seems fishy. Could we either make
|
||||
// the input immutable or make the function not return anything?
|
||||
|
||||
fn apply_conversion(buf: &mut [u8], ct: &ConversionTable) {
|
||||
for idx in 0..buf.len() {
|
||||
buf[idx] = ct[buf[idx] as usize];
|
||||
}
|
||||
fn apply_conversion(buf: Vec<u8>, ct: &ConversionTable) -> impl Iterator<Item = u8> + '_ {
|
||||
buf.into_iter().map(|b| ct[b as usize])
|
||||
}
|
||||
|
||||
match mode {
|
||||
ConversionMode::ConvertOnly(ct) => {
|
||||
apply_conversion(&mut buf, ct);
|
||||
buf
|
||||
}
|
||||
ConversionMode::ConvertOnly(ct) => apply_conversion(buf, ct).collect(),
|
||||
ConversionMode::BlockThenConvert(ct, cbs, sync) => {
|
||||
let mut blocks = block(&buf, *cbs, *sync, rstat);
|
||||
for buf in &mut blocks {
|
||||
apply_conversion(buf, ct);
|
||||
}
|
||||
blocks.into_iter().flatten().collect()
|
||||
let blocks = block(&buf, *cbs, *sync, rstat);
|
||||
blocks
|
||||
.into_iter()
|
||||
.flat_map(|block| apply_conversion(block, ct))
|
||||
.collect()
|
||||
}
|
||||
ConversionMode::ConvertThenBlock(ct, cbs, sync) => {
|
||||
apply_conversion(&mut buf, ct);
|
||||
let buf: Vec<_> = apply_conversion(buf, ct).collect();
|
||||
block(&buf, *cbs, *sync, rstat)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
|
@ -116,12 +107,11 @@ pub(crate) fn conv_block_unblock_helper(
|
|||
.flatten()
|
||||
.collect(),
|
||||
ConversionMode::UnblockThenConvert(ct, cbs) => {
|
||||
let mut buf = unblock(&buf, *cbs);
|
||||
apply_conversion(&mut buf, ct);
|
||||
buf
|
||||
let buf = unblock(&buf, *cbs);
|
||||
apply_conversion(buf, ct).collect()
|
||||
}
|
||||
ConversionMode::ConvertThenUnblock(ct, cbs) => {
|
||||
apply_conversion(&mut buf, ct);
|
||||
let buf: Vec<_> = apply_conversion(buf, ct).collect();
|
||||
unblock(&buf, *cbs)
|
||||
}
|
||||
ConversionMode::UnblockOnly(cbs) => unblock(&buf, *cbs),
|
||||
|
|
|
@ -861,6 +861,33 @@ fn test_swab_257_test() {
|
|||
.stdout_is_fixture_bytes("seq-byte-values-odd.spec");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_block_lower() {
|
||||
new_ucmd!()
|
||||
.args(&["conv=block,lcase", "cbs=8"])
|
||||
.pipe_in_fixture("dd-block8-lowercase.test")
|
||||
.succeeds()
|
||||
.stdout_is_fixture_bytes("dd-block8-lowercase.spec");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_lower_block() {
|
||||
new_ucmd!()
|
||||
.args(&["conv=lcase,block", "cbs=8"])
|
||||
.pipe_in_fixture("dd-block8-lowercase.test")
|
||||
.succeeds()
|
||||
.stdout_is_fixture_bytes("dd-block8-lowercase.spec");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unblock_lower() {
|
||||
new_ucmd!()
|
||||
.args(&["conv=unblock,lcase", "cbs=8"])
|
||||
.pipe_in_fixture("dd-unblock8-lowercase.test")
|
||||
.succeeds()
|
||||
.stdout_is_fixture_bytes("dd-unblock8-lowercase.spec");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zeros_4k_conv_sync_obs_gt_ibs() {
|
||||
new_ucmd!()
|
||||
|
|
1
tests/fixtures/dd/dd-block8-lowercase.spec
vendored
Normal file
1
tests/fixtures/dd/dd-block8-lowercase.spec
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
abcd abcdefgh
|
2
tests/fixtures/dd/dd-block8-lowercase.test
vendored
Normal file
2
tests/fixtures/dd/dd-block8-lowercase.test
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
ABCD
|
||||
ABCDEFGHI
|
2
tests/fixtures/dd/dd-unblock8-lowercase.spec
vendored
Normal file
2
tests/fixtures/dd/dd-unblock8-lowercase.spec
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
abcd
|
||||
abcdefgh
|
1
tests/fixtures/dd/dd-unblock8-lowercase.test
vendored
Normal file
1
tests/fixtures/dd/dd-unblock8-lowercase.test
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
ABCD ABCDEFGH
|
Loading…
Reference in a new issue