All pastes #70901 Raw Edit

tobz

public text v1 · immutable
#70901 ·published 2006-06-24 23:40 UTC
rendered paste body
/*
 *
 * Erevlydeux.Utilities - common and misc. utilities
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program 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
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
using System;
using System.Collections;

namespace Erevlydeux.Utilities
{
	/// <summary>
	/// This class deals with string arrays, ArrayLists of just strings, or just regular ArrayLists. 
	/// It was born out of a need to deal with string arrays, and due to their ease of use, ArrayLists. 
	/// Some things are new, some things are neat wrappers for the ugly .NET equivalent, and some things 
	/// build on their .NET counterpart.
	/// </summary>
	public sealed class ArrayUtils
	{
		/// <summary>
		/// Convert an ArrayList to a string array
		/// </summary>
		/// <param name="inputArray">The ArrayList to convert</param>
		/// <returns>A string array of the ArrayList</returns>
		[CLSCompliant(true)]
		public static string[] ToStringArray(ArrayList inputArray)
		{
			return (string[])inputArray.ToArray(typeof(string));
		}

		/// <summary>
		/// Convert a string array to an ArrayList
		/// </summary>
		/// <param name="inputArray">The string array to convert</param>
		/// <returns>An ArrayList of the string array</returns>
		[CLSCompliant(true)]
		public static ArrayList ToArrayList(string[] inputArray)
		{
			ArrayList tempArray = new ArrayList();

			foreach (string tempSz in inputArray)
			{
				tempArray.Add(tempSz);
			}

			return tempArray;
		}

		/// <summary>
		/// Combines the strings of a string array into one delimited string
		/// </summary>
		/// <param name="inputArray">The string array to combine</param>
		/// <param name="delimiter">The delimited</param>
		/// <returns>A string of the delimited strings</returns>
		[CLSCompliant(true)]
		public static string ToDelimitedString(string[] inputArray, string delimiter)
		{
			string returnSz = "";

			if (inputArray.Length > 1) returnSz = String.Join(delimiter, inputArray);
			else returnSz = inputArray[0];

			return returnSz;
		}

		/// <summary>
		/// Combines the strings of an ArrayList into one delimited string
		/// </summary>
		/// <param name="inputArray">The ArrayList to combine</param>
		/// <param name="delimiter">The delimited</param>
		/// <returns>A string of the delimited strings</returns>
		[CLSCompliant(true)]
		public static string ToDelimitedString(ArrayList inputArray, string delimiter)
		{
			string returnSz = "";

			if (inputArray.Count > 1) returnSz = String.Join(delimiter, ToStringArray(inputArray));
			else returnSz = (string)inputArray[0];

			return returnSz;
		}

		/// <summary>
		/// Combines the strings of a string array into a string, which resemble a list
		/// </summary>
		/// <param name="szArray">The string array to combine</param>
		/// <returns>A string which resembles a list, using commas, and follows the rules of English grammar</returns>
		[CLSCompliant(true)]
		public static string GetReadableList(string[] szArray)
		{
			if (szArray.Length == 0) return "none";
			if (szArray.Length == 1) return szArray[0];

			string list = String.Join(";", szArray);

			list = list.Replace(";", ", ");

			int lastSplitPos = list.LastIndexOf(", ");

			list = list.Insert(lastSplitPos + 2, "and ");

			return list;
		}

		/// <summary>
		/// Combines the strings of an ArrayList into a string, which resemble a list
		/// </summary>
		/// <param name="inputArray">The ArrayList to combine</param>
		/// <returns>A string which resembles a list, using commas, and follows the rules of English grammar</returns>
		[CLSCompliant(true)]
		public static string GetReadableList(ArrayList inputArray)
		{
			if (inputArray.Count == 0) return "none";
			if (inputArray.Count == 1) return (string)inputArray[0];

			string[] szArray = (string[])inputArray.ToArray(typeof(string));

			string list = String.Join(";", szArray);

			list = list.Replace(";", ", ");

			int lastSplitPos = list.LastIndexOf(", ");

			list = list.Insert(lastSplitPos + 2, "and ");

			return list;
		}

		/// <summary>
		/// Removes all the entries from one ArrayList in another ArrayList
		/// </summary>
		/// <param name="arrayToRemove">The ArrayList to remove from</param>
		/// <param name="arrayToRemoveFrom">The ArrayList to remove</param>
		/// <returns>An ArrayList that does not contain the elements of arrayToRemoveFrom</returns>
		[CLSCompliant(true)]
		public static ArrayList RemoveEntries(ArrayList arrayToRemove, ArrayList arrayToRemoveFrom)
		{
			ArrayList returnArray = arrayToRemoveFrom;

			foreach (object entry in arrayToRemove)
			{
				if (returnArray.Contains(entry))
				{
					returnArray.Remove(entry);
				}
			}

			return returnArray;
		}

		/// <summary>
		/// Applies a string to existing strings in an ArrayList, at a certain position in the string
		/// </summary>
		/// <param name="inputArray">The ArrayList to apply the string to</param>
		/// <param name="position">The position to apply the string, if -1 the string is applied at the end</param>
		/// <param name="toAdd">The string to pply</param>
		/// <returns>An ArrayList with the new strings</returns>
		[CLSCompliant(true)]
		public static void AppendToElements(ArrayList inputArray, int position, string appendage)
		{
			string tempString = "";
			ArrayList returnList = new ArrayList();

			foreach (string element in inputArray)
			{
				tempString = (position == -1 ? element.Insert(element.Length, appendage) : element.Insert(position, appendage));
				returnList.Add(tempString);
			}

			inputArray = returnList;
		}

		/// <summary>
		/// Applies a string to existing strings in an ArrayList, at a certain position in the string
		/// </summary>
		/// <param name="szArray">The string array to apply the string to</param>
		/// <param name="position">The position to apply the string, if -1 the string is applied at the end</param>
		/// <param name="toAdd">The string to pply</param>
		/// <returns>An ArrayList with the new strings</returns>
		[CLSCompliant(true)]
		public static string[] AppendToElements(string[] szArray, int position, string appendage)
		{
			int position = -1;
			string tempString = "";
			string[] returnList = new string[szArray.Length];

			foreach (string element in szArray)
			{
				tempString = (position == -1 ? element.Insert(element.Length, appendage) : element.Insert(position, appendage));
				returnList[++i] = tempString;
			}

			return returnList;
		}

		/// <summary>
		/// Safely splits a string without erroring if the delimiter is not present
		/// </summary>
		/// <param name="inputString">The string to split</param>
		/// <param name="delimiter">The character to split on</param>
		/// <returns>A string array of the split string</returns>
		[CLSCompliant(true)]
		public static string[] Split(string inputSz, char delimiter)
		{
			if (String.IsNullOrEmpty(inputSz)) return new string[] { };

			if (inputSz.IndexOf(delimiter) == -1) return new string[] { inputSz };

			return inputSz.Split(delimiter);
		}

		/// <summary>
		/// Pops an item off the end of a string array
		/// </summary>
		/// <param name="inputArray">The array to push the item from</param>
		/// <returns>The new string array, without the previous last item</returns>
		[CLSCompliant(true)]
		public static string[] Pop(string[] szArray)
		{
			string[] newSzArray = new string[szArray.Length - 1];

			for (int position = 0; position < szArray.Length - 1; position++)
			{
				newSzArray[position] = szArray[position];
			}

			return newSzArray;
		}

		/// <summary>
		/// Pops an item off the end of an ArrayList
		/// </summary>
		/// <param name="inputArray">The array to push the item from</param>
		/// <returns>The new ArrayList, without the previous last item</returns>
		[CLSCompliant(true)]
		public static void Pop(ArrayList inputArray)
		{
			ArrayList newArray = new ArrayList();

			if (inputArray.Count > 0)
			{
				for (int position = 0; position < inputArray.Count - 1; position++)
				{
					newArray.Add(inputArray[position]);
				}
			}

			inputArray = newArray;
		}

		/// <summary>
		/// Pops an item off the front of a string array
		/// </summary>
		/// <param name="inputArray">The array to push the item from</param>
		/// <returns>The new string array, without the previous first item</returns>
		[CLSCompliant(true)]
		public static string[] ReversePop(string[] inputArray)
		{
			string[] newArray = new string[inputArray.Length - 1];

			for (int position = 0; position < inputArray.Length - 1; position++)
			{
				newArray[position] = inputArray[position + 1];
			}

			return newArray;
		}

		/// <summary>
		/// Pops an item off the front of an ArrayList
		/// </summary>
		/// <param name="inputArray">The array to push the item from</param>
		/// <returns>The new ArrayList, without the previous first item</returns>
		[CLSCompliant(true)]
		public static void ReversePop(ArrayList inputArray)
		{
			ArrayList newArray = new ArrayList();

			if (inputArray.Count > 0)
			{
				for (int position = 0; position < inputArray.Count - 1; position++)
				{
					newArray.Add(inputArray[position + 1]);
				}
			}

			inputArray = newArray;
		}

		/// <summary>
		/// Pops an item off the end of a string array
		/// </summary>
		[CLSCompliant(true)]
		public static string[] Push(string[] szArray, string inputSz)
		{
			string[] newSzArray = new string[szArray.Length - 1];

			for (int position = 0; position < szArray.Length - 1; position++)
			{
				newSzArray[position] = szArray[position];
			}

			return newSzArray;
		}

		/// <summary>
		/// Pushes a string onto the end of an ArrayList
		/// </summary>
		[CLSCompliant(true)]
		public static void Push(ArrayList inputArray, string inputSz)
		{
			inputArray.Insert(inputArray.Count, inputSz);
		}

		/// <summary>
		/// Pushes an object onto the end of an ArrayList
		/// </summary>
		[CLSCompliant(true)]
		public static void Push(ArrayList inputArray, object inputObj)
		{
			inputArray.Insert(inputArray.Count, inputObj);
		}

		/// <summary>
		/// Pushes a string onto the front of a string array
		/// </summary>
		[CLSCompliant(true)]
		public static string[] ReversePush(string[] inputArray, string inputSz)
		{
			string[] newArray = new string[inputArray.Length - 1];

			for (int position = 0; position < inputArray.Length - 1; position++)
			{
				newArray[position] = inputArray[position + 1];
			}

			return newArray;
		}

		/// <summary>
		/// Pushes a string onto the front of an ArrayList
		/// </summary>
		[CLSCompliant(true)]
		public static void ReversePush(ArrayList inputArray, string inputSz)
		{
			ArrayList newArray = new ArrayList();

			if (inputArray.Count > 0)
			{
				newArray.Add(inputSz);

				newArray.AddRange(inputArray);
			}

			inputArray = newArray;
		}

		/// <summary>
		/// Pushes an item onto the front of an ArrayList
		/// </summary>
		[CLSCompliant(true)]
		public static void ReversePush(ArrayList inputArray, object inputObj)
		{
			ArrayList newArray = new ArrayList();

			if (inputArray.Count > 0)
			{
				newArray.Add(inputObj);

				newArray.AddRange(inputArray);
			}

			inputArray = newArray;
		}
	}
}