128 lines
6.2 KiB
C#
128 lines
6.2 KiB
C#
/*
|
|
* VirtualFS - Virtual File System library.
|
|
* Copyright (c) 2013, Grzegorz Russek (grzegorz.russek@gmail.com)
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* Redistributions of source code must retain the above copyright notice,
|
|
* this list of conditions and the following disclaimer.
|
|
*
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
* and/or other materials provided with the distribution.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
using System;
|
|
|
|
namespace VirtualFS.Extensions
|
|
{
|
|
internal static class Validators
|
|
{
|
|
/// <summary>
|
|
/// Returns a new validated string using the rules given.
|
|
/// </summary>
|
|
/// <param name="source">The source string.</param>
|
|
/// <param name="desc">A description of the source string to build errors and exceptions if needed.</param>
|
|
/// <param name="canBeNull">True if the returned string can be null.</param>
|
|
/// <param name="canBeEmpty">True if the returned string can be empty.</param>
|
|
/// <param name="trim">True to trim the returned string.</param>
|
|
/// <param name="trimStart">True to left-trim the returned string.</param>
|
|
/// <param name="trimEnd">True to right-trim the returned string.</param>
|
|
/// <param name="minLen">If >= 0, the min valid length for the returned string.</param>
|
|
/// <param name="maxLen">If >= 0, the max valid length for the returned string.</param>
|
|
/// <param name="padLeft">If not '\0', the character to use to left-pad the returned string if needed.</param>
|
|
/// <param name="padRight">If not '\0', the character to use to right-pad the returned string if needed.</param>
|
|
/// <param name="invalidChars">If not null, an array containing invalid chars that must not appear in the returned
|
|
/// string.</param>
|
|
/// <param name="validChars">If not null, an array containing the only characters that are considered valid for the
|
|
/// returned string.</param>
|
|
/// <param name="invalidSeq">If not null, an array containing the sequences that are not allowed.</param>
|
|
/// <returns>A new validated string.</returns>
|
|
public static string Validated(this string source, string desc = null,
|
|
bool canBeNull = false, bool canBeEmpty = false,
|
|
bool trim = true, bool trimStart = false, bool trimEnd = false,
|
|
int minLen = -1, int maxLen = -1, char padLeft = '\0', char padRight = '\0',
|
|
char[] invalidChars = null, char[] validChars = null, string[] invalidSeq = null)
|
|
{
|
|
// Assuring a valid descriptor...
|
|
if (string.IsNullOrWhiteSpace(desc)) desc = "Source";
|
|
|
|
// Validating if null sources are accepted...
|
|
if (source == null)
|
|
{
|
|
if (!canBeNull) throw new ArgumentNullException(desc, string.Format("{0} cannot be null.", desc));
|
|
return null;
|
|
}
|
|
|
|
// Trimming if needed...
|
|
if (trim && !(trimStart || trimEnd)) source = source.Trim();
|
|
else
|
|
{
|
|
if (trimStart) source = source.TrimStart(' ');
|
|
if (trimEnd) source = source.TrimEnd(' ');
|
|
}
|
|
|
|
// Adjusting lenght...
|
|
if (minLen > 0)
|
|
{
|
|
if (padLeft != '\0') source = source.PadLeft(minLen, padLeft);
|
|
if (padRight != '\0') source = source.PadRight(minLen, padRight);
|
|
}
|
|
|
|
if (maxLen > 0)
|
|
{
|
|
if (padLeft != '\0') source = source.PadLeft(maxLen, padLeft);
|
|
if (padRight != '\0') source = source.PadRight(maxLen, padRight);
|
|
}
|
|
|
|
// Validating emptyness and lenghts...
|
|
if (source.Length == 0)
|
|
{
|
|
if (!canBeEmpty) throw new ArgumentException(string.Format("{0} cannot be empty.", desc));
|
|
return string.Empty;
|
|
}
|
|
|
|
if (minLen >= 0 && source.Length < minLen) throw new ArgumentException(string.Format("Lenght of {0} '{1}' is lower than '{2}'.", desc, source, minLen));
|
|
if (maxLen >= 0 && source.Length > maxLen) throw new ArgumentException(string.Format("Lenght of {0} '{1}' is bigger than '{2}'.", desc, source, maxLen));
|
|
|
|
// Checking invalid chars...
|
|
if (invalidChars != null)
|
|
{
|
|
int n = source.IndexOfAny(invalidChars);
|
|
if (n >= 0) throw new ArgumentException(string.Format("Invalid character '{0}' found in {1} '{2}'.", source[n], desc, source));
|
|
}
|
|
|
|
// Checking valid chars...
|
|
if (validChars != null)
|
|
{
|
|
int n = validChars.ToString().IndexOfAny(source.ToCharArray());
|
|
if (n >= 0)
|
|
throw new ArgumentException(string.Format("Invalid character '{0}' found in {1} '{2}'.", validChars.ToString()[n], desc, source));
|
|
}
|
|
|
|
// Checking invalid sequences
|
|
if (invalidSeq != null)
|
|
{
|
|
foreach (var seq in invalidSeq)
|
|
if (source.IndexOf(seq) >= 0)
|
|
throw new ArgumentException(string.Format("Invalid sequence '{0}' found in {1} '{2}'.", seq, desc, source));
|
|
}
|
|
|
|
return source;
|
|
}
|
|
}
|
|
} |