Click here to Skip to main content
13,090,426 members (51,561 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

2.5K views
1 bookmarked
Posted 23 Mar 2011

How to Toggle String Case in .NET

, 23 Mar 2011
Rate this:
Please Sign up or sign in to vote.
And yet another ...5% slower ... than the fastest to date -> SimmoTech's v2,(an awesome piece of righteously parsimonious code).85% less filling ... the diet version -> 44 vs 256 bytes(.Net v2 and above)private const int AddressBitsPerUnit = 5;private const int BitsPerElement =...
And yet another ...

5% slower ... than the fastest to date -> SimmoTech's v2,
(an awesome piece of righteously parsimonious code).

85% less filling ... the diet version -> 44 vs 256 bytes

(.Net v2 and above)
private const int AddressBitsPerUnit = 5;
private const int BitsPerElement = 32;
private const int BitIndexMask = BitsPerElement - 1;
private static readonly int[] ValidLatinChars = { 0, 0, 134217726, 134217726, 0, 0, 2139095039, 2139095039 };
public static string ToggleCase_SimmoTech_MacMaverick( string s )
{
  TextInfo currentCultureTextInfo = CultureInfo.CurrentCulture.TextInfo;
  char[] chs = s.ToCharArray();
  for ( int i = 0; i < s.Length; ++i )
  {
    char ch = chs[i];
    if ( ch <= 0xFF )
    {
      int bitSet = 1 << ( ch & BitIndexMask );
      if ( ( ValidLatinChars[( ch >> AddressBitsPerUnit )] & bitSet ) == bitSet )
      {
        chs[i] = (char) ( ch ^ 0x20 );
      }
      continue;
    }
    switch ( char.GetUnicodeCategory( ch ) )
    {
      case UnicodeCategory.UppercaseLetter:
        chs[i] = currentCultureTextInfo.ToLower( ch );
        break;
      case UnicodeCategory.LowercaseLetter:
        chs[i] = currentCultureTextInfo.ToUpper( ch );
        break;
    }
  }
  return ( new string( chs ) );
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

MacMaverick
Australia Australia
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 23 Mar 2011
Article Copyright 2011 by MacMaverick
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid