capitalize!()
public
Modifies str by converting the first character to uppercase and
the remainder to lowercase. Returns nil if no changes are made.
a = "hello"
a.capitalize!
a
a.capitalize!
Show source
/*
* call-seq:
* str.capitalize! => str or nil
*
* Modifies <i>str</i> by converting the first character to uppercase and the
* remainder to lowercase. Returns <code>nil</code> if no changes are made.
*
* a = "hello"
* a.capitalize! #=> "Hello"
* a #=> "Hello"
* a.capitalize! #=> nil
*/
static VALUE
rb_str_capitalize_bang(str)
VALUE str;
{
char *s, *send;
int modify = 0;
rb_str_modify(str);
if (RSTRING(str)->len == 0 || !RSTRING(str)->ptr) return Qnil;
s = RSTRING(str)->ptr; send = s + RSTRING(str)->len;
if (ISLOWER(*s)) {
*s = toupper(*s);
modify = 1;
}
while (++s < send) {
if (ismbchar(*s)) {
s+=mbclen(*s) - 1;
}
else if (ISUPPER(*s)) {
*s = tolower(*s);
modify = 1;
}
}
if (modify) return str;
return Qnil;
}