using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Threading;
namespace Sleis.Utility
{
///
/// Basic helper functions for dealing with strings.
///
public static class StringUtility {
// Encoder that does not output byte marker (BOM) at start of encoding
public static readonly UTF8Encoding UTF8 = new UTF8Encoding(false, true);
public static bool NoiseTolerantCompare(string val1, string val2)
{
//Nulls or empty not the same
if (String.IsNullOrEmpty(val1) ||
String.IsNullOrEmpty(val2))
{
return false;
}
return String.Compare(val1.Trim(), val2.Trim(),
StringComparison.InvariantCultureIgnoreCase)
.Equals(0);
}
public static IList ToSearchTokens(string text)
{
IList list = new List();
if (String.IsNullOrEmpty(text))
{
return list;
}else{
foreach (string part in text.Split(' '))
{
var cleanPart = ToSearchToken(part);
if (!list.Contains(cleanPart))
{
list.Add(cleanPart);
}
}
}
return list;
}
public static string ToSearchToken(string text)
{
if (String.IsNullOrEmpty(text))
{
return null;
}
StringBuilder sb = new StringBuilder();
foreach (char c in text.Trim().ToLower().ToCharArray())
{
if (Char.IsLetterOrDigit(c))
{
sb.Append(c);
}
}
return sb.ToString();
}
///
/// Join the input string values using the separator and return the resulting string.
///
public static string Join(string separator, IEnumerable values) {
if ( values == null ) {
return string.Empty;
}
StringBuilder sb = new StringBuilder();
bool addedFirst = false;
foreach(T value in values) {
if ( addedFirst ) {
sb.Append(separator);
}
if (value != null)
{
sb.Append(value.ToString());
}
else
{
sb.Append(string.Empty);
}
addedFirst = true;
}
return sb.ToString();
}
///
/// Return the character index of findChar within text, starting from the end of text
/// and working backwards until the n-th occurance is found (specified by 1-based lastNthCount).
///
public static int LastNthIndexOf(string text, char findChar, int lastNthCount)
{
if (lastNthCount <= 0)
{
throw new ArgumentException("lastNthCount must be greater than 0");
}
int index = text.Length - 1;
while ( index > 0 ) {
index = text.LastIndexOf(findChar, index - 1);
if (index >= 0)
{
--lastNthCount;
if (lastNthCount == 0)
{
return index;
}
}
}
return -1;
}
///
/// Return the character index of findChar within text, starting from the beginning of text
/// and working forwards until the n-th occurance is found (specified by 1-based nthCount).
///
public static int NthIndexOf(string text, char findChar, int nthCount)
{
if (nthCount <= 0)
{
throw new ArgumentException("nthCount must be greater than 0");
}
int index = -1;
while (index < text.Length - 1)
{
index = text.IndexOf(findChar, index + 1);
if (index >= 0)
{
--nthCount;
if (nthCount == 0)
{
return index;
}
}
}
return -1;
}
public static bool ContainsLowercaseChars(string text)
{
if (!string.IsNullOrEmpty(text))
{
foreach (char chr in text)
{
if (char.IsLower(chr))
{
return true;
}
}
}
return false;
}
public static bool IsWord(string text, int offset, int length)
{
return IsWord(text, offset, length, char.IsLetterOrDigit);
}
public static bool Contains(string sourceString, string checkSubString,
StringComparison comparison)
{
if (string.IsNullOrEmpty(sourceString) || string.IsNullOrEmpty(checkSubString))
{
return false;
}
return (sourceString.IndexOf(checkSubString, comparison) >= 0);
}
public static bool Contains(string checkString, IEnumerable strings,
StringComparison comparison)
{
return (IndexOf(checkString, strings, comparison) >= 0);
}
public static int LastIndexOf(string checkString, IEnumerable strings,
int startIndex, int count, StringComparison comparison)
{
foreach (string str in strings)
{
int index = checkString.LastIndexOf(str, startIndex, count, comparison);
if (index >= 0)
{
return index;
}
}
return -1;
}
public static int IndexOf(string checkString, IEnumerable strings,
StringComparison comparison)
{
if (strings != null)
{
int i = 0;
IEnumerable genericCollection = strings as IEnumerable;
if (genericCollection != null)
{
foreach (string str in genericCollection)
{
if (string.Equals(checkString, str, comparison))
{
return i;
}
++i;
}
}
else
{
foreach (object obj in strings)
{
if (string.Equals(checkString, obj.ToString(), comparison))
{
return i;
}
++i;
}
}
}
return -1;
}
public static string LastSubstringDelimitedBy(string text, string delimeter)
{
if (!string.IsNullOrEmpty(text))
{
int index = text.LastIndexOf(delimeter);
if (index >= 0)
{
if (index == (text.Length - 1))
{
return string.Empty;
}
else
{
return text.Substring(index + 1);
}
}
}
return text;
}
public delegate bool IsWordCharacter(char c);
public static bool IsWord(string text, int offset, int length, IsWordCharacter isChar)
{
if (string.IsNullOrEmpty(text) || (length <= 0) || (offset < 0) || (offset >= text.Length))
{
return false;
}
if (offset > 0)
{
if ((offset + length) < text.Length)
{
return (!isChar(text[offset - 1]) && !char.IsLetterOrDigit(text[offset + length]));
}
else
{
return !isChar(text[offset - 1]);
}
}
else if ((offset + length) < text.Length)
{
return !isChar(text[offset + length]);
}
else
{
return true;
}
}
public static string ReplaceAllChars(string text, string replaceString)
{
if (string.IsNullOrEmpty(text))
{
return string.Empty;
}
if ( string.IsNullOrEmpty(replaceString))
{
throw new ArgumentException("replaceString cannot be empty");
}
StringBuilder sb = new StringBuilder(text.Length * replaceString.Length);
foreach (char chr in text)
{
sb.Append(replaceString);
}
return sb.ToString();
}
public static string SplitCamelCaseName(string camelCaseName, char separator)
{
ExceptionUtility.ThrowIfEmptyString(camelCaseName, "camelCaseName");
StringBuilder sb = new StringBuilder((camelCaseName.Length * 3) / 2);
sb.Append(camelCaseName[0]);
bool lastCharWasLower = false;
bool lastCharWasNumber = false;
bool twoOrMoreUpperChars = false;
for (int i = 1; i < camelCaseName.Length; ++i)
{
char curChar = camelCaseName[i];
if (char.IsLetter(curChar))
{
lastCharWasNumber = false;
if (char.IsUpper(curChar))
{
if (lastCharWasLower)
{
if (sb[sb.Length - 1] != separator)
{
sb.Append(separator);
}
twoOrMoreUpperChars = false;
}
else
{
twoOrMoreUpperChars = true;
}
sb.Append(curChar);
lastCharWasLower = false;
}
else
{
if (twoOrMoreUpperChars)
{
sb.Insert(sb.Length - 1, separator);
twoOrMoreUpperChars = false;
}
sb.Append(curChar);
lastCharWasLower = true;
}
}
else if (char.IsDigit(curChar))
{
if (!lastCharWasNumber)
{
if (sb[sb.Length - 1] != separator)
{
sb.Append(separator);
}
}
sb.Append(curChar);
lastCharWasNumber = true;
lastCharWasLower = true;
twoOrMoreUpperChars = false;
}
else
{
sb.Append(curChar);
lastCharWasLower = true;
lastCharWasNumber = false;
twoOrMoreUpperChars = false;
}
}
return sb.ToString();
}
public static string GetStringOrNull(object value)
{
if (value == null)
{
return null;
}
string rtnVal = value.ToString();
return string.IsNullOrEmpty(rtnVal) ? null : rtnVal;
}
public static string GetStringValue(string value, string defaultVal)
{
if (String.IsNullOrEmpty(value) || String.IsNullOrEmpty(value.Trim()))
{
return defaultVal;
}
else
{
return value.Trim();
}
}
public static bool IsNullOrEmpty(object value)
{
if (value == null)
{
return true;
}
string valueStr = value.ToString();
return string.IsNullOrEmpty(valueStr);
}
public static string BreakUpText(string text, int numBreakChars, string breakString)
{
if (string.IsNullOrEmpty(text))
{
return text;
}
StringBuilder sb = null;
int currentNonWhitepaceCount = 0, currentCharIndex = 0;
foreach (char currentChar in text)
{
if (char.IsWhiteSpace(currentChar) || (currentChar == '-'))
{
currentNonWhitepaceCount = 0;
}
else {
if (++currentNonWhitepaceCount > numBreakChars)
{
if (sb == null)
{
sb = new StringBuilder(text);
}
sb.Insert(currentCharIndex, breakString);
currentCharIndex += breakString.Length;
currentNonWhitepaceCount = 1;
}
}
++currentCharIndex;
}
return (sb == null) ? text : sb.ToString();
}
[System.Runtime.InteropServices.DllImport("rpcrt4.dll", SetLastError = true)]
static extern int UuidCreateSequential(out Guid guid);
public static string CreateSequentialGuid()
{
const int RPC_S_OK = 0;
Guid guid;
int hr = UuidCreateSequential(out guid);
if (hr != RPC_S_OK)
{
throw new ApplicationException("UuidCreateSequential failed: " + hr);
}
return guid.ToString();
}
private static System.Security.Cryptography.SHA256 s_Hasher = new System.Security.Cryptography.SHA256Managed();
public static Int64 HashCode64(string text)
{
Int64 hashCode = 0;
if (!string.IsNullOrEmpty(text))
{
//Unicode Encode Covering all characterset
byte[] byteContents = Encoding.UTF8.GetBytes(text);
byte[] hashText = s_Hasher.ComputeHash(byteContents);
Int64 h1 = BitConverter.ToInt64(hashText, 0);
Int64 h2 = BitConverter.ToInt64(hashText, 8);
Int64 h3 = BitConverter.ToInt64(hashText, 16);
Int64 h4 = BitConverter.ToInt64(hashText, 24);
hashCode = h1 ^ h2 ^ h3 ^ h4;
}
return (hashCode);
}
}
}