Skip to content
Snippets Groups Projects
Unverified Commit 7b7165ef authored by Yorick Peterse's avatar Yorick Peterse
Browse files

Add String.byte

This method can be used to obtain a single byte at a byte position. This
in turn is useful when performing byte comparisons of strings, as it
removes the need for creating byte arrays.
parent d42a6819
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -184,6 +184,7 @@ module Inkoc
RandomNumber
RandomRange
RandomBytes
StringByte
]
.each_with_index
.each_with_object({}) { |(value, index), hash| hash[value] = index }
Loading
Loading
Loading
Loading
@@ -1374,6 +1374,10 @@ module Inkoc
typedb.string_type.new_instance
end
 
def on_raw_string_byte(*)
typedb.integer_type.new_instance
end
def on_raw_stdin_read(*)
typedb.integer_type.new_instance
end
Loading
Loading
Loading
Loading
@@ -1088,6 +1088,10 @@ module Inkoc
raw_ternary_instruction(:StringSlice, node, body)
end
 
def on_raw_string_byte(node, body)
raw_binary_instruction(:StringByte, node, body)
end
def on_raw_stdin_read(node, body)
raw_binary_instruction(:StdinRead, node, body)
end
Loading
Loading
Loading
Loading
@@ -58,6 +58,22 @@ impl String {
_INKOC.string_size(self)
}
 
# Returns the byte at the given byte index.
#
# # Examples
#
# Obtaining a single byte from a `String`:
#
# 'inko'.byte(0) # => 105
#
# You can also use a negative index to access a byte from the back of the
# `String`:
#
# 'inko'.byte(-1) # => 111
def byte(index: Integer) -> Integer {
_INKOC.string_byte(self, index)
}
# Slices `self` into a substring.
#
# Slicing a string allows one to extract a substring by providing a start
Loading
Loading
Loading
Loading
@@ -128,3 +128,17 @@ test.group('std::string::String.hash') do (g) {
assert.equal(hasher1.to_hash, hasher2.to_hash)
}
}
test.group('std::string::String.byte') do (g) {
g.test('Obtaining the first byte') {
assert.equal('inko'.byte(0), 105)
}
g.test('Obtaining the byte of a positive index') {
assert.equal('inko'.byte(1), 110)
}
g.test('Obtaining the byte of a negative index') {
assert.equal('inko'.byte(-1), 111)
}
}
Loading
Loading
@@ -179,9 +179,7 @@ impl ObjectValue {
pub fn as_byte_array_mut(&mut self) -> Result<&mut Vec<u8>, String> {
match *self {
ObjectValue::ByteArray(ref mut val) => Ok(val),
_ => {
Err("as_byte_array_mut called on a non byte array".to_string())
}
_ => Err("as_byte_array_mut called on a non byte array".to_string()),
}
}
 
Loading
Loading
Loading
Loading
@@ -182,6 +182,7 @@ pub enum InstructionType {
RandomNumber,
RandomRange,
RandomBytes,
StringByte,
}
 
/// Struct for storing information about a single instruction.
Loading
Loading
Loading
Loading
@@ -1838,6 +1838,14 @@ impl Machine {
 
context.set_register(reg, value);
}
InstructionType::StringByte => {
let reg = instruction.arg(0);
let string = context.get_register(instruction.arg(1));
let index = context.get_register(instruction.arg(2));
let res = string::byte(string, index)?;
context.set_register(reg, res);
}
InstructionType::FloatToBits => {
let reg = instruction.arg(0);
let val = context.get_register(instruction.arg(1));
Loading
Loading
Loading
Loading
@@ -204,3 +204,16 @@ pub fn to_float(
)))
}
}
pub fn byte(
str_ptr: ObjectPointer,
index_ptr: ObjectPointer,
) -> Result<ObjectPointer, String> {
let string = str_ptr.string_value()?;
let index =
slicing::index_for_slice(string.len(), index_ptr.integer_value()?);
let byte = i64::from(string.as_bytes()[index]);
Ok(ObjectPointer::integer(byte))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment