<<(p1)
public
Append—Concatenates the given object to str. If the object is a
Integer, it is considered as a codepoint, and
is converted to a character before concatenation.
a = "hello "
a << "world"
a.concat(33)
Show source
VALUE
rb_str_concat(VALUE str1, VALUE str2)
{
unsigned int lc;
if (FIXNUM_P(str2)) {
if ((int)str2 < 0)
rb_raise(rb_eRangeError, "negative argument");
lc = FIX2UINT(str2);
}
else if (TYPE(str2) == T_BIGNUM) {
if (!RBIGNUM_SIGN(str2))
rb_raise(rb_eRangeError, "negative argument");
lc = NUM2UINT(str2);
}
else {
return rb_str_append(str1, str2);
}
if ((VALUE)lc > UINT_MAX) {
rb_raise(rb_eRangeError, "%"PRIuVALUE" out of char range", lc);
}
{
rb_encoding *enc = STR_ENC_GET(str1);
long pos = RSTRING_LEN(str1);
int cr = ENC_CODERANGE(str1);
int len;
if ((len = rb_enc_codelen(lc, enc)) <= 0) {
rb_raise(rb_eRangeError, "%u invalid char", lc);
}
rb_str_resize(str1, pos+len);
rb_enc_mbcput(lc, RSTRING_PTR(str1)+pos, enc);
if (cr == ENC_CODERANGE_7BIT && lc > 127)
cr = ENC_CODERANGE_VALID;
ENC_CODERANGE_SET(str1, cr);
return str1;
}
}