Omer van Kloeten's .NET Zen

Programming is life, the rest is mere details

News

Omer van Kloeten's Facebook profile

Get Firefox

.NET Resources

Articles :: CodeDom

Articles :: nGineer

Culture

Projects

September 2005 - Posts

Splitting Hairs
I love reading Phil Haack's weblog, because he writes about common problems and is also generally a nice guy. :)
He usually provokes me into thinking, causing me to respond to his posts quite a bit. In one of his latest posts, Splitting Pascal/Camel Cased Strings, he shows an example for a technique to split pascal cased strings.

Here's my take on this:
using System;
using System.Text;
using System.Globalization;
using System.Collections;

namespace Example
{
    class Program
    {
        public static class UnicodeUtils
        {
            private static Hashtable unicodeCategories;

            /// <summary>
            /// Gets an array of all characters in current default encoding that are of a specific unicode category.
            /// </summary>
            /// <param name="category">The category to filter according to.</param>
            /// <returns>An array of characters that represents the category.</returns>
            public static char[] GetCategoryChars(UnicodeCategory category)
            {
                if (unicodeCategories == null)
                {
                    try
                    {
                        unicodeCategories = new Hashtable(Enum.GetValues(typeof(UnicodeCategory)).Length);

                        foreach (UnicodeCategory uc in Enum.GetValues(typeof(UnicodeCategory)))
                        {
                            ArrayList list = new ArrayList();

                            for (char c = (char)0; (int)c < (Encoding.Default.IsSingleByte ? byte.MaxValue : ushort.MaxValue); c = ((char)((int)c + 1)))
                            {
                                if (char.GetUnicodeCategory(c) == uc)
                                {
                                    list.Add(c);
                                }
                            }

                            unicodeCategories.Add(uc, ((char[])(list.ToArray(typeof(char)))));
                        }
                    }
                    catch
                    {
                        unicodeCategories = null;
                        throw;
                    }
                }

                return ((char[])(unicodeCategories[category]));
            }
        }

        /// <summary>
        /// Gets a sentence from a pascal/camel cased name.
        /// </summary>
        /// <param name="name">The name to convert.</param>
        /// <returns>The sentence that was once the name.</returns>
        private static string GetSentence(string name)
        {
            int index = 1, charsFound = 0;
            StringBuilder builder = new StringBuilder(name);

            while ((index = name.IndexOfAny(UnicodeUtils.GetCategoryChars(UnicodeCategory.UppercaseLetter), index)) != -1)
                builder.Insert(index++ + charsFound++, ' ');

            return builder.ToString();
        }
    }
}
Controls, controls...

I've been thinking today - there ought to be a Wiki that lists controls, either for WinForms, ASP.NET and/or GTK#.
It's pretty annoying to have to create a new control just because you can't find one and then, a couple of months later, finding it...
Does anyone know about something like this?

More Posts