大写如何工作?

fab*_*bbb 1 ruby ruby-c-extension

我试图了解String#capitalize!内部如何运作.我可以创建一个哈希.给定字符串foo = "the",foo[0]"t",查找lower_case "t",并将其与大写"T"值匹配.实际上,Ruby源代码显示:

static VALUE
rb_str_capitalize_bang(VALUE str)
{
    rb_encoding *enc;
    char *s, *send;
    int modify = 0;
    unsigned int c;
    int n;

    str_modify_keep_cr(str);
    enc = STR_ENC_GET(str);
    rb_str_check_dummy_enc(enc);
    if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return Qnil;
    s = RSTRING_PTR(str); send = RSTRING_END(str);

    c = rb_enc_codepoint_len(s, send, &n, enc);
    if (rb_enc_islower(c, enc)) {
        rb_enc_mbcput(rb_enc_toupper(c, enc), s, enc);
        modify = 1;
    }
    s += n;
    while (s < send) {
        c = rb_enc_codepoint_len(s, send, &n, enc);
        if (rb_enc_isupper(c, enc)) {
            rb_enc_mbcput(rb_enc_tolower(c, enc), s, enc);
            modify = 1;
        }
        s += n;
    }

    if (modify) return str;
    return Qnil;
}
Run Code Online (Sandbox Code Playgroud)

相关功能是toupper.它如何知道toupper("t")平等"T"

Chu*_*uck 6

你想知道它是如何知道角色的大写版本是什么?像大多数此类函数的实际实现一样,它使用查找表.