Ejemplo n.º 1
0
        public void Acquire_should_hand_out_different_instances_when_called_multiple_times_in_a_row_from_same_thread()
        {
            var array1 = CharArrayCache.Acquire(10);
            var array2 = CharArrayCache.Acquire(10);
            var array3 = CharArrayCache.Acquire(10);

            array1.Should().NotBeSameAs(array2);
            array1.Should().NotBeSameAs(array3);
        }
Ejemplo n.º 2
0
        public void Acquire_should_cache_and_reuse_returned_instances()
        {
            var array = CharArrayCache.Acquire(10);

            for (var i = 0; i < 10; i++)
            {
                CharArrayCache.Return(array);

                CharArrayCache.Acquire(10).Should().BeSameAs(array);
            }
        }
        // TODO(krait): Can't it render messages directly to TextWriter? But let's not change it yet..
        /// <summary>
        /// <para>Renders the template to a fully formed log message, replacing placeholders with values from <paramref name="properties"/>.</para>
        /// <para>See <see cref="LogEvent.MessageTemplate"/> for details on <paramref name="template"/> format.</para>
        /// <para>This method never throws exceptions.</para>
        /// </summary>
        /// <param name="template">A message template with zero or more placeholders to substitute.</param>
        /// <param name="properties">A dictionary of properties to be used for substitution.</param>
        public static string FormatMessage([CanBeNull] string template, [CanBeNull] IReadOnlyDictionary <string, object> properties)
        {
            if (template == null)
            {
                return(null);
            }

            if (properties == null)
            {
                return(template);
            }

            var resultBuilder     = StringBuilderCache.Acquire(template.Length * 2);
            var tokenBuilderChars = CharArrayCache.Acquire(template.Length);
            var tokenBuilder      = new TokenBuilder(tokenBuilderChars);

            for (var i = 0; i < template.Length; i++)
            {
                var currentChar = template[i];

                if (currentChar != '{' && currentChar != '}')
                {
                    tokenBuilder.Add(currentChar);
                    continue;
                }

                if (!tokenBuilder.IsEmpty)
                {
                    tokenBuilder.MoveToResult(resultBuilder);
                }

                if (i == template.Length - 1)
                {
                    tokenBuilder.Add(currentChar);
                    continue;
                }

                var nextChar = template[i + 1];
                if (currentChar == nextChar)
                {
                    tokenBuilder.Add(currentChar);
                    i++;
                    continue;
                }

                if (currentChar == '}')
                {
                    tokenBuilder.Add(currentChar);
                    continue;
                }

                var findTokenResult = tokenBuilder.TryFindToken(template, i);

                i += tokenBuilder.Length - 1;

                if (findTokenResult)
                {
                    var key = tokenBuilder.GetKeyFromBuffer();
                    if (properties.TryGetValue(key, out var value))
                    {
                        resultBuilder.Append(FormatPropertyValue(value));
                        tokenBuilder.Clear();
                    }
                }
            }

            if (!tokenBuilder.IsEmpty)
            {
                tokenBuilder.MoveToResult(resultBuilder);
            }

            CharArrayCache.Return(tokenBuilderChars);

            return(StringBuilderCache.GetStringAndRelease(resultBuilder));
        }
Ejemplo n.º 4
0
 public void Acquire_should_always_provide_an_array_of_at_least_requested_capacity(int capacity)
 {
     CharArrayCache.Acquire(capacity).Length.Should().BeGreaterOrEqualTo(capacity);
 }
Ejemplo n.º 5
0
        private string ReadString(IStreamReader reader)
        {
            var bLen = reader.ReadInt32(); // Byte array length

            if (bLen < 0)
            {
                return(null);
            }

            if (bLen == 0)
            {
                return(String.Empty);
            }

            if (bLen == 1)
            {
                return(new String((char)reader.ReadByte(), 1));
            }

            var sLen = reader.ReadInt32(); // String length

            if (sLen > bLen || bLen > 4 * sLen)
            {
                throw new Exception(SerializationErrors.StreamNotContainingValidWireMessage);
            }

            if (sLen > MaxAllowedStringLength)
            {
                throw new Exception(SerializationErrors.StringLengthExceededAllowedLimit);
            }

            var sb    = (StringBuilder)null;
            var bytes = StringBytesCache.Acquire();

            try
            {
                var chars = StringCharsCache.Acquire();
                try
                {
                    var readLen     = 0;
                    var bytesOffset = 0;
                    var bytesUsed   = 0;
                    var charsUsed   = 0;
                    var completed   = false;

                    var remaining = bLen;
                    do
                    {
                        readLen = Math.Min(StringBufferSize - bytesOffset, remaining);

                        readLen = reader.Read(bytes, bytesOffset, readLen);
                        if (readLen == bLen)
                        {
                            return(Encoding.UTF8.GetString(bytes, 0, readLen));
                        }

                        if (readLen == 0)
                        {
                            throw new ArgumentOutOfRangeException(nameof(readLen));
                        }

                        bytesOffset += readLen;

                        UTF8Decoder.Convert(bytes, 0, bytesOffset, chars, 0, StringBufferSize,
                                            false, out bytesUsed, out charsUsed, out completed);

                        if (charsUsed > 0)
                        {
                            if (sb == null)
                            {
                                sb = new StringBuilder(sLen);
                            }

                            sb.Append(chars, 0, charsUsed);

                            bytesOffset -= bytesUsed;
                            if (bytesOffset > 0)
                            {
                                Array.Copy(bytes, bytesUsed, bytes, 0, bytesOffset);
                            }
                        }
                    }while ((remaining -= readLen) > 0);
                }
                finally
                {
                    StringCharsCache.Release(chars);
                }
            }
            finally
            {
                StringBytesCache.Release(bytes);
            }

            return(sb?.ToString());
        }