Binary strings: Difference between revisions

m (→‎{{header|Raku}}: Fix Perl6 -> Raku in comments)
Line 1,282:
bary
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
<lang groovy>import java.nio.charset.StandardCharsets
 
class MutableByteString {
private byte[] bytes
private int length
 
MutableByteString(byte... bytes) {
setInternal(bytes)
}
 
int length() {
return length
}
 
boolean isEmpty() {
return length == 0
}
 
byte get(int index) {
return bytes[check(index)]
}
 
void set(byte[] bytes) {
setInternal(bytes)
}
 
void set(int index, byte b) {
bytes[check(index)] = b
}
 
void append(byte b) {
if (length >= bytes.length) {
int len = 2 * bytes.length
if (len < 0) {
len = Integer.MAX_VALUE
}
bytes = Arrays.copyOf(bytes, len)
}
bytes[length] = b
length++
}
 
MutableByteString substring(int from, int to) {
return new MutableByteString(Arrays.copyOfRange(bytes, from, to))
}
 
void replace(byte[] from, byte[] to) {
ByteArrayOutputStream copy = new ByteArrayOutputStream()
if (from.length == 0) {
for (byte b : bytes) {
copy.write(to, 0, to.length)
copy.write(b)
}
copy.write(to, 0, to.length)
} else {
for (int i = 0; i < length; i++) {
if (regionEqualsImpl(i, from)) {
copy.write(to, 0, to.length)
i += from.length - 1
} else {
copy.write(bytes[i])
}
}
}
set(copy.toByteArray())
}
 
boolean regionEquals(int offset, MutableByteString other, int otherOffset, int len) {
if (Math.max(offset, otherOffset) + len < 0) {
return false
}
if (offset + len > length || otherOffset + len > other.length()) {
return false
}
for (int i = 0; i < len; i++) {
if (bytes[offset + i] != other.get(otherOffset + i)) {
return false
}
}
return true
}
 
String toHexString() {
char[] hex = new char[2 * length]
for (int i = 0; i < length; i++) {
hex[2 * i] = "0123456789abcdef".charAt(bytes[i] >> 4 & 0x0F)
hex[2 * i + 1] = "0123456789abcdef".charAt(bytes[i] & 0x0F)
}
return new String(hex)
}
 
String toStringUtf8() {
return new String(bytes, 0, length, StandardCharsets.UTF_8)
}
 
private void setInternal(byte[] bytes) {
this.bytes = bytes.clone()
this.length = bytes.length
}
 
private boolean regionEqualsImpl(int offset, byte[] other) {
int len = other.length
if (offset < 0 || offset + len < 0)
return false
if (offset + len > length)
return false
for (int i = 0; i < len; i++) {
if (bytes[offset + i] != other[i])
return false
}
return true
}
 
private int check(int index) {
if (index < 0 || index >= length)
throw new IndexOutOfBoundsException(String.valueOf(index))
return index
}
}</lang>
 
Test Code
<lang groovy>import org.testng.Assert
import org.testng.annotations.Test
 
import java.nio.charset.StandardCharsets
 
class MutableByteStringTest {
@Test
void replaceEmpty() {
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8))
str.replace([] as byte[], ['-' as char] as byte[])
 
Assert.assertEquals(str.toStringUtf8(), "-h-e-l-l-o-")
}
 
@Test
void replaceMultiple() {
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8))
str.replace(['l' as char] as byte[], ['1' as char, '2' as char, '3' as char] as byte[])
 
Assert.assertEquals(str.toStringUtf8(), "he123123o")
}
 
@Test
void toHexString() {
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8))
 
Assert.assertEquals(str.toHexString(), "68656c6c6f")
}
 
@Test
void append() {
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8))
str.append((',' as char) as byte)
str.append((' ' as char) as byte)
str.append(('w' as char) as byte)
str.append(('o' as char) as byte)
str.append(('r' as char) as byte)
str.append(('l' as char) as byte)
str.append(('d' as char) as byte)
 
Assert.assertEquals(str.toStringUtf8(), "hello, world")
}
 
@Test
void substring() {
MutableByteString str = new MutableByteString("hello, world".getBytes(StandardCharsets.UTF_8))
 
Assert.assertEquals(str.substring(0, 5).toStringUtf8(), "hello")
Assert.assertEquals(str.substring(7, 12).toStringUtf8(), "world")
}
 
@Test
void regionEquals(){
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8))
 
Assert.assertTrue(str.regionEquals(0, new MutableByteString(['h' as char] as byte[]), 0, 1))
Assert.assertFalse(str.regionEquals(0, new MutableByteString(['h' as char] as byte[]), 0, 2))
}
}</lang>
 
=={{header|Haskell}}==
1,452

edits