APIdock / Ruby
/
method

reverse

ruby latest stable - Class: String
reverse()
public

Returns a new string with the characters from str in reverse order.

"stressed".reverse  #=> "desserts"
static VALUE
rb_str_reverse(VALUE str)
{
 rb_encoding *enc;
 VALUE rev;
 char *s, *e, *p;
 int cr;
 if (RSTRING_LEN(str) <= 1) return rb_str_dup(str);
 enc = STR_ENC_GET(str);
 rev = rb_str_new_with_class(str, 0, RSTRING_LEN(str));
 s = RSTRING_PTR(str); e = RSTRING_END(str);
 p = RSTRING_END(rev);
 cr = ENC_CODERANGE(str);
 if (RSTRING_LEN(str) > 1) {
 if (single_byte_optimizable(str)) {
 while (s < e) {
 *--p = *s++;
 }
 }
 else if (cr == ENC_CODERANGE_VALID) {
 while (s < e) {
 int clen = rb_enc_fast_mbclen(s, e, enc);
 p -= clen;
 memcpy(p, s, clen);
 s += clen;
 }
 }
 else {
 cr = rb_enc_asciicompat(enc) ?
 ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID;
 while (s < e) {
 int clen = rb_enc_mbclen(s, e, enc);
 if (clen > 1 || (*s & 0x80)) cr = ENC_CODERANGE_UNKNOWN;
 p -= clen;
 memcpy(p, s, clen);
 s += clen;
 }
 }
 }
 STR_SET_LEN(rev, RSTRING_LEN(str));
 OBJ_INFECT_RAW(rev, str);
 str_enc_copy(rev, str);
 ENC_CODERANGE_SET(rev, cr);
 return rev;
}

AltStyle によって変換されたページ (->オリジナル) /