Swapping nibbles range style

Volcz volcz at kth.se
Tue Dec 10 22:19:41 PST 2013


On Tuesday, 10 December 2013 at 22:20:54 UTC, MrSmith wrote:
> On Tuesday, 10 December 2013 at 22:08:17 UTC, Volcz wrote:
>
> Can you explain a bit more. It's unclear what is the exact 
> problem.
> Is it intended to work with unicode strings of any length? 
> Simply swap pairs of chars? What if string length is odd?

 From my experience this is a common exercise in the telecom 
business.
My data is luckily always stored in a hex format, so there is no 
need to consider unicode(?).
I've never seen data with a missing nibble. I usually checks that 
the data is a even number chars. In real world cases F usually 
indicates unused nibble.
These things are all over the 3gpp specifications.

On Tuesday, 10 December 2013 at 23:26:01 UTC, Justin Whear wrote:
> On Tue, 10 Dec 2013 23:08:16 +0100, Volcz wrote:
>
> How's this?
> http://dpaste.dzfl.pl/135285f4

On Tuesday, 10 December 2013 at 23:39:04 UTC, bearophile wrote:
> A nibble is 4 bits. Working with strings like this is not so 
> efficient.
> string swapAdjacent(in string s) pure
> in {
>     assert(s.length % 2 == 0);
>     assert(s.all!(c => c < 128));
> } out(result) {
>     assert(result.length == s.length);
> } body {
>     return s
>            .chunks(2)
>            .map!(c => [cast(char)c.dropOne.front, 
> cast(char)c.front])
>            .join;
> Bye,
> bearophile

Are there any obvious difference between the three solutions I 
have received? They all to work the same to me.
I like very much the contract programming, it's a area still 
unexplored to me.
Readability wise I think
chunk => chunk.cycle.dropOne.takeExactly(2)
is more readable than
c => [cast(char)c.dropOne.front, cast(char)c.front]

My Dlang-fu has much improved thanks to the masters help,
Volcz


More information about the Digitalmars-d-learn mailing list