Exemple #1
0
        private static void AssertModeMatchesType <T>(T tss) where T : IHasToStringState
        {
            ToStringMode expected =
                tss is ISpanFormattable ? ToStringMode.ISpanFormattableTryFormat :
                tss is IFormattable ? ToStringMode.IFormattableToString :
                ToStringMode.ObjectToString;

            Assert.Equal(expected, tss.ToStringState.ToStringMode);
        }
Exemple #2
0
        // Adds escape sequences (such as "\n", or "\007") to the input string
        internal static string EscapeString(string s, ToStringMode toStringMode, char?special = null)
        {
            Debug.Assert(special == null || (special >= 32 && special <= 126),
                         "special character must be in ASCII range 32-126");

            if (toStringMode == ToStringMode.Compat)
            {
                // Encode UTF-8 bytes
                var    utf8  = new UTF8Encoding();
                byte[] bytes = utf8.GetBytes(s);

                var result = new StringBuilder(bytes.Length);
                for (int i = 0; i < bytes.Length; i++)
                {
                    EncodeChar((char)bytes[i], result, toStringMode, special);
                }

                return(result.ToString());
            }
            else
            {
                var result = new StringBuilder(s.Length);

                for (int i = 0; i < s.Length; i++)
                {
                    char c = s[i];
                    if ((toStringMode == ToStringMode.Unicode) || !char.IsSurrogate(c))
                    {
                        EncodeChar(c, result, toStringMode, special);
                    }
                    else
                    {
                        Debug.Assert((toStringMode == ToStringMode.ASCII) && char.IsSurrogate(c));
                        if (i + 1 == s.Length)
                        {
                            throw new System.ArgumentException("high surrogate without low surrogate", nameof(s));
                        }
                        else
                        {
                            i++;
                            int codePoint = char.ConvertToUtf32(c, s[i]);
                            // append \Unnnnnnnn
                            result.Append("\\U");
                            string hex = System.Convert.ToString(codePoint, 16);
                            for (int j = hex.Length; j < 8; j++)
                            {
                                result.Append('0');
                            }
                            result.Append(hex);
                        }
                    }
                }

                return(result.ToString());
            }
        }
Exemple #3
0
 /// <summary>
 /// Converts an object identity to a string.
 /// </summary>
 /// <param name="ident">The object identity to convert.</param>
 /// <param name="toStringMode">Specifies if and how non-printable ASCII characters are escaped in the result.</param>
 /// <returns>The string representation of the object identity.</returns>
 public static string identityToString(Identity ident, ToStringMode toStringMode = ToStringMode.Unicode)
 {
     if (ident.category == null || ident.category.Length == 0)
     {
         return(IceUtilInternal.StringUtil.escapeString(ident.name, "/", toStringMode));
     }
     else
     {
         return(IceUtilInternal.StringUtil.escapeString(ident.category, "/", toStringMode) + '/' +
                IceUtilInternal.StringUtil.escapeString(ident.name, "/", toStringMode));
     }
 }
		/// <summary>
		/// Returns ToString converter instance for given ToString mode.
		/// </summary>
		/// <param name="mode">ToString mode.</param>
		/// <returns>Converter instance.</returns>
		/// <exception cref="ArgumentOutOfRangeException"><paramref name="mode" /> is out of range.</exception>
		static public IStringConverter GetStringConverter(ToStringMode mode)
		{
			switch (mode)
			{
				case ToStringMode.Fast:
					return FastStringConverter;
				case ToStringMode.Classic:
					return ClassicStringConverter;
				default:
					throw new ArgumentOutOfRangeException("mode");
			}
		}
Exemple #5
0
 /// <summary>
 /// Converts an object identity to a string.
 /// </summary>
 /// <param name="mode">Specifies if and how non-printable ASCII characters are escaped in the result.</param>
 /// <returns>The string representation of the object identity.</returns>
 public string ToString(ToStringMode mode)
 {
     if (string.IsNullOrEmpty(category))
     {
         return(IceUtilInternal.StringUtil.escapeString(name, "/", mode));
     }
     else
     {
         return(IceUtilInternal.StringUtil.escapeString(category, "/", mode) + '/' +
                IceUtilInternal.StringUtil.escapeString(name, "/", mode));
     }
 }
