[macruby-changes] [145] MacRuby/trunk/string.c
source_changes at macosforge.org
source_changes at macosforge.org
Thu Apr 17 00:18:20 PDT 2008
Revision: 145
http://trac.macosforge.org/projects/ruby/changeset/145
Author: lsansonetti at apple.com
Date: 2008-04-17 00:18:14 -0700 (Thu, 17 Apr 2008)
Log Message:
-----------
fixing String#capitalize
Modified Paths:
--------------
MacRuby/trunk/string.c
Modified: MacRuby/trunk/string.c
===================================================================
--- MacRuby/trunk/string.c 2008-04-17 01:14:09 UTC (rev 144)
+++ MacRuby/trunk/string.c 2008-04-17 07:18:14 UTC (rev 145)
@@ -4546,18 +4546,33 @@
rb_str_capitalize_bang(VALUE str)
{
#if WITH_OBJC
+ CFStringRef tmp;
+ long i, n;
+ bool changed;
UniChar c;
- CFStringRef tmp;
+ UniChar *buffer;
rb_str_modify(str);
- if (CFStringGetLength((CFStringRef)str) == 0)
+ n = CFStringGetLength((CFStringRef)str);
+ if (n == 0)
return Qnil;
- c = CFStringGetCharacterAtIndex((CFStringRef)str, 0);
- if (!iswlower(c))
+ buffer = (UniChar *)alloca(sizeof(UniChar) * n);
+ CFStringGetCharacters((CFStringRef)str, CFRangeMake(0, n), buffer);
+ changed = false;
+ if (iswlower(buffer[0])) {
+ buffer[0] = towupper(buffer[0]);
+ changed = true;
+ }
+ for (i = 1; i < n; i++) {
+ if (iswupper(buffer[i])) {
+ buffer[i] = towlower(buffer[i]);
+ changed = true;
+ }
+ }
+ if (!changed)
return Qnil;
- c = towupper(c);
- tmp = CFStringCreateWithCharacters(NULL, &c, 1);
- CFStringReplace((CFMutableStringRef)str, CFRangeMake(0, 1), tmp);
+ tmp = CFStringCreateWithCharacters(NULL, buffer, n);
+ CFStringReplaceAll((CFMutableStringRef)str, tmp);
return str;
#else
rb_encoding *enc;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macruby-changes/attachments/20080417/16e0b781/attachment.html
More information about the macruby-changes
mailing list