Implement "get" function for strings

This commit is contained in:
Konstantin Nazarov 2024-09-29 22:56:46 +01:00
parent c097273aec
commit 9e8f857836
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
2 changed files with 14 additions and 1 deletions

View file

@ -217,6 +217,17 @@ Result<Value> String::copy_value() const {
Result<String> String::copy() const { return String(TRY(_value.copy())); }
Result<Value> String::get(const Value& key) const {
if (!key.is<Int64>()) {
return ERROR(TypeMismatch);
}
uint64_t idx = (uint64_t)key.to<Int64>()->value();
auto c = TRY(operator[](idx));
auto res = Value(TRY(String::create(&c, 1)));
return res;
}
Result<Value> Symbol::copy_value() const {
return Value(Symbol(TRY(_value.copy())));
}

View file

@ -540,6 +540,8 @@ class String : public Object {
return _value->data[idx];
}
Result<Value> get(const Value& key) const;
Result<String> concat(const char* rhs, uint64_t rhs_size) {
uint64_t lhs_size = TRY(size());
uint64_t res_size = lhs_size + rhs_size;
@ -1408,7 +1410,7 @@ class Value {
return ((Object*)buf)->div(*(Object*)rhs.buf);
}
Result<Value> get(Value& key) const { return ((Object*)buf)->get(key); }
Result<Value> get(const Value& key) const { return ((Object*)buf)->get(key); }
Result<Value> get(int64_t key) const {
Value k = TRY(Int64::create(key));
return ((Object*)buf)->get(k);