Author: atsushi
Date: 2005-04-05 15:57:20 -0400 (Tue, 05 Apr 2005)
New Revision: 42580
Modified:
trunk/mcs/class/corlib/System.Globalization/ChangeLog
trunk/mcs/class/corlib/System.Globalization/TextInfo.cs
trunk/mcs/class/corlib/Test/System.Globalization/ChangeLog
trunk/mcs/class/corlib/Test/System.Globalization/TextInfoTest.cs
Log:
2005-04-05 Atsushi Enomoto <[EMAIL PROTECTED]>
* TextInfo.cs : Fixed ToTitleCase() so that
- handle some titlecase characters in Latin ang Greek.
- it keeps "all title case" word as is ("ABC" does not become "Abc").
- it can handle such string like "1ABc".
* TextInfoTest.cs : added further ToTitleCase() tests.
Modified: trunk/mcs/class/corlib/System.Globalization/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/System.Globalization/ChangeLog 2005-04-05
19:27:46 UTC (rev 42579)
+++ trunk/mcs/class/corlib/System.Globalization/ChangeLog 2005-04-05
19:57:20 UTC (rev 42580)
@@ -1,3 +1,10 @@
+2005-04-05 Atsushi Enomoto <[EMAIL PROTECTED]>
+
+ * TextInfo.cs : Fixed ToTitleCase() so that
+ - handle some titlecase characters in Latin ang Greek.
+ - it keeps "all title case" word as is ("ABC" does not become "Abc").
+ - it can handle such string like "1ABc".
+
2005-04-04 Atsushi Enomoto <[EMAIL PROTECTED]>
* TextInfo.cs : implemented culture sensitive ToLower() and ToUpper()
Modified: trunk/mcs/class/corlib/System.Globalization/TextInfo.cs
===================================================================
--- trunk/mcs/class/corlib/System.Globalization/TextInfo.cs 2005-04-05
19:27:46 UTC (rev 42579)
+++ trunk/mcs/class/corlib/System.Globalization/TextInfo.cs 2005-04-05
19:57:20 UTC (rev 42580)
@@ -4,14 +4,16 @@
// Author:
// Dick Porter ([EMAIL PROTECTED])
// Duncan Mak ([EMAIL PROTECTED])
+// Atsushi Enomoto ([EMAIL PROTECTED])
//
// (C) 2002 Ximian, Inc.
+// (C) 2005 Novell, Inc.
//
// TODO:
// Missing the various code page mappings.
// Missing the OnDeserialization implementation.
//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004, 2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
@@ -145,26 +147,68 @@
{
if(str == null)
throw new ArgumentNullException("string is
null");
-
- Text.StringBuilder s = new Text.StringBuilder ();
- bool space_seen = true;
-
- for (int i = 0; i < str.Length; i ++){
- char c = str [i];
- if (Char.IsLetter (c)){
- if (space_seen)
- s.Append (Char.ToUpper (c, ci));
- else
- s.Append (Char.ToLower (c, ci));
- space_seen = false;
- } else {
- s.Append (c);
- if (Char.IsWhiteSpace (c))
- space_seen = true;
+
+ StringBuilder sb = null;
+ int i = 0;
+ int start = 0;
+ while (i < str.Length) {
+ if (!Char.IsLetter (str [i++]))
+ continue;
+ i--;
+ char t = ToTitleCase (str [i]);
+ bool capitalize = true;
+ if (t == str [i]) {
+ capitalize = false;
+ bool allTitle = true;
+ // if the word is all titlecase,
+ // then don't capitalize it.
+ int saved = i;
+ while (++i < str.Length) {
+ if (Char.IsWhiteSpace (str [i]))
+ break;
+ t = ToTitleCase (str [i]);
+ if (t != str [i]) {
+ allTitle = false;
+ break;
+ }
+ }
+ if (allTitle)
+ continue;
+ i = saved;
+
+ // still check if all remaining
+ // characters are lowercase,
+ // where we don't have to modify
+ // the source word.
+ while (++i < str.Length) {
+ if (Char.IsWhiteSpace (str [i]))
+ break;
+ if (ToLower (str [i]) != str
[i]) {
+ capitalize = true;
+ i = saved;
+ break;
+ }
+ }
}
+
+ if (capitalize) {
+ if (sb == null)
+ sb = new StringBuilder
(str.Length);
+ sb.Append (str, start, i - start);
+ sb.Append (ToTitleCase (str [i]));
+ start = i + 1;
+ while (++i < str.Length) {
+ if (Char.IsWhiteSpace (str [i]))
+ break;
+ sb.Append (ToLower (str [i]));
+ }
+ start = i;
+ }
}
+ if (sb != null)
+ sb.Append (str, start, str.Length - start);
- return s.ToString ();
+ return sb != null ? sb.ToString () : str;
}
// Only Azeri and Turkish have their own special cases.
@@ -262,6 +306,33 @@
return Char.ToUpperInvariant (c);
}
+ private char ToTitleCase (char c)
+ {
+ // Handle some Latin characters.
+ switch (c) {
+ case '\u01c4':
+ case '\u01c5':
+ case '\u01c6':
+ return '\u01c5';
+ case '\u01c7':
+ case '\u01c8':
+ case '\u01c9':
+ return '\u01c8';
+ case '\u01ca':
+ case '\u01cb':
+ case '\u01cc':
+ return '\u01cb';
+ case '\u01f1':
+ case '\u01f2':
+ case '\u01f3':
+ return '\u01f2';
+ }
+ if ('\u2170' <= c && c <= '\u217f' || // Roman numbers
+ '\u24d0' <= c && c <= '\u24e9')
+ return c;
+ return ToUpper (c);
+ }
+
public virtual string ToLower (string s)
{
// In ICU (3.2) there are a few cases that one single
@@ -293,13 +364,13 @@
if (s [i] != convert (s [i])) {
if (sb == null)
sb = new StringBuilder
(s.Length);
- sb.Append (s.Substring (start, i -
start));
+ sb.Append (s, start, i - start);
sb.Append (convert (s [i]));
start = i + 1;
}
}
if (sb != null && start < s.Length)
- sb.Append (s.Substring (start));
+ sb.Append (s, start, s.Length - start);
return sb == null ? s : sb.ToString ();
}
Modified: trunk/mcs/class/corlib/Test/System.Globalization/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/Test/System.Globalization/ChangeLog 2005-04-05
19:27:46 UTC (rev 42579)
+++ trunk/mcs/class/corlib/Test/System.Globalization/ChangeLog 2005-04-05
19:57:20 UTC (rev 42580)
@@ -1,3 +1,7 @@
+2005-04-05 Atsushi Enomoto <[EMAIL PROTECTED]>
+
+ * TextInfoTest.cs : added further ToTitleCase() tests.
+
2004-10-27 Ben Maurer <[EMAIL PROTECTED]>
* TextInfoTest.cs: Fix up migster's type
Modified: trunk/mcs/class/corlib/Test/System.Globalization/TextInfoTest.cs
===================================================================
--- trunk/mcs/class/corlib/Test/System.Globalization/TextInfoTest.cs
2005-04-05 19:27:46 UTC (rev 42579)
+++ trunk/mcs/class/corlib/Test/System.Globalization/TextInfoTest.cs
2005-04-05 19:57:20 UTC (rev 42580)
@@ -28,6 +28,42 @@
AssertEquals ("La Guerra Yla Paz", ti.ToTitleCase ("la Guerra
yLa pAz"));
AssertEquals ("\tTab\tAnd\tPeace", ti.ToTitleCase
("\ttab\taNd\tpeaCE"));
}
+
+ [Test]
+ public void TitleCase2 ()
+ {
+ foreach (CultureInfo ci in CultureInfo.GetCultures
(CultureTypes.AllCultures)) {
+ Check (ci, "AB", "AB");
+ Check (ci, "ab", "Ab");
+ Check (ci, "aB", "Ab");
+ Check (ci, "1Ab", "1Ab");
+ Check (ci, "abc AB ab aB Ab ABc 1AB 1Ab 1ab 1aB",
+ "Abc AB Ab Ab Ab Abc 1AB 1Ab 1Ab 1Ab");
+ Check (ci, "LJ", "LJ");
+ Check (ci, "lj", "Lj");
+ Check (ci, "lJ", "Lj");
+ Check (ci, "lj abc ljabc", "Lj Abc Ljabc");
+ Check (ci, "ab", "Ab");
+
+ // Some greek titlecase characters
+ Check (ci, "\u01c4", "\u01c5");
+ Check (ci, "\u01c5", "\u01c5");
+ Check (ci, "\u01c6", "\u01c5");
+ Check (ci, "\u01ca", "\u01cb");
+ Check (ci, "\u01cb", "\u01cb");
+ Check (ci, "\u01cc", "\u01cb");
+ // Roman numbers are not converted unlike ToUpper().
+ Check (ci, "\u2170", "\u2170");
+ Check (ci, "\u24e9", "\u24e9");
+ }
+ }
+
+ private void Check (CultureInfo ci, string src, string expected)
+ {
+ AssertEquals (src + " at culture " + ci.LCID,
+ expected,
+ ci.TextInfo.ToTitleCase (src));
+ }
}
}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches