Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 18. Cocktails > The CharHelper Class

The CharHelper Class

In Listing 18.6, QuickJumpGrid uses a class called CharHelper to figure out which of the 27 “buckets” each string key belongs in (a-z or #). This class is implemented in Listing 18.7.

Listing 18.7. CharHelper.cs—A Static Helper Class for Bucketizing Entries

using System;
using System.Collections.Generic;

namespace WindowsPhoneApp
{
  public static class CharHelper
  {
    static Dictionary<char, char> accentMap = new Dictionary<char, char>();

    static CharHelper()
    {
      // Map some common accented letters to non-accented letters
      accentMap.Add('à', 'a'); accentMap.Add('á', 'a'); accentMap.Add('â', 'a');
      accentMap.Add('ã', 'a'); accentMap.Add('ä', 'a'); accentMap.Add('', 'a');
      accentMap.Add('æ', 'a');
      accentMap.Add('è', 'e'); accentMap.Add('é', 'e'); accentMap.Add('ê', 'e');
      accentMap.Add('ë', 'e');
      accentMap.Add('ì', 'i'); accentMap.Add('í', 'i'); accentMap.Add('î', 'i');
      accentMap.Add('ï', 'i');
      accentMap.Add('ò', 'o'); accentMap.Add('ó', 'o'); accentMap.Add('ô', 'o');
      accentMap.Add('õ', 'o'); accentMap.Add('ö', 'o');
      accentMap.Add('ù', 'u'); accentMap.Add('ú', 'u'); accentMap.Add('û', 'u');
      accentMap.Add('ü', 'u');
    }

    public static char GetBucket(string s)
    {
      char c = Char.ToLowerInvariant(s[0]);
      if (!Char.IsLetter(c))
        return '#';
      return RemoveAccent(c);
    }

    static char RemoveAccent(char letter)
    {
      if (letter >= 'a' && letter <= 'z')
        return letter;

      if (accentMap.ContainsKey(letter))
        return accentMap[letter];

      // Unknown accented letter
      return '#';
    }
  }
}

					  


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial