diff --git a/src/common.cpp b/src/common.cpp index f0ba233..bded8c3 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -217,6 +217,17 @@ Result String::copy_value() const { Result String::copy() const { return String(TRY(_value.copy())); } +Result String::get(const Value& key) const { + if (!key.is()) { + return ERROR(TypeMismatch); + } + uint64_t idx = (uint64_t)key.to()->value(); + auto c = TRY(operator[](idx)); + + auto res = Value(TRY(String::create(&c, 1))); + return res; +} + Result Symbol::copy_value() const { return Value(Symbol(TRY(_value.copy()))); } diff --git a/src/common.hpp b/src/common.hpp index e17180d..a99445b 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -540,6 +540,8 @@ class String : public Object { return _value->data[idx]; } + Result get(const Value& key) const; + Result 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 get(Value& key) const { return ((Object*)buf)->get(key); } + Result get(const Value& key) const { return ((Object*)buf)->get(key); } Result get(int64_t key) const { Value k = TRY(Int64::create(key)); return ((Object*)buf)->get(k);