We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 30b6648 commit 8a9a241Copy full SHA for 8a9a241
1 file changed
src/string.c
@@ -695,14 +695,21 @@ mrb_str_modify(mrb_state *mrb, struct RString *s)
695
}
696
if (RSTR_NOFREE_P(s)) {
697
char *p = s->as.heap.ptr;
698
+ mrb_int len = s->as.heap.len;
699
- s->as.heap.ptr = (char *)mrb_malloc(mrb, (size_t)s->as.heap.len+1);
700
+ RSTR_UNSET_NOFREE_FLAG(s);
701
+ if (len < RSTRING_EMBED_LEN_MAX) {
702
+ RSTR_SET_EMBED_FLAG(s);
703
+ RSTR_SET_EMBED_LEN(s, len);
704
+ }
705
+ else {
706
+ s->as.heap.ptr = (char *)mrb_malloc(mrb, (size_t)len+1);
707
+ s->as.heap.aux.capa = len;
708
709
if (p) {
- memcpy(RSTR_PTR(s), p, s->as.heap.len);
710
+ memcpy(RSTR_PTR(s), p, len);
711
- RSTR_PTR(s)[s->as.heap.len] = '\0';
- s->as.heap.aux.capa = s->as.heap.len;
- RSTR_UNSET_NOFREE_FLAG(s);
712
+ RSTR_PTR(s)[len] = '\0';
713
return;
714
715
0 commit comments