コード例 #1
0
    /// <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)));
    }
コード例 #2
0
    /// <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));
    }
コード例 #3
0
    /// <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);
    }
コード例 #4
0
        public void WriteZeroBytes(int count)
        {
            ThrowHelper.ArgumentMustNotNegative(count < 0, nameof(count));

            this.EnsureWriteSpace(count);

            Array.Clear(this._buffer, this._index, count);
            this._index += count;
        }
コード例 #5
0
        public byte[] ReadBytes(int size)
        {
            ThrowHelper.ArgumentMustNotNegative((size < 0), nameof(size));

            this.EnsureGetSpace(size);

            byte[] value = new byte[size];
            this.ReadBytes(value);
            return(value);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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));
        }
コード例 #9
0
    /// <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());
    }
コード例 #10
0
        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);
        }
コード例 #11
0
        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()));
        }
コード例 #12
0
ファイル: [V]DecadeFullTime.cs プロジェクト: ackmanFork/Lever
        public DecadeFullTime(int value)
        {
            ThrowHelper.ArgumentMustNotNegative((value < 0), nameof(value));

            this._value = value;
        }
コード例 #13
0
        public static string Base2Encode(long value)
        {
            ThrowHelper.ArgumentMustNotNegative((value < 0), nameof(value));

            return(BaseXEncode(value, Base2Templates));
        }
コード例 #14
0
        public IntMinute(int value)
        {
            ThrowHelper.ArgumentMustNotNegative((value < 0), nameof(value));

            this._value = value;
        }