Skip to content

Commit

Permalink
Merge pull request #3910 from maxonfjvipon/bug/#3265/bytes-of-shift-s…
Browse files Browse the repository at this point in the history
…imple

bug(#3265): divided shift method in `BytesRaw`
  • Loading branch information
yegor256 authored Feb 12, 2025
2 parents f3690f3 + fbc1ad0 commit ed3b721
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 50 deletions.
32 changes: 16 additions & 16 deletions eo-runtime/src/main/java/org/eolang/BytesOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,6 @@ public final class BytesOf implements Bytes {
*/
private final Bytes bytes;

/**
* Ctor.
* @param bytes Bytes.
*/
public BytesOf(final Bytes bytes) {
this.bytes = bytes;
}

/**
* Ctor.
* @param data Data.
*/
public BytesOf(final byte[] data) {
this(new BytesRaw(Arrays.copyOf(data, data.length)));
}

/**
* Ctor.
* @param str UTF-8 Text.
Expand Down Expand Up @@ -96,6 +80,22 @@ public BytesOf(final double number) {
this(ByteBuffer.allocate(Double.BYTES).putDouble(number).array());
}

/**
* Ctor.
* @param data Data.
*/
public BytesOf(final byte[] data) {
this(new BytesRaw(Arrays.copyOf(data, data.length)));
}

/**
* Ctor.
* @param bytes Bytes.
*/
public BytesOf(final Bytes bytes) {
this.bytes = bytes;
}

@Override
public Bytes not() {
return this.bytes.not();
Expand Down
86 changes: 52 additions & 34 deletions eo-runtime/src/main/java/org/eolang/BytesRaw.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
* Bytes to be created from byte array only.
*
* @since 0.1.0
* @todo #3239:90min Method {@link BytesOf#shift} should be refactored to get rid of
* {@code @SuppressWarnings("PMD.CognitiveComplexity")} warning. You can
* check description of this rules here
* <a href="https://pmd.github.io/pmd/pmd_rules_java_design">pmd.github.io</a>
*/
@SuppressWarnings({"PMD.TooManyMethods", "PMD.GodClass"})
final class BytesRaw implements Bytes {
Expand Down Expand Up @@ -92,43 +88,17 @@ public Bytes xor(final Bytes other) {
}

@Override
@SuppressWarnings("PMD.CognitiveComplexity")
public Bytes shift(final int bits) {
// @checkstyle MethodBodyCommentsCheck (3 lines)
// @checkstyle NestedIfDepthCheck (40 lines)
final byte[] bytes = this.take();
final int mod = Math.abs(bits) % Byte.SIZE;
final int offset = Math.abs(bits) / Byte.SIZE;
final Bytes shifted;
if (bits < 0) {
final byte carry = (byte) ((0x01 << mod) - 1);
for (int index = 0; index < bytes.length; index += 1) {
final int source = index + offset;
if (source >= bytes.length) {
bytes[index] = 0;
} else {
byte dst = (byte) (bytes[source] << mod);
if (source + 1 < bytes.length) {
dst |= (byte) (bytes[source + 1] >>> (Byte.SIZE - mod) & (carry & 0xFF));
}
bytes[index] = dst;
}
}
shifted = BytesRaw.shiftLeft(bytes, mod, offset);
} else {
final byte carry = (byte) (0xFF << (Byte.SIZE - mod));
for (int index = bytes.length - 1; index >= 0; index -= 1) {
final int source = index - offset;
if (source < 0) {
bytes[index] = 0;
} else {
byte dst = (byte) ((0xff & bytes[source]) >>> mod);
if (source - 1 >= 0) {
dst |= (byte) (bytes[source - 1] << (Byte.SIZE - mod) & (carry & 0xFF));
}
bytes[index] = dst;
}
}
shifted = BytesRaw.shiftRight(bytes, mod, offset);
}
return new BytesOf(bytes);
return shifted;
}

@Override
Expand Down Expand Up @@ -225,6 +195,54 @@ public int hashCode() {
return Arrays.hashCode(this.data);
}

/**
* Shift bytes to the left.
* @param bytes Bytes
* @param mod Mod
* @param offset Offset
* @return Shifted bytes
*/
private static Bytes shiftLeft(final byte[] bytes, final int mod, final int offset) {
final byte carry = (byte) ((0x01 << mod) - 1);
for (int index = 0; index < bytes.length; index += 1) {
final int source = index + offset;
if (source >= bytes.length) {
bytes[index] = 0;
} else {
byte dst = (byte) (bytes[source] << mod);
if (source + 1 < bytes.length) {
dst |= (byte) (bytes[source + 1] >>> (Byte.SIZE - mod) & (carry & 0xFF));
}
bytes[index] = dst;
}
}
return new BytesOf(bytes);
}

/**
* Shift bytes to the right.
* @param bytes Bytes
* @param mod Mod
* @param offset Offset
* @return Shifted bytes
*/
private static Bytes shiftRight(final byte[] bytes, final int mod, final int offset) {
final byte carry = (byte) (0xFF << (Byte.SIZE - mod));
for (int index = bytes.length - 1; index >= 0; index -= 1) {
final int source = index - offset;
if (source < 0) {
bytes[index] = 0;
} else {
byte dst = (byte) ((0xff & bytes[source]) >>> mod);
if (source - 1 >= 0) {
dst |= (byte) (bytes[source - 1] << (Byte.SIZE - mod) & (carry & 0xFF));
}
bytes[index] = dst;
}
}
return new BytesOf(bytes);
}

/**
* Count leading zero bits.
* @param num Byte.
Expand Down

1 comment on commit ed3b721

@0pdd
Copy link

@0pdd 0pdd commented on ed3b721 Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 3239-50a22f3c disappeared from eo-runtime/src/main/java/org/eolang/BytesRaw.java), that's why I closed #3265. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.