Replacement to function for composite two string nodes

This commit is contained in:
dearblue
2019-02-27 21:49:41 +09:00
parent 477e5285e8
commit 25957421bc
+14 -7
View File
@@ -858,19 +858,26 @@ string_node_p(node *n)
return (int)((enum node_type)(intptr_t)n->car == NODE_STR);
}
static node*
composite_string_node(parser_state *p, node *a, node *b)
{
size_t newlen = (size_t)a->cdr + (size_t)b->cdr;
char *str = (char*)mrb_pool_realloc(p->pool, a->car, (size_t)a->cdr + 1, newlen + 1);
memcpy(str + (size_t)a->cdr, b->car, (size_t)b->cdr);
str[newlen] = '\0';
a->car = (node*)str;
a->cdr = (node*)newlen;
cons_free(b);
return a;
}
static node*
concat_string(parser_state *p, node *a, node *b)
{
if (string_node_p(a)) {
if (string_node_p(b)) {
/* a == NODE_STR && b == NODE_STR */
size_t newlen = (size_t)a->cdr->cdr + (size_t)b->cdr->cdr;
char *str = (char*)mrb_pool_realloc(p->pool, a->cdr->car, (size_t)a->cdr->cdr + 1, newlen + 1);
memcpy(str + (size_t)a->cdr->cdr, b->cdr->car, (size_t)b->cdr->cdr);
str[newlen] = '\0';
a->cdr->car = (node*)str;
a->cdr->cdr = (node*)newlen;
cons_free(b->cdr);
composite_string_node(p, a->cdr, b->cdr);
cons_free(b);
return a;
}