Implement "get" function for strings
This commit is contained in:
parent
c097273aec
commit
9e8f857836
2 changed files with 14 additions and 1 deletions
|
@ -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())));
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue