This is an automated email from the ASF dual-hosted git repository. jhyde pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/calcite-avatica.git
The following commit(s) were added to refs/heads/main by this push: new dcc0ff324 [CALCITE-5415] In ByteString, add 'startsWith' and 'endsWith' methods dcc0ff324 is described below commit dcc0ff3243e01150aff3f14e157d02e2669ed691 Author: Julian Hyde <jh...@apache.org> AuthorDate: Thu Dec 1 17:18:05 2022 -0800 [CALCITE-5415] In ByteString, add 'startsWith' and 'endsWith' methods --- .../apache/calcite/avatica/util/ByteString.java | 29 +++++++++++++++ .../calcite/avatica/test/AvaticaUtilsTest.java | 41 ++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/core/src/main/java/org/apache/calcite/avatica/util/ByteString.java b/core/src/main/java/org/apache/calcite/avatica/util/ByteString.java index 86b934a31..dd968a63f 100644 --- a/core/src/main/java/org/apache/calcite/avatica/util/ByteString.java +++ b/core/src/main/java/org/apache/calcite/avatica/util/ByteString.java @@ -347,6 +347,35 @@ public class ByteString implements Comparable<ByteString>, Serializable { } return -1; } + + /** Returns whether the substring of this ByteString beginning at the + * specified index starts with the specified prefix. + * + * @param prefix The prefix + * @param offset Where to begin looking in this string + */ + public boolean startsWith(ByteString prefix, int offset) { + // Note: offset might be near -1>>>1. + if (offset < 0 || offset > bytes.length - prefix.bytes.length) { + return false; + } + for (int i = offset, j = 0; j < prefix.bytes.length;) { + if (bytes[i++] != prefix.bytes[j++]) { + return false; + } + } + return true; + } + + /** Returns whether this ByteString starts with the specified prefix. */ + public boolean startsWith(ByteString prefix) { + return startsWith(prefix, 0); + } + + /** Returns whether this ByteString ends with the specified suffix. */ + public boolean endsWith(ByteString suffix) { + return startsWith(suffix, length() - suffix.length()); + } } // End ByteString.java diff --git a/core/src/test/java/org/apache/calcite/avatica/test/AvaticaUtilsTest.java b/core/src/test/java/org/apache/calcite/avatica/test/AvaticaUtilsTest.java index d57912c29..fb026e3a7 100644 --- a/core/src/test/java/org/apache/calcite/avatica/test/AvaticaUtilsTest.java +++ b/core/src/test/java/org/apache/calcite/avatica/test/AvaticaUtilsTest.java @@ -411,6 +411,47 @@ public class AvaticaUtilsTest { copyBytes[3] = 11; assertThat(s.getBytes()[3], is((byte) 92)); assertThat(s, is(s2)); + + // startsWith + assertThat(s.startsWith(s), is(true)); + assertThat(s.startsWith(s.substring(0, 1)), is(true)); + assertThat(s.startsWith(s.substring(0, 3)), is(true)); + assertThat(s.startsWith(s3), is(true)); // "" + assertThat(s.startsWith(s4), is(false)); // "\0" + assertThat(s3.startsWith(s3), is(true)); // "" starts with "" + + // startsWith offset 0 + assertThat(s.startsWith(s, 0), is(true)); + assertThat(s.startsWith(s.substring(0, 1), 0), is(true)); + assertThat(s.startsWith(s.substring(0, 3), 0), is(true)); + assertThat(s.startsWith(s3, 0), is(true)); // "" + assertThat(s.startsWith(s4, 0), is(false)); // "\0" + assertThat(s3.startsWith(s3, 0), is(true)); // "" starts with "" + + // startsWith, other offsets + assertThat(s.startsWith(s, 1), is(false)); + assertThat(s.startsWith(s3, 1), is(true)); + assertThat(s.startsWith(s3, s.length() - 1), is(true)); + assertThat(s.startsWith(s3, s.length()), is(true)); + assertThat(s.startsWith(s3, s.length() + 1), is(false)); + + // endsWith + assertThat(reverse(s).endsWith(reverse(s)), is(true)); + assertThat(reverse(s).endsWith(reverse(s.substring(0, 1))), is(true)); + assertThat(reverse(s).endsWith(reverse(s.substring(0, 3))), is(true)); + assertThat(reverse(s).endsWith(reverse(s3)), is(true)); // "" + assertThat(reverse(s).endsWith(reverse(s4)), is(false)); // "\0" + assertThat(reverse(s3).endsWith(reverse(s3)), is(true)); + } + + private ByteString reverse(ByteString s) { + final byte[] bytes = s.getBytes(); + for (int i = 0, j = bytes.length - 1; i < j; i++, j--) { + byte b = bytes[i]; + bytes[i] = bytes[j]; + bytes[j] = b; + } + return new ByteString(bytes); } @Test public void testSkipFully() throws IOException {