|
|
#region Copyright © 2009 Jose Antonio De Santiago-Castillo.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Text;
|
|
|
|
|
|
namespace DotNumerics.FortranLibrary
|
|
|
{
|
|
|
public class Characters
|
|
|
{
|
|
|
#region Borrar
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region Fields
|
|
|
|
|
|
private char[] _CharArray = new char[0];
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region Constructor
|
|
|
|
|
|
public Characters(int length)
|
|
|
{
|
|
|
if (length < 0) length = 0;
|
|
|
this._CharArray = new char[length];
|
|
|
|
|
|
for (int i = 0; i < this._CharArray.Length; i++)
|
|
|
{
|
|
|
this._CharArray[i] = ' ';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public Characters(char[] sourceChars, bool copy)
|
|
|
{
|
|
|
if (sourceChars == null)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
if (copy == true)
|
|
|
{
|
|
|
this._CharArray = new char[sourceChars.Length];
|
|
|
Array.Copy(sourceChars, this._CharArray, this._CharArray.Length);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
this._CharArray = sourceChars;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public Characters(string s) : this(s.ToCharArray(), false) { }
|
|
|
|
|
|
public Characters(string s, int length)
|
|
|
: this(length)
|
|
|
{
|
|
|
this.Copy(s);
|
|
|
}
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region Properties
|
|
|
|
|
|
public char[] CharArray
|
|
|
{
|
|
|
get { return this._CharArray; }
|
|
|
}
|
|
|
|
|
|
public int Length
|
|
|
{
|
|
|
get { return this._CharArray.Length; }
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
#region Methods
|
|
|
|
|
|
|
|
|
public void Copy(int startIndex, int lastIndex, char[] sourceCharArray)
|
|
|
{
|
|
|
if (lastIndex > this.Length) lastIndex = this.Length;
|
|
|
int length = lastIndex - startIndex + 1;
|
|
|
length = Math.Min(length, sourceCharArray.Length);
|
|
|
|
|
|
startIndex--;
|
|
|
|
|
|
Array.Copy(sourceCharArray, 0, this._CharArray, startIndex, length);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
public void Copy(int startIndex, char[] sourceCharArray)
|
|
|
{
|
|
|
this.Copy(startIndex, this.Length, sourceCharArray);
|
|
|
}
|
|
|
|
|
|
public void Copy(char[] sourceCharArray)
|
|
|
{
|
|
|
this.Copy(1, sourceCharArray);
|
|
|
}
|
|
|
|
|
|
public void Copy(int startIndex, int lastIndex, string sourceString)
|
|
|
{
|
|
|
this.Copy(startIndex, lastIndex, sourceString.ToCharArray());
|
|
|
}
|
|
|
|
|
|
public void Copy(int startIndex, string sourceString)
|
|
|
{
|
|
|
this.Copy(startIndex, sourceString.ToCharArray());
|
|
|
}
|
|
|
|
|
|
public void Copy(string sourceString)
|
|
|
{
|
|
|
this.Copy(sourceString.ToCharArray());
|
|
|
}
|
|
|
|
|
|
|
|
|
public void Replace(char[] source)
|
|
|
{
|
|
|
this.ToBlanks();
|
|
|
this.Copy(source);
|
|
|
}
|
|
|
|
|
|
public void Replace(Characters source)
|
|
|
{
|
|
|
this.Replace(source.CharArray);
|
|
|
}
|
|
|
|
|
|
public void Replace(string source)
|
|
|
{
|
|
|
this.Replace(source.ToCharArray());
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Characters Substring(int startIndex, int lastIndex)
|
|
|
{
|
|
|
int length = lastIndex - startIndex + 1;
|
|
|
Characters sub = new Characters(length);
|
|
|
|
|
|
startIndex--;
|
|
|
Array.Copy(this._CharArray, startIndex, sub.CharArray, 0, length);
|
|
|
|
|
|
return sub;
|
|
|
|
|
|
}
|
|
|
|
|
|
public Characters Substring(int startIndex)
|
|
|
{
|
|
|
return Substring(startIndex, this.Length);
|
|
|
}
|
|
|
|
|
|
|
|
|
public void ToUpper()
|
|
|
{
|
|
|
for (int i = 0; i < this._CharArray.Length; i++)
|
|
|
{
|
|
|
this._CharArray[i] = Char.ToUpper(this._CharArray[i]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void ToLower()
|
|
|
{
|
|
|
for (int i = 0; i < this._CharArray.Length; i++)
|
|
|
{
|
|
|
this._CharArray[i] = Char.ToLower(this._CharArray[i]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void ToBlanks()
|
|
|
{
|
|
|
for (int i = 0; i < this._CharArray.Length; i++)
|
|
|
{
|
|
|
this._CharArray[i] = ' ';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void ToBlanks(int start, int last)
|
|
|
{
|
|
|
start--;
|
|
|
int max = Math.Min(last, this._CharArray.Length);
|
|
|
for (int i = start; i < max; i++)
|
|
|
{
|
|
|
this._CharArray[i] = ' ';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void ToBlanks(int length)
|
|
|
{
|
|
|
this.ToBlanks(1, length + 1);
|
|
|
}
|
|
|
|
|
|
public int ToInt32()
|
|
|
{
|
|
|
if (this.LenTrim() == 0) return 0;
|
|
|
string s = new string(this._CharArray);
|
|
|
int val = Convert.ToInt32(s);
|
|
|
return val;
|
|
|
}
|
|
|
|
|
|
public string Trim()
|
|
|
{
|
|
|
string s = new string(this._CharArray);
|
|
|
return s.TrimEnd(new char[] { ' ' });
|
|
|
}
|
|
|
|
|
|
public void AdjustLeft()
|
|
|
{
|
|
|
int numLeftBlanks = 0;
|
|
|
for (int i = 0; i < this._CharArray.Length; i++)
|
|
|
{
|
|
|
if (this._CharArray[i] == ' ')
|
|
|
{
|
|
|
numLeftBlanks++;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (numLeftBlanks == 0) return;
|
|
|
else
|
|
|
{
|
|
|
int lastIndex = this._CharArray.Length - numLeftBlanks;
|
|
|
for (int i = 0; i < this._CharArray.Length; i++)
|
|
|
{
|
|
|
if (i < lastIndex)
|
|
|
{
|
|
|
this._CharArray[i] = this._CharArray[numLeftBlanks + i];
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
this._CharArray[i] = ' ';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void AdjustRight()
|
|
|
{
|
|
|
Array.Reverse(this._CharArray);
|
|
|
this.AdjustLeft();
|
|
|
Array.Reverse(this._CharArray);
|
|
|
|
|
|
}
|
|
|
|
|
|
public int LenTrim()
|
|
|
{
|
|
|
int lentrim = this._CharArray.Length;
|
|
|
char blank = ' ';
|
|
|
|
|
|
for (int i = this._CharArray.Length - 1; i > -1; i--)
|
|
|
{
|
|
|
if (this._CharArray[i] == blank)
|
|
|
{
|
|
|
lentrim--;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
return lentrim;
|
|
|
}
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
#region Operators
|
|
|
|
|
|
#region operator +
|
|
|
|
|
|
public static Characters operator +(Characters c1, Characters c2)
|
|
|
{
|
|
|
return Characters.Add(c1.CharArray, c2.CharArray);
|
|
|
}
|
|
|
|
|
|
public static Characters operator +(string s, Characters c2)
|
|
|
{
|
|
|
return Characters.Add(s.ToCharArray(), c2.CharArray);
|
|
|
}
|
|
|
|
|
|
public static Characters operator +(Characters c1, string s)
|
|
|
{
|
|
|
return Characters.Add(c1.CharArray, s.ToCharArray());
|
|
|
}
|
|
|
|
|
|
public static Characters Add(char[] c1, char[] c2)
|
|
|
{
|
|
|
Characters newCharacters = new Characters(c1.Length + c2.Length);
|
|
|
|
|
|
Array.Copy(c1, newCharacters.CharArray, c1.Length);
|
|
|
|
|
|
Array.Copy(c2, 0, newCharacters.CharArray, c1.Length, c2.Length);
|
|
|
|
|
|
return newCharacters;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region Operators == and !=
|
|
|
|
|
|
public static bool operator ==(Characters c1, Characters c2)
|
|
|
{
|
|
|
return Characters.AreEqual(c1.CharArray, c2.CharArray);
|
|
|
}
|
|
|
|
|
|
public static bool operator ==(string s, Characters c2)
|
|
|
{
|
|
|
return Characters.AreEqual(s.ToCharArray(), c2.CharArray);
|
|
|
}
|
|
|
|
|
|
public static bool operator ==(Characters c1, string s)
|
|
|
{
|
|
|
return Characters.AreEqual(c1.CharArray, s.ToCharArray());
|
|
|
}
|
|
|
|
|
|
|
|
|
public static bool operator !=(Characters c1, Characters c2)
|
|
|
{
|
|
|
return !Characters.AreEqual(c1.CharArray, c2.CharArray);
|
|
|
}
|
|
|
|
|
|
public static bool operator !=(string s, Characters c2)
|
|
|
{
|
|
|
return !Characters.AreEqual(s.ToCharArray(), c2.CharArray);
|
|
|
}
|
|
|
|
|
|
public static bool operator !=(Characters c1, string s)
|
|
|
{
|
|
|
return !Characters.AreEqual(c1.CharArray, s.ToCharArray());
|
|
|
}
|
|
|
|
|
|
|
|
|
public static bool AreEqual(char[] left, char[] right)
|
|
|
{
|
|
|
|
|
|
int maxLength = Math.Max(left.Length, right.Length);
|
|
|
char[] c1 = Characters.GetCharArray(left, maxLength);
|
|
|
char[] c2 = Characters.GetCharArray(right, maxLength);
|
|
|
|
|
|
if (c1.Length != c2.Length) return false;
|
|
|
|
|
|
bool areEqual = true;
|
|
|
|
|
|
for (int i = 0; i < c1.Length; i++)
|
|
|
{
|
|
|
if (c1[i] != c2[i])
|
|
|
{
|
|
|
areEqual = false;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
return areEqual;
|
|
|
}
|
|
|
|
|
|
|
|
|
public override bool Equals(System.Object obj)
|
|
|
{
|
|
|
|
|
|
if (obj == null)
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
|
|
|
Characters c = obj as Characters;
|
|
|
if ((System.Object)c == null)
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
|
|
|
return Characters.AreEqual(c.CharArray, this.CharArray);
|
|
|
}
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
{
|
|
|
return base.GetHashCode();
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region operator Large Than (> ,>=)
|
|
|
|
|
|
|
|
|
public static bool operator >(Characters c1, Characters c2)
|
|
|
{
|
|
|
return Characters.LargeThan(c1.CharArray, c2.CharArray);
|
|
|
}
|
|
|
|
|
|
public static bool operator >(string s, Characters c2)
|
|
|
{
|
|
|
return Characters.LargeThan(s.ToCharArray(), c2.CharArray);
|
|
|
}
|
|
|
|
|
|
public static bool operator >(Characters c1, string s)
|
|
|
{
|
|
|
return Characters.LargeThan(c1.CharArray, s.ToCharArray());
|
|
|
}
|
|
|
|
|
|
|
|
|
public static bool operator >=(Characters c1, Characters c2)
|
|
|
{
|
|
|
bool isLE = Characters.AreEqual(c1.CharArray, c2.CharArray) || Characters.LargeThan(c1.CharArray, c2.CharArray);
|
|
|
return isLE;
|
|
|
}
|
|
|
|
|
|
public static bool operator >=(string s, Characters c2)
|
|
|
{
|
|
|
bool isLE = Characters.AreEqual(s.ToCharArray(), c2.CharArray) || Characters.LargeThan(s.ToCharArray(), c2.CharArray);
|
|
|
return isLE;
|
|
|
}
|
|
|
|
|
|
public static bool operator >=(Characters c1, string s)
|
|
|
{
|
|
|
bool isLE = Characters.AreEqual(c1.CharArray, s.ToCharArray()) || Characters.LargeThan(c1.CharArray, s.ToCharArray());
|
|
|
return isLE;
|
|
|
}
|
|
|
|
|
|
|
|
|
public static bool LargeThan(char[] left, char[] right)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int maxLength = Math.Max(left.Length, right.Length);
|
|
|
char[] leftCharArray = Characters.GetCharArray(left, maxLength);
|
|
|
char[] rightCharArray = Characters.GetCharArray(right, maxLength);
|
|
|
|
|
|
bool isLargeThan = false;
|
|
|
|
|
|
for (int i = 0; i < leftCharArray.Length; i++)
|
|
|
{
|
|
|
if (leftCharArray[i] != rightCharArray[i])
|
|
|
{
|
|
|
if (leftCharArray[i] > rightCharArray[i])
|
|
|
{
|
|
|
isLargeThan = true;
|
|
|
break;
|
|
|
}
|
|
|
else if (leftCharArray[i] < rightCharArray[i])
|
|
|
{
|
|
|
isLargeThan = false;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return isLargeThan;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
#region operator Large Than (< ,<=)
|
|
|
|
|
|
public static bool operator <(Characters c1, Characters c2)
|
|
|
{
|
|
|
return Characters.LeastThan(c1.CharArray, c2.CharArray);
|
|
|
}
|
|
|
|
|
|
public static bool operator <(string s, Characters c2)
|
|
|
{
|
|
|
return Characters.LeastThan(s.ToCharArray(), c2.CharArray);
|
|
|
}
|
|
|
|
|
|
public static bool operator <(Characters c1, string s)
|
|
|
{
|
|
|
return Characters.LeastThan(c1.CharArray, s.ToCharArray());
|
|
|
}
|
|
|
|
|
|
public static bool operator <=(Characters c1, Characters c2)
|
|
|
{
|
|
|
bool isLE = Characters.AreEqual(c1.CharArray, c2.CharArray) || Characters.LeastThan(c1.CharArray, c2.CharArray);
|
|
|
return isLE;
|
|
|
}
|
|
|
|
|
|
public static bool operator <=(string s, Characters c2)
|
|
|
{
|
|
|
bool isLE = Characters.AreEqual(s.ToCharArray(), c2.CharArray) || Characters.LeastThan(s.ToCharArray(), c2.CharArray);
|
|
|
return isLE;
|
|
|
}
|
|
|
|
|
|
public static bool operator <=(Characters c1, string s)
|
|
|
{
|
|
|
bool isLE = Characters.AreEqual(c1.CharArray, s.ToCharArray()) || Characters.LeastThan(c1.CharArray, s.ToCharArray());
|
|
|
return isLE;
|
|
|
}
|
|
|
|
|
|
|
|
|
public static bool LeastThan(char[] left, char[] right)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int maxLength = Math.Max(left.Length, right.Length);
|
|
|
char[] leftCharArray = Characters.GetCharArray(left, maxLength);
|
|
|
char[] rightCharArray = Characters.GetCharArray(right, maxLength);
|
|
|
|
|
|
bool isLeastThan = false;
|
|
|
|
|
|
for (int i = 0; i < leftCharArray.Length; i++)
|
|
|
{
|
|
|
if (leftCharArray[i] != rightCharArray[i])
|
|
|
{
|
|
|
if (leftCharArray[i] < rightCharArray[i])
|
|
|
{
|
|
|
isLeastThan = true;
|
|
|
break;
|
|
|
}
|
|
|
else if (leftCharArray[i] > rightCharArray[i])
|
|
|
{
|
|
|
isLeastThan = false;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return isLeastThan;
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
private static char[] GetCharArray(char[] source, int length)
|
|
|
{
|
|
|
if (source.Length == length) return source;
|
|
|
char[] newCharArray = new char[length];
|
|
|
int sourceLength = Math.Min(source.Length, length);
|
|
|
|
|
|
for (int i = 0; i < sourceLength; i++)
|
|
|
{
|
|
|
newCharArray[i] = source[i];
|
|
|
}
|
|
|
|
|
|
for (int i = sourceLength; i < newCharArray.Length; i++)
|
|
|
{
|
|
|
newCharArray[i] = ' ';
|
|
|
}
|
|
|
|
|
|
return newCharArray;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region implicit Operators
|
|
|
|
|
|
public static implicit operator Characters(string s)
|
|
|
{
|
|
|
Characters TheChararacters = new Characters(s);
|
|
|
return TheChararacters;
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
public override string ToString()
|
|
|
{
|
|
|
return new String(this._CharArray);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|