diff options
| author | Yukihiro Matsumoto <[email protected]> | 2012-05-31 16:31:33 +0900 |
|---|---|---|
| committer | Yukihiro Matsumoto <[email protected]> | 2012-05-31 16:31:33 +0900 |
| commit | b05014f6a9666b66935c33543edd9fcf451ae3a1 (patch) | |
| tree | fa5828951285bdfd990965da5a7120b2e243f223 /src | |
| parent | 0009239f29039f5f4534eb56c1f8a907b28d744c (diff) | |
| download | mruby-b05014f6a9666b66935c33543edd9fcf451ae3a1.tar.gz mruby-b05014f6a9666b66935c33543edd9fcf451ae3a1.zip | |
reimplement String#capitalize
Diffstat (limited to 'src')
| -rw-r--r-- | src/string.c | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/string.c b/src/string.c index 1511c4f88..3417b63d6 100644 --- a/src/string.c +++ b/src/string.c @@ -794,6 +794,22 @@ mrb_str_aref_m(mrb_state *mrb, mrb_value str) static mrb_value mrb_str_capitalize_bang(mrb_state *mrb, mrb_value str) { + char *s, *send; + int modify = 0; + + if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return mrb_nil_value(); + s = RSTRING_PTR(str); send = RSTRING_END(str); + if (ISLOWER(*s)) { + *s = toupper(*s); + modify = 1; + } + while (++s < send) { + if (ISUPPER(*s)) { + *s = tolower(*s); + modify = 1; + } + } + if (modify) return str; return mrb_nil_value(); } |
