Simplify some container __repr__ implementations with %R
This commit is contained in:
parent
3e8bb098ff
commit
9d5c9200e8
@ -325,15 +325,10 @@ KRK_Method(bytearray,__repr__) {
|
||||
METHOD_TAKES_NONE();
|
||||
struct StringBuilder sb = {0};
|
||||
pushStringBuilderStr(&sb, "bytearray(", 10);
|
||||
|
||||
krk_push(self->actual);
|
||||
KrkValue repred_bytes = krk_callDirect(vm.baseClasses->bytesClass->_reprer, 1);
|
||||
if (!IS_STRING(repred_bytes)) {
|
||||
/* Invalid repr of bytes? */
|
||||
discardStringBuilder(&sb);
|
||||
if (!krk_pushStringBuilderFormat(&sb, "%R", self->actual)) {
|
||||
krk_discardStringBuilder(&sb);
|
||||
return NONE_VAL();
|
||||
}
|
||||
pushStringBuilderStr(&sb, AS_STRING(repred_bytes)->chars, AS_STRING(repred_bytes)->length);
|
||||
pushStringBuilder(&sb,')');
|
||||
return finishStringBuilder(&sb);
|
||||
}
|
||||
|
100
src/obj_dict.c
100
src/obj_dict.c
@ -188,35 +188,21 @@ KRK_Method(dict,__repr__) {
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
KrkTableEntry * entry = &self->entries.entries[i];
|
||||
if (IS_KWARGS(entry->key)) continue;
|
||||
if (c > 0) {
|
||||
pushStringBuilderStr(&sb, ", ", 2);
|
||||
}
|
||||
if (c) pushStringBuilderStr(&sb, ", ", 2);
|
||||
c++;
|
||||
|
||||
{
|
||||
KrkClass * type = krk_getType(entry->key);
|
||||
krk_push(entry->key);
|
||||
KrkValue result = krk_callDirect(type->_reprer, 1);
|
||||
if (IS_STRING(result)) {
|
||||
pushStringBuilderStr(&sb, AS_CSTRING(result), AS_STRING(result)->length);
|
||||
}
|
||||
}
|
||||
|
||||
if (!krk_pushStringBuilderFormat(&sb, "%R", entry->key)) goto _error;
|
||||
pushStringBuilderStr(&sb, ": ", 2);
|
||||
|
||||
{
|
||||
KrkClass * type = krk_getType(entry->value);
|
||||
krk_push(entry->value);
|
||||
KrkValue result = krk_callDirect(type->_reprer, 1);
|
||||
if (IS_STRING(result)) {
|
||||
pushStringBuilderStr(&sb, AS_CSTRING(result), AS_STRING(result)->length);
|
||||
}
|
||||
}
|
||||
if (!krk_pushStringBuilderFormat(&sb, "%R", entry->value)) goto _error;
|
||||
}
|
||||
|
||||
pushStringBuilder(&sb,'}');
|
||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||
return finishStringBuilder(&sb);
|
||||
|
||||
_error:
|
||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||
krk_discardStringBuilder(&sb);
|
||||
return NONE_VAL();
|
||||
}
|
||||
|
||||
KRK_Method(dict,copy) {
|
||||
@ -368,39 +354,23 @@ KRK_Method(dictitems,__repr__) {
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
KrkTableEntry * entry = &AS_DICT(self->dict)->entries[i];
|
||||
if (IS_KWARGS(entry->key)) continue;
|
||||
if (c > 0) {
|
||||
pushStringBuilderStr(&sb, ", ", 2);
|
||||
}
|
||||
if (c) pushStringBuilderStr(&sb, ", ", 2);
|
||||
c++;
|
||||
|
||||
pushStringBuilder(&sb,'(');
|
||||
|
||||
{
|
||||
KrkClass * type = krk_getType(entry->key);
|
||||
krk_push(entry->key);
|
||||
KrkValue result = krk_callDirect(type->_reprer, 1);
|
||||
if (IS_STRING(result)) {
|
||||
pushStringBuilderStr(&sb, AS_CSTRING(result), AS_STRING(result)->length);
|
||||
}
|
||||
}
|
||||
|
||||
if (!krk_pushStringBuilderFormat(&sb, "%R", entry->key)) goto _error;
|
||||
pushStringBuilderStr(&sb, ", ", 2);
|
||||
|
||||
{
|
||||
KrkClass * type = krk_getType(entry->value);
|
||||
krk_push(entry->value);
|
||||
KrkValue result = krk_callDirect(type->_reprer, 1);
|
||||
if (IS_STRING(result)) {
|
||||
pushStringBuilderStr(&sb, AS_CSTRING(result), AS_STRING(result)->length);
|
||||
}
|
||||
}
|
||||
|
||||
if (!krk_pushStringBuilderFormat(&sb, "%R", entry->value)) goto _error;
|
||||
pushStringBuilder(&sb,')');
|
||||
}
|
||||
|
||||
pushStringBuilderStr(&sb,"])",2);
|
||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||
return finishStringBuilder(&sb);
|
||||
|
||||
_error:
|
||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||
krk_discardStringBuilder(&sb);
|
||||
return NONE_VAL();
|
||||
}
|
||||
|
||||
#undef CURRENT_CTYPE
|
||||
@ -449,24 +419,19 @@ KRK_Method(dictkeys,__repr__) {
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
KrkTableEntry * entry = &AS_DICT(self->dict)->entries[i];
|
||||
if (IS_KWARGS(entry->key)) continue;
|
||||
if (c > 0) {
|
||||
pushStringBuilderStr(&sb, ", ", 2);
|
||||
}
|
||||
if (c) pushStringBuilderStr(&sb, ", ", 2);
|
||||
c++;
|
||||
|
||||
{
|
||||
KrkClass * type = krk_getType(entry->key);
|
||||
krk_push(entry->key);
|
||||
KrkValue result = krk_callDirect(type->_reprer, 1);
|
||||
if (IS_STRING(result)) {
|
||||
pushStringBuilderStr(&sb, AS_CSTRING(result), AS_STRING(result)->length);
|
||||
}
|
||||
}
|
||||
if (!krk_pushStringBuilderFormat(&sb, "%R", entry->key)) goto _error;
|
||||
}
|
||||
|
||||
pushStringBuilderStr(&sb,"])",2);
|
||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||
return finishStringBuilder(&sb);
|
||||
|
||||
_error:
|
||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||
krk_discardStringBuilder(&sb);
|
||||
return NONE_VAL();
|
||||
}
|
||||
|
||||
#undef CURRENT_CTYPE
|
||||
@ -515,24 +480,19 @@ KRK_Method(dictvalues,__repr__) {
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
KrkTableEntry * entry = &AS_DICT(self->dict)->entries[i];
|
||||
if (IS_KWARGS(entry->key)) continue;
|
||||
if (c > 0) {
|
||||
pushStringBuilderStr(&sb, ", ", 2);
|
||||
}
|
||||
if (c) pushStringBuilderStr(&sb, ", ", 2);
|
||||
c++;
|
||||
|
||||
{
|
||||
KrkClass * type = krk_getType(entry->value);
|
||||
krk_push(entry->value);
|
||||
KrkValue result = krk_callDirect(type->_reprer, 1);
|
||||
if (IS_STRING(result)) {
|
||||
pushStringBuilderStr(&sb, AS_CSTRING(result), AS_STRING(result)->length);
|
||||
}
|
||||
}
|
||||
if (!krk_pushStringBuilderFormat(&sb, "%R", entry->value)) goto _error;
|
||||
}
|
||||
|
||||
pushStringBuilderStr(&sb,"])",2);
|
||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||
return finishStringBuilder(&sb);
|
||||
|
||||
_error:
|
||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||
krk_discardStringBuilder(&sb);
|
||||
return NONE_VAL();
|
||||
}
|
||||
|
||||
_noexport
|
||||
|
@ -138,15 +138,7 @@ KRK_Method(list,__repr__) {
|
||||
pushStringBuilder(&sb, '[');
|
||||
pthread_rwlock_rdlock(&self->rwlock);
|
||||
for (size_t i = 0; i < self->values.count; ++i) {
|
||||
/* repr(self[i]) */
|
||||
KrkClass * type = krk_getType(self->values.values[i]);
|
||||
krk_push(self->values.values[i]);
|
||||
KrkValue result = krk_callDirect(type->_reprer, 1);
|
||||
|
||||
if (IS_STRING(result)) {
|
||||
pushStringBuilderStr(&sb, AS_STRING(result)->chars, AS_STRING(result)->length);
|
||||
}
|
||||
|
||||
if (!krk_pushStringBuilderFormat(&sb,"%R",self->values.values[i])) goto _error;
|
||||
if (i + 1 < self->values.count) {
|
||||
pushStringBuilderStr(&sb, ", ", 2);
|
||||
}
|
||||
@ -156,6 +148,12 @@ KRK_Method(list,__repr__) {
|
||||
pushStringBuilder(&sb,']');
|
||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||
return finishStringBuilder(&sb);
|
||||
|
||||
_error:
|
||||
krk_discardStringBuilder(&sb);
|
||||
pthread_rwlock_unlock(&self->rwlock);
|
||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||
return NONE_VAL();
|
||||
}
|
||||
|
||||
static int _list_extend_callback(void * context, const KrkValue * values, size_t count) {
|
||||
|
@ -80,22 +80,19 @@ KRK_Method(set,__repr__) {
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
KrkTableEntry * entry = &self->entries.entries[i];
|
||||
if (IS_KWARGS(entry->key)) continue;
|
||||
if (c > 0) {
|
||||
pushStringBuilderStr(&sb, ", ", 2);
|
||||
}
|
||||
if (c) pushStringBuilderStr(&sb, ", ", 2);
|
||||
c++;
|
||||
|
||||
KrkClass * type = krk_getType(entry->key);
|
||||
krk_push(entry->key);
|
||||
KrkValue result = krk_callDirect(type->_reprer, 1);
|
||||
if (IS_STRING(result)) {
|
||||
pushStringBuilderStr(&sb, AS_CSTRING(result), AS_STRING(result)->length);
|
||||
}
|
||||
if (!krk_pushStringBuilderFormat(&sb, "%R", entry->key)) goto _error;
|
||||
}
|
||||
|
||||
pushStringBuilder(&sb,'}');
|
||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||
return finishStringBuilder(&sb);
|
||||
|
||||
_error:
|
||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||
krk_discardStringBuilder(&sb);
|
||||
return NONE_VAL();
|
||||
}
|
||||
|
||||
KRK_Method(set,__and__) {
|
||||
|
@ -122,33 +122,20 @@ KRK_Method(slice,__repr__) {
|
||||
((KrkObj*)self)->flags |= KRK_OBJ_FLAGS_IN_REPR;
|
||||
struct StringBuilder sb = {0};
|
||||
pushStringBuilderStr(&sb,"slice(",6);
|
||||
|
||||
KrkClass * type;
|
||||
KrkValue result;
|
||||
|
||||
/* start */
|
||||
type = krk_getType(self->start);
|
||||
krk_push(self->start);
|
||||
result = krk_callDirect(type->_reprer, 1);
|
||||
if (IS_STRING(result)) pushStringBuilderStr(&sb, AS_STRING(result)->chars, AS_STRING(result)->length);
|
||||
if (!krk_pushStringBuilderFormat(&sb, "%R", self->start)) goto _error;
|
||||
pushStringBuilderStr(&sb,", ",2);
|
||||
|
||||
/* end */
|
||||
type = krk_getType(self->end);
|
||||
krk_push(self->end);
|
||||
result = krk_callDirect(type->_reprer, 1);
|
||||
if (IS_STRING(result)) pushStringBuilderStr(&sb, AS_STRING(result)->chars, AS_STRING(result)->length);
|
||||
if (!krk_pushStringBuilderFormat(&sb, "%R", self->end)) goto _error;
|
||||
pushStringBuilderStr(&sb,", ",2);
|
||||
|
||||
/* step */
|
||||
type = krk_getType(self->step);
|
||||
krk_push(self->step);
|
||||
result = krk_callDirect(type->_reprer, 1);
|
||||
if (IS_STRING(result)) pushStringBuilderStr(&sb, AS_STRING(result)->chars, AS_STRING(result)->length);
|
||||
|
||||
if (!krk_pushStringBuilderFormat(&sb, "%R", self->step)) goto _error;
|
||||
pushStringBuilder(&sb,')');
|
||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||
return finishStringBuilder(&sb);
|
||||
|
||||
_error:
|
||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||
krk_discardStringBuilder(&sb);
|
||||
return NONE_VAL();
|
||||
}
|
||||
|
||||
KRK_Method(slice,start) {
|
||||
|
@ -148,15 +148,8 @@ KRK_Method(tuple,__repr__) {
|
||||
pushStringBuilder(&sb, '(');
|
||||
|
||||
for (size_t i = 0; i < self->values.count; ++i) {
|
||||
KrkClass * type = krk_getType(self->values.values[i]);
|
||||
krk_push(self->values.values[i]);
|
||||
KrkValue result = krk_callDirect(type->_reprer, 1);
|
||||
if (IS_STRING(result)) {
|
||||
pushStringBuilderStr(&sb, AS_STRING(result)->chars, AS_STRING(result)->length);
|
||||
}
|
||||
if (i != self->values.count - 1) {
|
||||
pushStringBuilderStr(&sb, ", ", 2);
|
||||
}
|
||||
if (i) pushStringBuilderStr(&sb, ", ", 2);
|
||||
if (!krk_pushStringBuilderFormat(&sb, "%R", self->values.values[i])) goto _error;
|
||||
}
|
||||
|
||||
if (self->values.count == 1) {
|
||||
@ -166,6 +159,11 @@ KRK_Method(tuple,__repr__) {
|
||||
pushStringBuilder(&sb, ')');
|
||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||
return finishStringBuilder(&sb);
|
||||
|
||||
_error:
|
||||
((KrkObj*)self)->flags &= ~(KRK_OBJ_FLAGS_IN_REPR);
|
||||
krk_discardStringBuilder(&sb);
|
||||
return NONE_VAL();
|
||||
}
|
||||
|
||||
KRK_Method(tuple,__add__) {
|
||||
|
Loading…
Reference in New Issue
Block a user