Exemple #6
0
 /// <summary>Converts an object identity to a string.</summary>
 /// <param name="mode">Specifies if and how non-printable ASCII characters are escaped in the result.</param>
 /// <returns>The string representation of the object identity.</returns>
 public string ToString(ToStringMode mode)
 {
     if (string.IsNullOrEmpty(Category))
     {
         return(StringUtil.EscapeString(Name, "/", mode));
     }
     else
     {
         return(StringUtil.EscapeString(Category, "/", mode) + '/' +
                StringUtil.EscapeString(Name, "/", mode));
     }
 }
Exemple #7
0
        /// <summary>
        /// Returns ToString converter instance for given ToString mode.
        /// </summary>
        /// <param name="mode">ToString mode.</param>
        /// <returns>Converter instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="mode" /> is out of range.</exception>
        static public IStringConverter GetStringConverter(ToStringMode mode)
        {
            switch (mode)
            {
            case ToStringMode.Fast:
                return(FastStringConverter);

            case ToStringMode.Classic:
                return(ClassicStringConverter);

            default:
                throw new ArgumentOutOfRangeException("mode");
            }
        }
        /// <summary>
        /// Returns ToString converter instance for given ToString mode.
        /// </summary>
        /// <param name="mode">ToString mode.</param>
        /// <returns>Converter instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="mode" /> is out of range.</exception>
        public static IStringConverter GetStringConverter(ToStringMode mode)
        {
            // Check value
            if (!Enum.IsDefined(typeof(ToStringMode), mode))
            {
                throw new ArgumentOutOfRangeException("mode");
            }

            switch (mode)
            {
                case ToStringMode.Fast:
                    return FastStringConverter;
                default:
                    return ClassicStringConverter;
            }
        }
        /// <summary>
        /// Returns ToString converter instance for given ToString mode.
        /// </summary>
        /// <param name="mode">ToString mode.</param>
        /// <returns>Converter instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="mode" /> is out of range.</exception>
        static public IStringConverter GetStringConverter(ToStringMode mode)
        {
            // Check value
            if (!Enum.IsDefined(typeof(ToStringMode), mode))
            {
                throw new ArgumentOutOfRangeException("mode");
            }

            switch (mode)
            {
            case ToStringMode.Fast:
                return(FastStringConverter);

            default:
                return(ClassicStringConverter);
            }
        }
