Skip to content

Commit

Permalink
Use Object::to_int() helper method for conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
seven1m committed Mar 1, 2022
1 parent 80ec8a1 commit c221fbc
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions src/string_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,33 +101,19 @@ String create_padding(String padding, size_t length) {
}

Value StringObject::center(Env *env, Value length, Value padstr) {
nat_int_t length_i;
auto to_int = "to_int"_s;

if (length->is_integer()) {
length_i = length->as_integer()->to_nat_int_t();
} else if (length->respond_to(env, to_int)) {
auto result = length->send(env, to_int);

if (!result->is_integer())
env->raise("TypeError", "length can't be converted to an integer");

length_i = result->as_integer()->to_nat_int_t();
} else {
env->raise("TypeError", "length can't be converted to an integer.");
}
nat_int_t length_i = length->to_int(env)->to_nat_int_t();

auto to_str = "to_str"_s;
String pad;

if (! padstr) {
if (!padstr) {
pad = new String { " " };
} else if (padstr->is_string()) {
pad = padstr->as_string()->string();
} else if (padstr->respond_to(env, to_str)) {
auto result = padstr->send(env, to_str);

if (! result->is_string())
if (!result->is_string())
env->raise("TypeError", "padstr can't be converted to a string");

pad = result->as_string()->string();
Expand Down

0 comments on commit c221fbc

Please sign in to comment.