Advertisement
ivandrofly

CharUtil C#

Jul 27th, 2015
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.21 KB | None | 0 0
  1. #region License
  2. /*---------------------------------------------------------------------------------*\
  3.  
  4.     Distributed under the terms of an MIT-style license:
  5.  
  6.     The MIT License
  7.  
  8.     Copyright (c) 2006-2010 Stephen M. McKamey
  9.  
  10.     Permission is hereby granted, free of charge, to any person obtaining a copy
  11.     of this software and associated documentation files (the "Software"), to deal
  12.     in the Software without restriction, including without limitation the rights
  13.     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14.     copies of the Software, and to permit persons to whom the Software is
  15.     furnished to do so, subject to the following conditions:
  16.  
  17.     The above copyright notice and this permission notice shall be included in
  18.     all copies or substantial portions of the Software.
  19.  
  20.     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21.     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22.     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23.     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24.     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25.     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26.     THE SOFTWARE.
  27.  
  28. \*---------------------------------------------------------------------------------*/
  29. #endregion License
  30.  
  31. using System;
  32.  
  33. namespace JsonFx.Utils
  34. {
  35.     /// <summary>
  36.     /// Character Utility
  37.     /// </summary>
  38.     /// <remarks>
  39.     /// These are either simpler definitions of character classes (e.g. letter is [a-zA-Z]),
  40.     /// or they implement platform-agnositic checks (read: "Silverlight workarounds").
  41.     /// </remarks>
  42.     public static class CharUtility
  43.     {
  44.         #region Char Methods
  45.  
  46.         /// <summary>
  47.         /// Checks if string is null, empty or entirely made up of whitespace
  48.         /// </summary>
  49.         /// <param name="value"></param>
  50.         /// <returns></returns>
  51.         /// <remarks>
  52.         /// Essentially the same as String.IsNullOrWhiteSpace from .NET 4.0
  53.         /// with a simplfied view of whitespace.
  54.         /// </remarks>
  55.         public static bool IsNullOrWhiteSpace(string value)
  56.         {
  57.             if (value != null)
  58.             {
  59.                 for (int i = 0, length = value.Length; i < length; i++)
  60.                 {
  61.                     if (!CharUtility.IsWhiteSpace(value[i]))
  62.                     {
  63.                         return false;
  64.                     }
  65.                 }
  66.             }
  67.             return true;
  68.         }
  69.  
  70.         /// <summary>
  71.         /// Checks if character is line ending, tab or space
  72.         /// </summary>
  73.         /// <param name="ch"></param>
  74.         /// <returns></returns>
  75.         public static bool IsWhiteSpace(char ch)
  76.         {
  77.             return
  78.                 (ch == ' ') |
  79.                 (ch == '\n') ||
  80.                 (ch == '\r') ||
  81.                 (ch == '\t');
  82.         }
  83.  
  84.         /// <summary>
  85.         ///
  86.         /// </summary>
  87.         /// <param name="ch"></param>
  88.         /// <returns></returns>
  89.         public static bool IsControl(char ch)
  90.         {
  91.             return (ch <= '\u001F');
  92.         }
  93.  
  94.         /// <summary>
  95.         /// Checks if character matches [A-Za-z]
  96.         /// </summary>
  97.         /// <param name="ch"></param>
  98.         /// <returns></returns>
  99.         public static bool IsLetter(char ch)
  100.         {
  101.             return
  102.                 ((ch >= 'a') && (ch <= 'z')) ||
  103.                 ((ch >= 'A') && (ch <= 'Z'));
  104.         }
  105.  
  106.         /// <summary>
  107.         /// Checks if character matches [0-9]
  108.         /// </summary>
  109.         /// <param name="ch"></param>
  110.         /// <returns></returns>
  111.         public static bool IsDigit(char ch)
  112.         {
  113.             return (ch >= '0') && (ch <= '9');
  114.         }
  115.  
  116.         /// <summary>
  117.         /// Checks if character matches [0-9A-Fa-f]
  118.         /// </summary>
  119.         /// <param name="ch"></param>
  120.         /// <returns></returns>
  121.         public static bool IsHexDigit(char ch)
  122.         {
  123.             return
  124.                 (ch >= '0' && ch <= '9') ||
  125.                 (ch >= 'A' && ch <= 'F') ||
  126.                 (ch >= 'a' && ch <= 'f');
  127.         }
  128.  
  129.         /// <summary>
  130.         /// Gets a 4-bit number as a hex digit
  131.         /// </summary>
  132.         /// <param name="i">0-15</param>
  133.         /// <returns></returns>
  134.         public static char GetHexDigit(int i)
  135.         {
  136.             if (i < 10)
  137.             {
  138.                 return (char)(i + '0');
  139.             }
  140.  
  141.             return (char)((i - 10) + 'a');
  142.         }
  143.  
  144.         /// <summary>
  145.         /// Formats a number as a hex digit
  146.         /// </summary>
  147.         /// <param name="i"></param>
  148.         /// <returns></returns>
  149.         public static string GetHexString(ulong i)
  150.         {
  151.             string hex = "";
  152.             while (i > 0)
  153.             {
  154.                 hex = String.Concat(CharUtility.GetHexDigit((int)(i % 0x10)), hex);
  155.                 i >>= 4;
  156.             }
  157.             return hex;
  158.         }
  159.  
  160.         /// <summary>
  161.         /// Converts the value of a UTF-16 encoded character or surrogate pair at a specified
  162.         /// position in a string into a Unicode code point.
  163.         /// </summary>
  164.         /// <param name="value"></param>
  165.         /// <param name="i"></param>
  166.         /// <returns></returns>
  167.         public static int ConvertToUtf32(string value, int index)
  168.         {
  169. #if SILVERLIGHT
  170.             return (int)value[index];
  171. #else
  172.             return Char.ConvertToUtf32(value, index);
  173. #endif
  174.         }
  175.  
  176.         /// <summary>
  177.         /// Converts the specified Unicode code point into a UTF-16 encoded string.
  178.         /// </summary>
  179.         /// <param name="utf32"></param>
  180.         /// <returns></returns>
  181.         public static string ConvertFromUtf32(int utf32)
  182.         {
  183. #if SILVERLIGHT
  184.             if (utf32 <= 0xFFFF)
  185.             {
  186.                 return new string((char)utf32, 1);
  187.             }
  188.  
  189.             utf32 -= 0x10000;
  190.  
  191.             return new string(
  192.                 new char[]
  193.                 {
  194.                     (char)((utf32 / 0x400) + 0xD800),
  195.                     (char)((utf32 % 0x400) + 0xDC00)
  196.                 });
  197. #else
  198.             return Char.ConvertFromUtf32(utf32);
  199. #endif
  200.         }
  201.  
  202.         #endregion Char Methods
  203.     }
  204. }
  205.  
  206. // Source: https://github.com/jsonfx/jsonfx
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement