swab(1) - swap bytes #22

Closed
opened 2023-12-30 15:36:22 +00:00 by trinity · 4 comments
Owner

This pertains to #23 in that it fulfills functionality offered in POSIX by dd(1p), specifically dd conv=swab.

Usage: swab (-w [word size])

swab(1) swaps bytes in standard input to standard output. The default word size is 2, as in two-byte words with the latter half of the word (in this case, one byte) being output before the former half of the word.

$ swab
hello world
ehll oowlrd

If there are remaining bytes less than the word size they're left unconverted. Word sizes up to 16 (128 bits) will be supported.

$ echo hello world | swab -w 8
o wohellrld

The usefulness of such a tool is limited but this should be small and easy to make, and would fill in more POSIX functionality in a way that is simpler than POSIX.

This pertains to #23 in that it fulfills functionality offered in POSIX by dd(1p), specifically `dd conv=swab`. ``` Usage: swab (-w [word size]) ``` swab(1) swaps bytes in standard input to standard output. The default word size is 2, as in two-byte words with the latter half of the word (in this case, one byte) being output before the former half of the word. ```sh $ swab hello world ehll oowlrd ``` If there are remaining bytes less than the word size they're left unconverted. Word sizes up to 16 (128 bits) will be supported. ```sh $ echo hello world | swab -w 8 o wohellrld ``` The usefulness of such a tool is limited but this should be small and easy to make, and would fill in more POSIX functionality in a way that is simpler than POSIX.
trinity added the
enhancement
label 2023-12-30 16:00:34 +00:00
emma added the
help wanted
label 2023-12-30 21:39:42 +00:00
emma removed the
help wanted
label 2023-12-30 21:41:35 +00:00
emma changed title from swab(1) - swap bytes to `swab(1)` - swap bytes 2024-01-23 22:08:00 +00:00
Author
Owner

My current conundrum is that there are multiple useful types of byte reversal:

  • reversing bits in a byte
  • reversing bytes within a certain block size (e.g. "swabbing" - swapping bytes within a machine word - and the job of GNU tac(1), where all bytes in a file are reversed)
  • reversing bytes in blocks delimited by a certain byte sequence (e.g. GNU's rev(1), using '\n' or '\0')

The third should be relegated to a separate utility as it's higher level (plaintext) than endianness/bit order tomfoolery. Should the former two be separate utilities?

My current conundrum is that there are multiple useful types of byte reversal: - reversing bits in a byte - reversing bytes within a certain block size (e.g. "swabbing" - swapping bytes within a machine word - and the job of GNU tac(1), where all bytes in a file are reversed) - reversing bytes in blocks delimited by a certain byte sequence (e.g. GNU's rev(1), using '\n' or '\0') The third should be relegated to a separate utility as it's higher level (plaintext) than endianness/bit order tomfoolery. Should the former two be separate utilities?
emma added the
help wanted
label 2024-02-08 03:25:24 +00:00
Owner

Ah, I didn’t realize GNU had a utility called rev already. Will come up with another name.

Ah, I didn’t realize GNU had a utility called `rev` already. Will come up with another name.
Owner

On first consideration, I think it makes sense for all three to be separate utilities.

On first consideration, I think it makes sense for all three to be separate utilities.
trinity removed the
help wanted
label 2024-02-24 03:50:24 +00:00
trinity self-assigned this 2024-02-24 03:50:29 +00:00
Author
Owner

swab(1) won't reverse bytes in a given word size but instead print the latter half of the word before the former half; actual reversal isn't relevant to the job, which is endianness in binary files.

I've started to implement this (with a lot of help from Emma and Mars as I am not used to Rust) and have a working but severely unpolished start.

swab(1) won't reverse bytes in a given word size but instead print the latter half of the word before the former half; actual reversal isn't relevant to the job, which is endianness in binary files. I've [started to implement this](https://git.tebibyte.media/bonsai/coreutils/commit/3f0d95fe8fe87cd3b3c6ba766e1ec643d6f7faa6) (with a lot of help from Emma and Mars as I am not used to Rust) and have a working but severely unpolished start.
emma closed this issue 2024-03-19 02:54:47 +00:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: bonsai/coreutils#22
No description provided.