Exemple #10
0
        private static void TraceRequest(
            string traceFramePrefix,
            Communicator communicator,
            Protocol protocol,
            byte frameType,
            byte compress,
            int size,
            int requestId,
            Identity identity,
            string facet,
            string operation,
            bool isIdempotent,
            IReadOnlyDictionary <string, string> context,
            Encoding encoding)
        {
            if (communicator.TraceLevels.Protocol >= 1)
            {
                var s = new StringBuilder();
                s.Append(traceFramePrefix);
                PrintHeader(protocol, frameType, compress, size, s);
                PrintRequestId(requestId, s);

                ToStringMode toStringMode = communicator.ToStringMode;
                s.Append("\nidentity = ");
                s.Append(identity.ToString(toStringMode));

                s.Append("\nfacet = ");
                if (facet.Length > 0)
                {
                    s.Append(StringUtil.EscapeString(facet, "", toStringMode));
                }

                s.Append("\noperation = ");
                s.Append(operation);

                OperationMode mode = isIdempotent ? OperationMode.Idempotent : OperationMode.Normal;
                s.Append("\noperation mode = ");
                s.Append((byte)mode);
                s.Append(mode switch
                {
                    OperationMode.Normal => " (non-idempotent)",
                    _ => " (idempotent)",
                });
Exemple #11
0
        /// <summary>Converts an object identity to a string, using the format specified by ToStringMode.</summary>
        /// <param name="mode">Specifies if and how non-printable ASCII characters are escaped in the result. See
        /// <see cref="ToStringMode"/>.</param>
        /// <returns>The string representation of the object identity.</returns>
        public string ToString(ToStringMode mode)
        {
            if (string.IsNullOrEmpty(Name))
            {
                return("");
            }
            Debug.Assert(Category != null);

            string escapedName = StringUtil.EscapeString(Name, mode, '/');

            if (Category.Length == 0)
            {
                return(escapedName);
            }
            else
            {
                string escapedCategory = StringUtil.EscapeString(Category, mode, '/');
                return($"{escapedCategory}/{escapedName}");
            }
        }
Exemple #12
0
        private static void PrintIdentityFacetOperation(System.IO.StringWriter s, InputStream str)
        {
            try
            {
                ToStringMode toStringMode = str.Communicator.ToStringMode;

                var identity = new Identity(str);
                s.Write("\nidentity = " + identity.ToString(toStringMode));

                string facet = str.ReadFacet();
                s.Write("\nfacet = ");
                if (facet.Length > 0)
                {
                    s.Write(IceUtilInternal.StringUtil.EscapeString(facet, "", toStringMode));
                }

                string operation = str.ReadString();
                s.Write("\noperation = " + operation);
            }
            catch (System.IO.IOException)
            {
                Debug.Assert(false);
            }
        }
Exemple #13
0
        EncodeChar(char c, StringBuilder sb, string?special, ToStringMode toStringMode)
        {
            switch (c)
            {
            case '\\':
            {
                sb.Append("\\\\");
                break;
            }

            case '\'':
            {
                sb.Append("\\'");
                break;
            }

            case '"':
            {
                sb.Append("\\\"");
                break;
            }

            case '\a':
            {
                if (toStringMode == ToStringMode.Compat)
                {
                    // Octal escape for compatibility with 3.6 and earlier
                    sb.Append("\\007");
                }
                else
                {
                    sb.Append("\\a");
                }
                break;
            }

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

            case '\f':
            {
                sb.Append("\\f");
                break;
            }

            case '\n':
            {
                sb.Append("\\n");
                break;
            }

            case '\r':
            {
                sb.Append("\\r");
                break;
            }

            case '\t':
            {
                sb.Append("\\t");
                break;
            }

            case '\v':
            {
                if (toStringMode == ToStringMode.Compat)
                {
                    // Octal escape for compatibility with 3.6 and earlier
                    sb.Append("\\013");
                }
                else
                {
                    sb.Append("\\v");
                }
                break;
            }

            default:
            {
                if (special != null && special.IndexOf(c) != -1)
                {
                    sb.Append('\\');
                    sb.Append(c);
                }
                else
                {
                    int i = (int)c;
                    if (i < 32 || i > 126)
                    {
                        if (toStringMode == ToStringMode.Compat)
                        {
                            //
                            // When ToStringMode=Compat, c is a UTF-8 byte
                            //
                            Debug.Assert(i < 256);

                            sb.Append('\\');
                            string octal = System.Convert.ToString(i, 8);
                            //
                            // Add leading zeros so that we avoid problems during
                            // decoding. For example, consider the encoded string
                            // \0013 (i.e., a character with value 1 followed by
                            // the character '3'). If the leading zeros were omitted,
                            // the result would be incorrectly interpreted by the
                            // decoder as a single character with value 11.
                            //
                            for (int j = octal.Length; j < 3; j++)
                            {
                                sb.Append('0');
                            }
                            sb.Append(octal);
                        }
                        else if (i < 32 || i == 127 || (toStringMode & ToStringMode.ASCII) != 0)
                        {
                            // append \\unnnn
                            sb.Append("\\u");
                            string hex = System.Convert.ToString(i, 16);
                            for (int j = hex.Length; j < 4; j++)
                            {
                                sb.Append('0');
                            }
                            sb.Append(hex);
                        }
                        else
                        {
                            // keep as is
                            sb.Append(c);
                        }
                    }
                    else
                    {
                        // printable ASCII character
                        sb.Append(c);
                    }
                }
                break;
            }
            }
        }
Exemple #14
0
 /// <summary>
 /// Creates new <see cref="IntXSettings" /> instance.
 /// </summary>
 /// <param name="globalSettings">IntX global settings to copy.</param>
 internal IntXSettings(IntXGlobalSettings globalSettings)
 {
     // Copy local settings from global ones
     _autoNormalize = globalSettings.AutoNormalize;
     _toStringMode  = globalSettings.ToStringMode;
 }
Exemple #15
0
 /// <summary>
 /// Creates new <see cref="IntXSettings" /> instance.
 /// </summary>
 /// <param name="globalSettings">IntX global settings to copy.</param>
 internal IntXSettings(IntXGlobalSettings globalSettings)
 {
     // Copy local settings from global ones
     _autoNormalize = globalSettings.AutoNormalize;
     _toStringMode = globalSettings.ToStringMode;
 }