public static T Initialize <T>([NotNull] ref T?target, T value) where T : class
 {
     RoslynDebug.Assert((object?)value != null);
     return(Interlocked.CompareExchange(ref target, value, null) ?? value);
 }
Beispiel #2
0
 public V Single()
 {
     RoslynDebug.Assert(_value is V); // Implies value != null
     return((V)_value);
 }
 internal static void AppendData(this IncrementalHash hash, ArraySegment <byte> segment)
 {
     RoslynDebug.AssertNotNull(segment.Array);
     hash.AppendData(segment.Array, segment.Offset, segment.Count);
 }
Beispiel #4
0
        // String escaping implementation forked from System.Runtime.Serialization.Json to
        // avoid a large dependency graph for this small amount of code:
        //
        // https://github.com/dotnet/corefx/blob/master/src/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/JavaScriptString.cs
        //
        private static string EscapeString(string?value)
        {
            PooledStringBuilder?pooledBuilder = null;
            StringBuilder?      b             = null;

            if (RoslynString.IsNullOrEmpty(value))
            {
                return(string.Empty);
            }

            int startIndex = 0;
            int count      = 0;

            for (int i = 0; i < value.Length; i++)
            {
                char c = value[i];

                if (c == '\"' || c == '\\' || ShouldAppendAsUnicode(c))
                {
                    if (b == null)
                    {
                        RoslynDebug.Assert(pooledBuilder == null);
                        pooledBuilder = PooledStringBuilder.GetInstance();
                        b             = pooledBuilder.Builder;
                    }

                    if (count > 0)
                    {
                        b.Append(value, startIndex, count);
                    }

                    startIndex = i + 1;
                    count      = 0;

                    switch (c)
                    {
                    case '\"':
                        b.Append("\\\"");
                        break;

                    case '\\':
                        b.Append("\\\\");
                        break;

                    default:
                        Debug.Assert(ShouldAppendAsUnicode(c));
                        AppendCharAsUnicode(b, c);
                        break;
                    }
                }
                else
                {
                    count++;
                }
            }

            if (b == null)
            {
                return(value);
            }
            else
            {
                RoslynDebug.Assert(pooledBuilder is object);
            }

            if (count > 0)
            {
                b.Append(value, startIndex, count);
            }

            return(pooledBuilder.ToStringAndFree());
        }