Пример #1
0
        /// <summary>
        /// Sets data to a given register offset.
        /// </summary>
        /// <typeparam name="T">Type of the data</typeparam>
        /// <param name="offset">Register offset</param>
        /// <param name="data">The data to set</param>
        public void Set <T>(MethodOffset offset, T data) where T : unmanaged
        {
            ReadOnlySpan <int> intSpan = MemoryMarshal.Cast <T, int>(MemoryMarshal.CreateReadOnlySpan(ref data, 1));

            intSpan.CopyTo(_memory.AsSpan().Slice((int)offset, intSpan.Length));
        }
Пример #2
0
 private void SetCork(int enabled)
 {
     Debug.Assert(enabled is 0 or 1);
     Socket.SetRawSocketOption(IPPROTO_TCP, TCP_CORK, MemoryMarshal.Cast <int, byte>(MemoryMarshal.CreateReadOnlySpan(ref enabled, 1)));
 }
Пример #3
0
 public ReadOnlySpan <byte> AsSpan()
 {
     return(MemoryMarshal.AsBytes(MemoryMarshal.CreateReadOnlySpan(ref this, 1)));
 }
Пример #4
0
 /// <summary>
 /// Adds single element to this builder.
 /// </summary>
 /// <param name="item">The item to be added.</param>
 /// <exception cref="InsufficientMemoryException">Pre-allocated initial buffer size is not enough to place <paramref name="item"/> to it and this builder is not growable.</exception>
 public void Add(T item) => Write(MemoryMarshal.CreateReadOnlySpan(ref item, 1));
