/// <summary> /// A string extension method that right safe. /// </summary> /// <param name="instance">The instance to act on.</param> /// <param name="length">The length.</param> /// <returns>A string.</returns> public static string RightSafe(this string instance, int length) { ThrowHelper.ArgumentNull((instance == null), nameof(instance)); ThrowHelper.ArgumentMustNotNegative((length < 0), nameof(length)); return(instance.Substring(Math.Max(0, instance.Length - length))); }
/// <summary> /// A string extension method that return the left part of the string. /// </summary> /// <param name="instance">The instance to act on.</param> /// <param name="length">The length.</param> /// <returns>The left part.</returns> public static string Left(this string instance, int length) { ThrowHelper.ArgumentNull((instance == null), nameof(instance)); ThrowHelper.ArgumentMustNotNegative((length < 0), nameof(length)); return(instance.Substring(0, length)); }
/// <summary> /// Receive data by size from server. /// </summary> /// <param name="instance">socket instance</param> /// <param name="size">ensure receiving size</param> /// <returns>received data</returns> public static byte[] ReceiveBySize(this Socket instance, int size) { ThrowHelper.ArgumentMustNotNegative((size < 0), nameof(size)); byte[] buffer = new byte[size]; instance.ReceiveBySize(buffer, 0, size); return(buffer); }
public void WriteZeroBytes(int count) { ThrowHelper.ArgumentMustNotNegative(count < 0, nameof(count)); this.EnsureWriteSpace(count); Array.Clear(this._buffer, this._index, count); this._index += count; }
public byte[] ReadBytes(int size) { ThrowHelper.ArgumentMustNotNegative((size < 0), nameof(size)); this.EnsureGetSpace(size); byte[] value = new byte[size]; this.ReadBytes(value); return(value); }
public static byte[] CreateRandomBytes(int size) { ThrowHelper.ArgumentMustNotNegative((size < 0), nameof(size)); Random random = new Random(); byte[] data = new byte[size]; random.NextBytes(data); return(data); }
public string ReadString(int length, Encoding encoding) { ThrowHelper.ArgumentMustNotNegative((length < 0), nameof(length)); ThrowHelper.ArgumentNull((encoding == null), nameof(encoding)); this.EnsureGetSpace(length); string value = encoding.GetString(this._buffer, this._index, length); this._index += length; return(value); }
public static string GenerateAlphanumeric(int size) { ThrowHelper.ArgumentMustNotNegative((size < 0), nameof(size)); Random random = new Random(); char[] buffer = new char[size]; for (int i = 0; i < size; i++) { buffer[i] = AlphanumericTemplate[random.Next(AlphanumericTemplate.Length)]; } return(new string(buffer)); }
/// <summary> /// A string extension method that repeats the string a specified number of times. /// </summary> /// <param name="instance">The instance to act on.</param> /// <param name="repeatCount">Number of repeats.</param> /// <returns>The repeated string.</returns> public static string Repeat(this string instance, int repeatCount) { ThrowHelper.ArgumentNull((instance == null), nameof(instance)); ThrowHelper.ArgumentMustNotNegative((repeatCount < 0), nameof(repeatCount)); StringBuilder builder = new StringBuilder(repeatCount * instance.Length); for (int i = 0; i < repeatCount; i++) { builder.Append(instance); } return(builder.ToString()); }
public static string GetString(this byte[] array, int offset, Encoding encoding) { ThrowHelper.ArgumentNull((array == null), nameof(array)); ThrowHelper.ArgumentMustNotNegative((offset < 0), nameof(offset)); int end = Array.IndexOf <byte>(array, 0, offset); if (end < 0) { end = array.Length; } string text = encoding.GetString(array, offset, end - offset); return(text); }
public static string BaseXEncode(long value, string templates) { ThrowHelper.ArgumentMustNotNegative((value < 0), nameof(value)); ThrowHelper.ArgumentNull((templates == null), nameof(templates)); ThrowHelper.ArgumentStringMustNotEmpty((templates.Length == 0), nameof(templates)); long current = value; System.Collections.Generic.Stack <char> result = new System.Collections.Generic.Stack <char>(); do { result.Push(templates[(int)(current % templates.Length)]); current /= templates.Length; }while (current != 0); return(new string(result.ToArray())); }
public DecadeFullTime(int value) { ThrowHelper.ArgumentMustNotNegative((value < 0), nameof(value)); this._value = value; }
public static string Base2Encode(long value) { ThrowHelper.ArgumentMustNotNegative((value < 0), nameof(value)); return(BaseXEncode(value, Base2Templates)); }
public IntMinute(int value) { ThrowHelper.ArgumentMustNotNegative((value < 0), nameof(value)); this._value = value; }