Turned many of the helper methods into extension methods

git-svn-id: file:///srv/devel/repo-conversion/nusu@209 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2010-11-20 11:53:25 +00:00
parent 483184ee78
commit 22cea75a7a
5 changed files with 15 additions and 76 deletions

View File

@ -185,7 +185,7 @@ namespace Nuclex.Support {
/// <returns>
/// The memory contents of the floating point value interpreted as an integer
/// </returns>
public static int ReinterpretAsInt(float value) {
public static int ReinterpretAsInt(this float value) {
FloatIntUnion union = new FloatIntUnion();
union.Float = value;
return union.Int;
@ -202,7 +202,7 @@ namespace Nuclex.Support {
/// The memory contents of the double precision floating point value
/// interpreted as an integer
/// </returns>
public static long ReinterpretAsLong(double value) {
public static long ReinterpretAsLong(this double value) {
DoubleLongUnion union = new DoubleLongUnion();
union.Double = value;
return union.Long;
@ -215,7 +215,7 @@ namespace Nuclex.Support {
/// <returns>
/// The memory contents of the integer value interpreted as a floating point value
/// </returns>
public static float ReinterpretAsFloat(int value) {
public static float ReinterpretAsFloat(this int value) {
FloatIntUnion union = new FloatIntUnion();
union.Int = value;
return union.Float;
@ -230,7 +230,7 @@ namespace Nuclex.Support {
/// The memory contents of the integer interpreted as a double precision
/// floating point value
/// </returns>
public static double ReinterpretAsDouble(long value) {
public static double ReinterpretAsDouble(this long value) {
DoubleLongUnion union = new DoubleLongUnion();
union.Long = value;
return union.Double;

View File

@ -29,14 +29,14 @@ namespace Nuclex.Support {
/// <summary>Returns the next highest power of 2 from the specified value</summary>
/// <param name="value">Value of which to return the next highest power of 2</param>
/// <returns>The next highest power of 2 to the value</returns>
public static long NextPowerOf2(long value) {
public static long NextPowerOf2(this long value) {
return (long)NextPowerOf2((ulong)value);
}
/// <summary>Returns the next highest power of 2 from the specified value</summary>
/// <param name="value">Value of which to return the next highest power of 2</param>
/// <returns>The next highest power of 2 to the value</returns>
public static ulong NextPowerOf2(ulong value) {
public static ulong NextPowerOf2(this ulong value) {
if(value == 0)
return 1;
@ -55,14 +55,14 @@ namespace Nuclex.Support {
/// <summary>Returns the next highest power of 2 from the specified value</summary>
/// <param name="value">Value of which to return the next highest power of 2</param>
/// <returns>The next highest power of 2 to the value</returns>
public static int NextPowerOf2(int value) {
public static int NextPowerOf2(this int value) {
return (int)NextPowerOf2((uint)value);
}
/// <summary>Returns the next highest power of 2 from the specified value</summary>
/// <param name="value">Value of which to return the next highest power of 2</param>
/// <returns>The next highest power of 2 to the value</returns>
public static uint NextPowerOf2(uint value) {
public static uint NextPowerOf2(this uint value) {
if(value == 0)
return 1;

View File

@ -25,6 +25,12 @@ using System.Text;
namespace Nuclex.Support {
/*
public enum Garbage {
Avoid,
Accept
}
*/
/// <summary>Contains helper methods for the string builder class</summary>
public static class StringBuilderHelper {
@ -35,7 +41,7 @@ namespace Nuclex.Support {
/// <summary>Clears the contents of a string builder</summary>
/// <param name="builder">String builder that will be cleared</param>
public static void Clear(StringBuilder builder) {
public static void Clear(this StringBuilder builder) {
builder.Remove(0, builder.Length);
}

View File

@ -37,11 +37,7 @@ namespace Nuclex.Support {
/// <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);
}
@ -59,11 +55,7 @@ namespace Nuclex.Support {
/// <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);
}
@ -83,11 +75,7 @@ namespace Nuclex.Support {
/// 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;
@ -117,11 +105,7 @@ namespace Nuclex.Support {
/// <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);
}
@ -139,11 +123,7 @@ namespace Nuclex.Support {
/// <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);
}
@ -163,11 +143,7 @@ namespace Nuclex.Support {
/// 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;

View File

@ -145,47 +145,6 @@ namespace Nuclex.Support {
return false;
}
#if USE_XMLDOCUMENT
/// <summary>Loads an XML document from a file</summary>
/// <param name="schema">Schema to use for validating the XML document</param>
/// <param name="documentPath">
/// Path to the file containing the XML document that will be loaded
/// </param>
/// <returns>The loaded XML document</returns>
public static XmlDocument LoadDocument(XmlSchema schema, string documentPath) {
using(FileStream documentStream = openFileForSharedReading(documentPath)) {
return LoadDocument(schema, documentStream);
}
}
/// <summary>Loads an XML document from a stream</summary>
/// <param name="schema">Schema to use for validating the XML document</param>
/// <param name="documentStream">
/// Stream from which the XML document will be read
/// </param>
/// <returns>The loaded XML document</returns>
public static XmlDocument LoadDocument(XmlSchema schema, Stream documentStream) {
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(schema);
using(XmlReader reader = XmlReader.Create(documentStream, settings)) {
XmlDocument document = new XmlDocument();
document.Schemas.Add(schema);
document.Load(reader);
ValidationEventProcessor eventProcessor = new ValidationEventProcessor();
document.Validate(eventProcessor.OnValidationEvent);
if(eventProcessor.OccurredException != null) {
throw eventProcessor.OccurredException;
}
return document;
}
}
#else // !USE_XMLDOCUMENT
/// <summary>Loads an XML document from a file</summary>
/// <param name="schema">Schema to use for validating the XML document</param>
/// <param name="documentPath">
@ -228,8 +187,6 @@ namespace Nuclex.Support {
}
}
#endif // USE_XMLDOCUMENT
/// <summary>Opens a file for shared reading</summary>
/// <param name="path">Path to the file that will be opened</param>
/// <returns>The opened file's stream</returns>