public static Slice utf8Slice(String string) { return copiedBuffer(string, UTF_8); }
public static Slice utf8Slice(String string) { return copiedBuffer(string, UTF_8); }
@Description("Converts integer number to hex value") @ScalarFunction("hex") @SqlType(StandardTypes.VARCHAR) public static Slice integerToHex(@SqlType(StandardTypes.BIGINT) long num) { String hex = Long.toHexString(num); return Slices.copiedBuffer(hex, UTF_8); }
@SqlNullable @Description("Converts binary to hex value") @ScalarFunction("hex") @SqlType(StandardTypes.VARCHAR) public static Slice BinaryrToHex(@SqlType(StandardTypes.VARBINARY) Slice inputNum) { try { Long num = Long.parseLong(inputNum.toStringUtf8(), 2); String hex = Long.toHexString(num); return Slices.copiedBuffer(hex, UTF_8); } catch (NumberFormatException e) { // Return NULL if invalid input return null; } }
@SqlNullable @Description("Converts string number to hex value") @ScalarFunction("hex") @SqlType(StandardTypes.VARCHAR) public static Slice stringIntegerToHex(@SqlType(StandardTypes.VARCHAR) Slice inputNum) { try { Long num = Long.parseLong(inputNum.toStringUtf8(), 10); String hex = Long.toHexString(num); return Slices.copiedBuffer(hex, UTF_8); } catch (NumberFormatException e) { // Return NULL if invalid input return null; } }
@Test public void testUtf8Conversion() { String s = "apple \u2603 snowman"; Slice slice = Slices.copiedBuffer(s, UTF_8); assertEquals(utf8Slice(s), slice); assertEquals(slice.toStringUtf8(), s); assertEquals(utf8Slice(s).toStringUtf8(), s); }
@Test public void testToString() { assertEquals(Slices.copiedBuffer("apple", UTF_8).toString(UTF_8), "apple"); for (int size = 0; size < 100; size++) { for (int index = 0; index < size; index++) { assertToStrings(allocate(size), index); } } }
@Test public void testUtf8Conversion() { String s = "apple \u2603 snowman"; Slice slice = Slices.copiedBuffer(s, UTF_8); assertEquals(utf8Slice(s), slice); assertEquals(slice.toStringUtf8(), s); assertEquals(utf8Slice(s).toStringUtf8(), s); }
@Description("Converts decimal number to binary") @ScalarFunction("bin") @SqlType(StandardTypes.VARCHAR) public static Slice integerToBin(@SqlType(StandardTypes.BIGINT) long num) { String bin = Long.toBinaryString(num); return Slices.copiedBuffer(bin, UTF_8); }
@Test public void testToString() { assertEquals(Slices.copiedBuffer("apple", UTF_8).toString(UTF_8), "apple"); for (int size = 0; size < 100; size++) { for (int index = 0; index < size; index++) { assertToStrings(allocate(size), index); } } }
@SqlNullable @Description("Converts Hexadecimal number to binary value") @ScalarFunction("unhex") @SqlType(StandardTypes.VARBINARY) public static Slice hexToInteger(@SqlType(StandardTypes.VARCHAR) Slice inputHex) { try { Long num = Long.parseLong(inputHex.toStringUtf8(), 16); return Slices.copiedBuffer(Long.toBinaryString(num), UTF_8); } catch (NumberFormatException e) { // Return NULL if invalid input return null; } } }
@SuppressWarnings("CharUsedInArithmeticContext") private static void assertToStrings(Slice slice, int index) { // fill slice with FF slice.fill((byte) 0xFF); // set and get the value char[] chars = new char[(slice.length() - index) / 2]; for (int i = 0; i < chars.length; i++) { chars[i] = (char) ('a' + (i % 26)); } String string = new String(chars); Slice value = Slices.copiedBuffer(string, UTF_8); slice.setBytes(index, value); assertEquals(slice.toString(index, value.length(), UTF_8), string); for (int length = 0; length < value.length(); length++) { slice.fill((byte) 0xFF); slice.setBytes(index, value, 0, length); assertEquals(slice.toString(index, length, UTF_8), string.substring(0, length)); } }
@SuppressWarnings("CharUsedInArithmeticContext") private static void assertToStrings(Slice slice, int index) { // fill slice with FF slice.fill((byte) 0xFF); // set and get the value char[] chars = new char[(slice.length() - index) / 2]; for (int i = 0; i < chars.length; i++) { chars[i] = (char) ('a' + (i % 26)); } String string = new String(chars); Slice value = Slices.copiedBuffer(string, UTF_8); slice.setBytes(index, value); assertEquals(slice.toString(index, value.length(), UTF_8), string); for (int length = 0; length < value.length(); length++) { slice.fill((byte) 0xFF); slice.setBytes(index, value, 0, length); assertEquals(slice.toString(index, length, UTF_8), string.substring(0, length)); } }