Пример #5
0
        /// <summary>Format the TimeSpan instance using the specified format.</summary>
        private static StringBuilder FormatCustomized(TimeSpan value, ReadOnlySpan <char> format, DateTimeFormatInfo dtfi, StringBuilder result)
        {
            Debug.Assert(dtfi != null);

            bool resultBuilderIsPooled = false;

            if (result == null)
            {
                result = StringBuilderCache.Acquire(InternalGlobalizationHelper.StringBuilderDefaultCapacity);
                resultBuilderIsPooled = true;
            }

            int  day  = (int)(value.Ticks / TimeSpan.TicksPerDay);
            long time = value.Ticks % TimeSpan.TicksPerDay;

            if (value.Ticks < 0)
            {
                day  = -day;
                time = -time;
            }
            int hours    = (int)(time / TimeSpan.TicksPerHour % 24);
            int minutes  = (int)(time / TimeSpan.TicksPerMinute % 60);
            int seconds  = (int)(time / TimeSpan.TicksPerSecond % 60);
            int fraction = (int)(time % TimeSpan.TicksPerSecond);

            long tmp = 0;
            int  i   = 0;
            int  tokenLen;

            while (i < format.Length)
            {
                char ch = format[i];
                int  nextChar;
                switch (ch)
                {
                case 'h':
                    tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                    if (tokenLen > 2)
                    {
                        goto default;     // to release the builder and throw
                    }
                    DateTimeFormat.FormatDigits(result, hours, tokenLen);
                    break;

                case 'm':
                    tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                    if (tokenLen > 2)
                    {
                        goto default;     // to release the builder and throw
                    }
                    DateTimeFormat.FormatDigits(result, minutes, tokenLen);
                    break;

                case 's':
                    tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                    if (tokenLen > 2)
                    {
                        goto default;     // to release the builder and throw
                    }
                    DateTimeFormat.FormatDigits(result, seconds, tokenLen);
                    break;

                case 'f':
                    //
                    // The fraction of a second in single-digit precision. The remaining digits are truncated.
                    //
                    tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                    if (tokenLen > DateTimeFormat.MaxSecondsFractionDigits)
                    {
                        goto default;     // to release the builder and throw
                    }

                    tmp  = fraction;
                    tmp /= TimeSpanParse.Pow10(DateTimeFormat.MaxSecondsFractionDigits - tokenLen);
                    result.Append((tmp).ToString(DateTimeFormat.fixedNumberFormats[tokenLen - 1], CultureInfo.InvariantCulture));
                    break;

                case 'F':
                    //
                    // Displays the most significant digit of the seconds fraction. Nothing is displayed if the digit is zero.
                    //
                    tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                    if (tokenLen > DateTimeFormat.MaxSecondsFractionDigits)
                    {
                        goto default;     // to release the builder and throw
                    }

                    tmp  = fraction;
                    tmp /= TimeSpanParse.Pow10(DateTimeFormat.MaxSecondsFractionDigits - tokenLen);
                    int effectiveDigits = tokenLen;
                    while (effectiveDigits > 0)
                    {
                        if (tmp % 10 == 0)
                        {
                            tmp = tmp / 10;
                            effectiveDigits--;
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (effectiveDigits > 0)
                    {
                        result.Append((tmp).ToString(DateTimeFormat.fixedNumberFormats[effectiveDigits - 1], CultureInfo.InvariantCulture));
                    }
                    break;

                case 'd':
                    //
                    // tokenLen == 1 : Day as digits with no leading zero.
                    // tokenLen == 2+: Day as digits with leading zero for single-digit days.
                    //
                    tokenLen = DateTimeFormat.ParseRepeatPattern(format, i, ch);
                    if (tokenLen > 8)
                    {
                        goto default;     // to release the builder and throw
                    }

                    DateTimeFormat.FormatDigits(result, day, tokenLen, true);
                    break;

                case '\'':
                case '\"':
                    tokenLen = DateTimeFormat.ParseQuoteString(format, i, result);
                    break;

                case '%':
                    // Optional format character.
                    // For example, format string "%d" will print day
                    // Most of the cases, "%" can be ignored.
                    nextChar = DateTimeFormat.ParseNextChar(format, i);
                    // nextChar will be -1 if we already reach the end of the format string.
                    // Besides, we will not allow "%%" appear in the pattern.
                    if (nextChar >= 0 && nextChar != (int)'%')
                    {
                        char          nextCharChar      = (char)nextChar;
                        StringBuilder origStringBuilder = FormatCustomized(value, MemoryMarshal.CreateReadOnlySpan <char>(ref nextCharChar, 1), dtfi, result);
                        Debug.Assert(ReferenceEquals(origStringBuilder, result));
                        tokenLen = 2;
                    }
                    else
                    {
                        //
                        // This means that '%' is at the end of the format string or
                        // "%%" appears in the format string.
                        //
                        goto default;     // to release the builder and throw
                    }
                    break;

                case '\\':
                    // Escaped character.  Can be used to insert character into the format string.
                    // For example, "\d" will insert the character 'd' into the string.
                    //
                    nextChar = DateTimeFormat.ParseNextChar(format, i);
                    if (nextChar >= 0)
                    {
                        result.Append(((char)nextChar));
                        tokenLen = 2;
                    }
                    else
                    {
                        //
                        // This means that '\' is at the end of the formatting string.
                        //
                        goto default;     // to release the builder and throw
                    }
                    break;

                default:
                    // Invalid format string
                    if (resultBuilderIsPooled)
                    {
                        StringBuilderCache.Release(result);
                    }
                    throw new FormatException(SR.Format_InvalidString);
                }
                i += tokenLen;
            }
            return(result);
        }
Пример #6
0
 /// <summary>
 /// Writes a byte to the current position in the stream and advances the position within the stream by one byte.
 /// </summary>
 /// <param name="value">The byte to write to the stream.</param>
 /// <exception cref="InvalidOperationException">Cannot perform write operations on a <see cref="BrotliStream" /> constructed with <see cref="CompressionMode.Decompress" />.
 /// -or-
 /// The encoder ran into invalid data.</exception>
 public override void WriteByte(byte value)
 {
     WriteCore(MemoryMarshal.CreateReadOnlySpan(ref value, 1));
 }
Пример #7
0
 public override void WriteByte(byte value)
 {
     ThrowIfDisposed();
     _writeBuffer.Write(MemoryMarshal.CreateReadOnlySpan(ref value, 1));
 }
Пример #8
0
        public void OnControlSequenceCommand(ControlSequenceCommand command, ReadOnlySpan <byte> intermediates, ReadOnlySpan <long> parameters, IgnoredData ignored = default)
        {
            var priv = intermediates.Optional(0).GetValueOrDefault(0) == '?';

            long?row, column;

            switch (command)
            {
            case ControlSequenceCommand.InsertBlank:
                _events.InsertBlank?.Invoke(parameters.Optional(0).GetValueOrDefault(1));
                break;

            case ControlSequenceCommand.MoveUp:
                _events.MoveUp?.Invoke(parameters.Optional(0).GetValueOrDefault(1), false);
                break;

            case ControlSequenceCommand.RepeatPrecedingCharacter:
                if (_precedingChar.HasValue)
                {
                    var count = parameters.Optional(0).GetValueOrDefault(1);
                    var c     = _precedingChar.Value;
                    ReadOnlySpan <char> chr = MapCharacters(MemoryMarshal.CreateReadOnlySpan(ref c, 1));
                    for (var i = 0; i < count; i++)
                    {
                        _events.Input?.Invoke(chr);
                    }
                }
                break;

            case ControlSequenceCommand.MoveDown1:
            case ControlSequenceCommand.MoveDown2:
                _events.MoveDown?.Invoke(parameters.Optional(0).GetValueOrDefault(1), false);
                break;

            case ControlSequenceCommand.IdentifyTerminal:
                _events.IdentifyTerminal?.Invoke();
                break;

            case ControlSequenceCommand.MoveForward1:
            case ControlSequenceCommand.MoveForward2:
                _events.MoveForward?.Invoke(parameters.Optional(0).GetValueOrDefault(1), false);
                break;

            case ControlSequenceCommand.MoveBackward:
                _events.MoveBackward?.Invoke(parameters.Optional(0).GetValueOrDefault(1), false);
                break;

            case ControlSequenceCommand.MoveDownAndCr:
                _events.MoveDown?.Invoke(parameters.Optional(0).GetValueOrDefault(1), true);
                break;

            case ControlSequenceCommand.MoveUpAndCr:
                _events.MoveUp?.Invoke(parameters.Optional(0).GetValueOrDefault(1), true);
                break;

            case ControlSequenceCommand.ClearTabulation:
                var tabClearMode = (TabulationClearMode)parameters.Optional(0).GetValueOrDefault(0);
                _events.ClearTabulation?.Invoke(tabClearMode);
                break;

            case ControlSequenceCommand.GotoColumn1:
            case ControlSequenceCommand.GotoColumn2:
                column = parameters.Optional(0).GetValueOrDefault(1) - 1;
                _events.Goto?.Invoke(column: column);
                break;

            case ControlSequenceCommand.Goto1:
            case ControlSequenceCommand.Goto2:
                column = parameters.Optional(0).GetValueOrDefault(1) - 1;
                row    = parameters.Optional(1).GetValueOrDefault(1) - 1;
                _events.Goto?.Invoke(column, row);
                break;

            case ControlSequenceCommand.MoveForwardTabs:
                _events.MoveForward?.Invoke(parameters.Optional(0).GetValueOrDefault(1), true);
                break;

            case ControlSequenceCommand.ClearScreen:
                var clearScreenMode = (ClearMode)parameters.Optional(0).GetValueOrDefault(0);
                _events.ClearScreen?.Invoke(clearScreenMode);
                break;

            case ControlSequenceCommand.ClearLine:
                var clearLineMode = (LineClearMode)parameters.Optional(0).GetValueOrDefault(0);
                _events.ClearLine?.Invoke(clearLineMode);
                break;

            case ControlSequenceCommand.ScrollUp:
                _events.ScrollUp?.Invoke(parameters.Optional(0).GetValueOrDefault(1));
                break;

            case ControlSequenceCommand.ScrollDown:
                _events.ScrollDown?.Invoke(parameters.Optional(0).GetValueOrDefault(1));
                break;

            case ControlSequenceCommand.InsertBlankLines:
                _events.InsertBlankLines?.Invoke(parameters.Optional(0).GetValueOrDefault(1));
                break;

            case ControlSequenceCommand.UnsetMode:
                for (var i = 0; i < parameters.Length; i++)
                {
                    _events.UnsetMode?.Invoke((Mode)parameters[i]);
                }
                break;

            case ControlSequenceCommand.DeleteLines:
                _events.DeleteLines?.Invoke(parameters.Optional(0).GetValueOrDefault(1));
                break;

            case ControlSequenceCommand.EraseChars:
                _events.EraseCharacters?.Invoke(parameters.Optional(0).GetValueOrDefault(1));
                break;

            case ControlSequenceCommand.DeleteChars:
                _events.DeleteCharacters?.Invoke(parameters.Optional(0).GetValueOrDefault(1));
                break;

            case ControlSequenceCommand.MoveBackwardTabs:
                _events.MoveBackward?.Invoke(parameters.Optional(0).GetValueOrDefault(1), true);
                break;

            case ControlSequenceCommand.GotoLine:
                _events.Goto?.Invoke(line: parameters.Optional(0).GetValueOrDefault(1) - 1);
                break;

            case ControlSequenceCommand.SetMode:
                for (var i = 0; i < parameters.Length; i++)
                {
                    _events.SetMode?.Invoke((Mode)parameters[i]);
                }
                break;

            case ControlSequenceCommand.TerminalAttribute:
                if (parameters.Length == 0)
                {
                    _events.TerminalAttribute?.Invoke(
                        TerminalAttribute.Reset);
                    break;
                }

                for (var i = 0; i < parameters.Length; i++)
                {
                    var index = parameters[i];

                    if (index < 30)
                    {
                        _events.TerminalAttribute?.Invoke(
                            (TerminalAttribute)index);
                        continue;
                    }

                    if (index == 38 || index == 48)
                    {
                        var consumed = TryParseColor(parameters.Slice(i), out NamedColor? ix, out Color? color);
                        if (consumed == 0)
                        {
                            break;
                        }
                        _events.TerminalAttribute?.Invoke(
                            (TerminalAttribute)index, index: ix, color: color);
                        i += consumed;
                        continue;
                    }

                    if (index == 39)
                    {
                        _events.TerminalAttribute?.Invoke(
                            TerminalAttribute.SetForeground, index: NamedColor.Foreground);
                        continue;
                    }

                    if (index < 40)
                    {
                        var ix = (NamedColor)(index - 30);
                        _events.TerminalAttribute?.Invoke(
                            TerminalAttribute.SetForeground, index: ix);
                        continue;
                    }

                    if (index == 49)
                    {
                        _events.TerminalAttribute?.Invoke(
                            TerminalAttribute.SetBackground, index: NamedColor.Background);
                        continue;
                    }

                    if (index < 50)
                    {
                        var ix = (NamedColor)(index - 40);
                        _events.TerminalAttribute?.Invoke(
                            TerminalAttribute.SetForeground, index: ix);
                        continue;
                    }

                    if (index < 90)
                    {
                        continue;
                    }

                    if (index < 100)
                    {
                        var ix = (NamedColor)(index - 90 + (int)NamedColor.BrightBlack);
                        _events.TerminalAttribute?.Invoke(
                            TerminalAttribute.SetForeground, index: ix);
                        continue;
                    }

                    if (index < 108)
                    {
                        var ix = (NamedColor)(index - 100 + (int)NamedColor.BrightBlack);
                        _events.TerminalAttribute?.Invoke(
                            TerminalAttribute.SetBackground, index: ix);
                        continue;
                    }
                }
                break;

            case ControlSequenceCommand.DeviceStatus:
                _events.DeviceStatus?.Invoke(parameters.Optional(0).GetValueOrDefault(0));
                break;

            case ControlSequenceCommand.SetScrollingRegion:
                if (priv)
                {
                    break;
                }
                _events.SetScrollingRegion?.Invoke(
                    parameters.Optional(0) - 1,
                    parameters.Optional(1) - 1);
                break;

            case ControlSequenceCommand.SaveCursorPosition:
                _events.SaveCursorPosition?.Invoke();
                break;

            case ControlSequenceCommand.RestoreCursorPosition:
                _events.RestoreCursorPosition?.Invoke();
                break;

            case ControlSequenceCommand.SetCursorStyle:
                switch (parameters.Optional(0).GetValueOrDefault(0))
                {
                case 0: _events.SetCursorStyle?.Invoke(default); break;

                case 1:
                case 2: _events.SetCursorStyle?.Invoke(CursorStyle.Block); break;

                case 3:
                case 4: _events.SetCursorStyle?.Invoke(CursorStyle.Underline); break;

                case 5:
                case 6: _events.SetCursorStyle?.Invoke(CursorStyle.Beam); break;
                }
                break;
            }
        }
Пример #9
0
 public ulong Hash(ref Outpoint outpoint)
 {
     return(this.Compute(MemoryMarshal.AsBytes(MemoryMarshal.CreateReadOnlySpan(ref outpoint, 1))));
 }
Пример #10
0
 public static ReadOnlySpan <T> CreateReadOnlySpan <T>(ref T reference, int length)
 {
     return(MemoryMarshal.CreateReadOnlySpan(ref reference, length));
 }
Пример #11
0
 public static string ByteToHex(this Span <byte> bytes)
 {
     return(MemoryMarshal.CreateReadOnlySpan(ref MemoryMarshal.GetReference(bytes), bytes.Length).ByteToHex());
 }
Пример #12
0
        public static int ToInt(this bool @this)
        {
            var myBoolSpan = MemoryMarshal.CreateReadOnlySpan(ref @this, 1);

            return(MemoryMarshal.AsBytes(myBoolSpan)[0]);
        }