Nuclex.Support/Source/StringHelper.cs
Markus Ewald f304f8c783 All methods in the StringHelper class are now extension methods in .NET 4.0 builds
git-svn-id: file:///srv/devel/repo-conversion/nusu@206 d2e56fa2-650e-0410-a79f-9358c0239efd
2010-10-10 10:47:37 +00:00

193 lines
7.5 KiB
C#

#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2010 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
namespace Nuclex.Support {
/// <summary>Helper routines for working with strings</summary>
public static class StringHelper {
/// <summary>
/// Searches for the first occurence of a character other than the characters
/// listed in the <paramref name="anyNotOf" /> parameter
/// </summary>
/// <param name="haystack">String that will be scanned in</param>
/// <param name="anyNotOf">Characters to not look for in the scanned string</param>
/// <returns>
/// The index of the first occurence of a character not in the
/// <paramref name="anyNotOf" /> array or -1 if all characters in the string were
/// present in the <paramref name="anyNotOf" /> array.
/// </returns>
#if NO_EXTENSION_METHODS
public static int IndexNotOfAny(string haystack, char[] anyNotOf) {
#else
public static int IndexNotOfAny(this string haystack, char[] anyNotOf) {
#endif
return IndexNotOfAny(haystack, anyNotOf, 0, haystack.Length);
}
/// <summary>
/// Searches for the first occurence of a character other than the characters
/// listed in the <paramref name="anyNotOf" /> parameter
/// </summary>
/// <param name="haystack">String that will be scanned in</param>
/// <param name="anyNotOf">Characters to not look for in the scanned string</param>
/// <param name="startIndex">
/// Index of the character in the haystack at which to start scanning
/// </param>
/// <returns>
/// The index of the first occurence of a character not in the
/// <paramref name="anyNotOf" /> array or -1 if all characters in the string were
/// present in the <paramref name="anyNotOf" /> array.
/// </returns>
#if NO_EXTENSION_METHODS
public static int IndexNotOfAny(string haystack, char[] anyNotOf, int startIndex) {
#else
public static int IndexNotOfAny(this string haystack, char[] anyNotOf, int startIndex) {
#endif
return IndexNotOfAny(haystack, anyNotOf, startIndex, haystack.Length - startIndex);
}
/// <summary>
/// Searches for the first occurence of a character other than the characters
/// listed in the <paramref name="anyNotOf" /> parameter
/// </summary>
/// <param name="haystack">String that will be scanned in</param>
/// <param name="anyNotOf">Characters to not look for in the scanned string</param>
/// <param name="startIndex">
/// Index of the character in the haystack at which to start scanning
/// </param>
/// <param name="count">Number of characters in the haystack to scan</param>
/// <returns>
/// The index of the first occurence of a character not in the
/// <paramref name="anyNotOf" /> array or -1 if all characters in the string were
/// present in the <paramref name="anyNotOf" /> array.
/// </returns>
public static int IndexNotOfAny(
#if NO_EXTENSION_METHODS
string haystack, char[] anyNotOf, int startIndex, int count
#else
this string haystack, char[] anyNotOf, int startIndex, int count
#endif
) {
int anyLength = anyNotOf.Length;
count += startIndex;
while(startIndex < count) {
char character = haystack[startIndex];
int index = Array.IndexOf<char>(anyNotOf, character, 0, anyLength);
if(index == -1) {
return startIndex;
}
++startIndex;
}
return -1;
}
/// <summary>
/// Searches backwards for the first occurence of a character other than the
/// characters listed in the <paramref name="anyNotOf" /> parameter
/// </summary>
/// <param name="haystack">String that will be scanned in</param>
/// <param name="anyNotOf">Characters to not look for in the scanned string</param>
/// <returns>
/// The index of the first occurence of a character not in the
/// <paramref name="anyNotOf" /> array or -1 if all characters in the string were
/// present in the <paramref name="anyNotOf" /> array.
/// </returns>
#if NO_EXTENSION_METHODS
public static int LastIndexNotOfAny(string haystack, char[] anyNotOf) {
#else
public static int LastIndexNotOfAny(this string haystack, char[] anyNotOf) {
#endif
return LastIndexNotOfAny(haystack, anyNotOf, haystack.Length - 1, haystack.Length);
}
/// <summary>
/// Searches backwards for the first occurence of a character other than the
/// characters listed in the <paramref name="anyNotOf" /> parameter
/// </summary>
/// <param name="haystack">String that will be scanned in</param>
/// <param name="anyNotOf">Characters to not look for in the scanned string</param>
/// <param name="startIndex">
/// Index of the character in the haystack at which to start scanning
/// </param>
/// <returns>
/// The index of the first occurence of a character not in the
/// <paramref name="anyNotOf" /> array or -1 if all characters in the string were
/// present in the <paramref name="anyNotOf" /> array.
/// </returns>
#if NO_EXTENSION_METHODS
public static int LastIndexNotOfAny(string haystack, char[] anyNotOf, int startIndex) {
#else
public static int LastIndexNotOfAny(this string haystack, char[] anyNotOf, int startIndex) {
#endif
return LastIndexNotOfAny(haystack, anyNotOf, startIndex, startIndex + 1);
}
/// <summary>
/// Searches backwards for the first occurence of a character other than the
/// characters listed in the <paramref name="anyNotOf" /> parameter
/// </summary>
/// <param name="haystack">String that will be scanned in</param>
/// <param name="anyNotOf">Characters to not look for in the scanned string</param>
/// <param name="startIndex">
/// Index of the character in the haystack at which to start scanning
/// </param>
/// <param name="count">Number of characters in the haystack to scan</param>
/// <returns>
/// The index of the first occurence of a character not in the
/// <paramref name="anyNotOf" /> array or -1 if all characters in the string were
/// present in the <paramref name="anyNotOf" /> array.
/// </returns>
public static int LastIndexNotOfAny(
#if NO_EXTENSION_METHODS
string haystack, char[] anyNotOf, int startIndex, int count
#else
this string haystack, char[] anyNotOf, int startIndex, int count
#endif
) {
int anyLength = anyNotOf.Length;
count = startIndex - count;
while(startIndex > count) {
char character = haystack[startIndex];
int index = Array.IndexOf<char>(anyNotOf, character, 0, anyLength);
if(index == -1) {
return startIndex;
}
--startIndex;
}
return -1;
}
}
} // namespace Nuclex.Support