You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2020/02/11 01:46:26 UTC
[incubator-tuweni] branch master updated: Fix TUWENI-31 by creating
a new method to allow 0x0 values for quantities
This is an automated email from the ASF dual-hosted git repository.
toulmean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git
The following commit(s) were added to refs/heads/master by this push:
new 59d76bd Fix TUWENI-31 by creating a new method to allow 0x0 values for quantities
59d76bd is described below
commit 59d76bd5cad7ada020e77043534cdcbc78b3de16
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Mon Feb 10 17:46:13 2020 -0800
Fix TUWENI-31 by creating a new method to allow 0x0 values for quantities
---
.../main/java/org/apache/tuweni/bytes/Bytes.java | 23 ++++++++++++++++++++++
.../org/apache/tuweni/bytes/CommonBytesTests.java | 7 +++++++
2 files changed, 30 insertions(+)
diff --git a/bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java b/bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java
index ea7c347..dfec53b 100644
--- a/bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java
+++ b/bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java
@@ -1470,6 +1470,29 @@ public interface Bytes extends Comparable<Bytes> {
}
/**
+ * @return This value represented as a minimal hexadecimal string (without any leading zero, except if it's valued
+ * zero or empty, in which case it returns 0x0).
+ */
+ default String toQuantityHexString() {
+ if (Bytes.EMPTY.equals(this)) {
+ return "0x0";
+ }
+ StringBuilder hex;
+ try {
+ hex = appendHexTo(new StringBuilder());
+ } catch (IOException e) {
+ // not thrown
+ throw new RuntimeException(e);
+ }
+
+ int i = 0;
+ while (i < hex.length() - 1 && hex.charAt(i) == '0') {
+ i++;
+ }
+ return "0x" + hex.substring(hex.charAt(hex.length() - 1) == '0' ? i : i++);
+ }
+
+ /**
* @return This value represented as base 64.
*/
default String toBase64String() {
diff --git a/bytes/src/test/java/org/apache/tuweni/bytes/CommonBytesTests.java b/bytes/src/test/java/org/apache/tuweni/bytes/CommonBytesTests.java
index c7aee0e..0d7efb2 100644
--- a/bytes/src/test/java/org/apache/tuweni/bytes/CommonBytesTests.java
+++ b/bytes/src/test/java/org/apache/tuweni/bytes/CommonBytesTests.java
@@ -434,6 +434,13 @@ abstract class CommonBytesTests {
}
@Test
+ void testQuantityHexString() {
+ assertEquals("0x0", h("0x").toQuantityHexString());
+ assertEquals("0x0", h("0x0000").toQuantityHexString());
+ assertEquals("0x1000001", h("0x01000001").toQuantityHexString());
+ }
+
+ @Test
void testHexString() {
assertEquals("0x", h("0x").toShortHexString());
assertEquals("0x", h("0x0000").toShortHexString());
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org