// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
using System;
namespace TheArtOfDev.HtmlRenderer.Core.Utils
{
///
/// Represents sub-string of a full string starting at specific location with a specific length.
///
internal sealed class SubString
{
#region Fields and Consts
///
/// the full string that this sub-string is part of
///
private readonly string _fullString;
///
/// the start index of the sub-string
///
private readonly int _startIdx;
///
/// the length of the sub-string starting at
///
private readonly int _length;
#endregion
///
/// Init sub-string that is the full string.
///
/// the full string that this sub-string is part of
public SubString(string fullString)
{
ArgChecker.AssertArgNotNull(fullString, "fullString");
_fullString = fullString;
_startIdx = 0;
_length = fullString.Length;
}
///
/// Init.
///
/// the full string that this sub-string is part of
/// the start index of the sub-string
/// the length of the sub-string starting at
/// is null
public SubString(string fullString, int startIdx, int length)
{
ArgChecker.AssertArgNotNull(fullString, "fullString");
if (startIdx < 0 || startIdx >= fullString.Length)
throw new ArgumentOutOfRangeException("startIdx", "Must within fullString boundries");
if (length < 0 || startIdx + length > fullString.Length)
throw new ArgumentOutOfRangeException("length", "Must within fullString boundries");
_fullString = fullString;
_startIdx = startIdx;
_length = length;
}
///
/// the full string that this sub-string is part of
///
public string FullString
{
get { return _fullString; }
}
///
/// the start index of the sub-string
///
public int StartIdx
{
get { return _startIdx; }
}
///
/// the length of the sub-string starting at
///
public int Length
{
get { return _length; }
}
///
/// Get string char at specific index.
///
/// the idx to get the char at
/// char at index
public char this[int idx]
{
get
{
if (idx < 0 || idx > _length)
throw new ArgumentOutOfRangeException("idx", "must be within the string range");
return _fullString[_startIdx + idx];
}
}
///
/// Is the sub-string is empty string.
///
/// true - empty string, false - otherwise
public bool IsEmpty()
{
return _length < 1;
}
///
/// Is the sub-string is empty string or contains only whitespaces.
///
/// true - empty or whitespace string, false - otherwise
public bool IsEmptyOrWhitespace()
{
for (int i = 0; i < _length; i++)
{
if (!char.IsWhiteSpace(_fullString, _startIdx + i))
return false;
}
return true;
}
///
/// Is the sub-string contains only whitespaces (at least one).
///
/// true - empty or whitespace string, false - otherwise
public bool IsWhitespace()
{
if (_length < 1)
return false;
for (int i = 0; i < _length; i++)
{
if (!char.IsWhiteSpace(_fullString, _startIdx + i))
return false;
}
return true;
}
///
/// Get a string of the sub-string.
/// This will create a new string object!
///
/// new string that is the sub-string represented by this instance
public string CutSubstring()
{
return _length > 0 ? _fullString.Substring(_startIdx, _length) : string.Empty;
}
///
/// Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.
///
/// The zero-based starting character position of a substring in this instance.
/// The number of characters in the substring.
/// A String equivalent to the substring of length length that begins at startIndex in this instance, or
/// Empty if startIndex is equal to the length of this instance and length is zero.
public string Substring(int startIdx, int length)
{
if (startIdx < 0 || startIdx > _length)
throw new ArgumentOutOfRangeException("startIdx");
if (length > _length)
throw new ArgumentOutOfRangeException("length");
if (startIdx + length > _length)
throw new ArgumentOutOfRangeException("length");
return _fullString.Substring(_startIdx + startIdx, length);
}
public override string ToString()
{
return string.Format("Sub-string: {0}", _length > 0 ? _fullString.Substring(_startIdx, _length) : string.Empty);
}
}
}