//****************************************************************************************************** // CharExtensions.cs - Gbtc // // Copyright © 2010, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/eclipse-1.0.php // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 12/13/2008 - F. Russell Robertson // Generated original version of source code. // 08/3/2009 - Josh L. Patterson // Updated comments. // 09/14/2009 - Stephen C. Wills // Added new header and license agreement. // 01/04/2010 - Andrew K. Hill // Modified the following methods per unit testing: // IsAny(char, IEnumerable) // 12/02/2010 - J. Ritchie Carroll // Modified IsWordTerminator to use standard Unicode character tests. // 10/8/2012 - Danyelle Gilliam // Modified Header // //****************************************************************************************************** #region [ Contributor License Agreements ] //****************************************************************************************************** // // Copyright © 2010, Grid Protection Alliance. All Rights Reserved. // // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/eclipse-1.0.php // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // //****************************************************************************************************** #endregion using System; using System.Collections.Generic; namespace GSF { /// /// Defines extension functions related to character manipulation. /// public static class CharExtensions { // so that this only happens one time private static char[] wordSeperators = { '\"', '/', '\\', '<', '>', '=', '{', '}', '(', ')', '[', ']', '@', '*' }; private static char[] numericValues = { '-', '+', ',', '.' }; /// /// Encodes the specified Unicode character in proper Regular Expression format. /// /// Unicode character to encode in Regular Expression format. /// Specified Unicode character in proper Regular Expression format. public static string RegexEncode(this char item) { return "\\u" + Convert.ToUInt16(item).ToString("x").PadLeft(4, '0'); } /// /// Tests a character to determine if it marks the end of a typical english word. /// /// Input character to check. /// true if character is a work seperator. /// /// Preforms no testing for ASCII codes > 127.
/// Does not seperate words based on punctuation of ' % - _
/// However does include the angle bracket symbols < > as seperators
///
/// For reference the standard char tests are: /// ///
public static bool IsWordTerminator(this char value) { if (char.IsPunctuation(value) || char.IsWhiteSpace(value) || char.IsSymbol(value) || char.IsControl(value)) return true; return value.IsAny(wordSeperators); } /// /// Tests a character to determine if is a common part of a numeric string (digits or one of "+ - , .") /// /// The character to check. /// true if numeric character. public static bool IsNumeric(this char value) { if (char.IsDigit(value)) return true; return value.IsAny(numericValues); } /// /// Determines if a character matches any character in a sent array. /// /// The character to check. /// The array of characters to test. /// Boolean value indicating a that the character is in the array. public static bool IsAny(this char value, IEnumerable testChars) { if ((object)testChars == null) throw new ArgumentNullException("testChars"); foreach (char c in testChars) { if (value == c) return true; } return false; } /// /// Tests a character to determine if it is between a specified character range /// /// Input character to process. /// Beginning of range character. /// End of range character. /// true is the character is within the range. public static bool IsInRange(this char value, char startOfRange, char endOfRange) { if (value >= startOfRange && value <= endOfRange) return true; return false; } } }