/// <summary>
        /// Gets a formatted and localized error message of a <see cref="JsonErrorInfoParameter"/>.
        /// </summary>
        /// <param name="parameter">
        /// The <see cref="JsonErrorInfoParameter"/> to localize.
        /// </param>
        /// <param name="localizer">
        /// The <see cref="Localizer"/> to use for generating a display value.
        /// </param>
        /// <returns>
        /// The localized display value.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="parameter"/> and/or <paramref name="localizer"/> are null.
        /// </exception>
        public static string GetLocalizedDisplayValue(JsonErrorInfoParameter parameter, Localizer localizer)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }
            if (localizer == null)
            {
                throw new ArgumentNullException(nameof(localizer));
            }

            switch (parameter)
            {
            case JsonErrorInfoParameter <char> charParameter:
                char c = charParameter.Value;
                return(StringLiteral.CharacterMustBeEscaped(c)
                        ? $"'{StringLiteral.EscapedCharacterString(c)}'"
                        : $"'{c}'");

            case JsonErrorInfoParameter <string> stringParameter:
                return(stringParameter.Value == null
                        ? localizer.Localize(NullString)
                        : $"\"{stringParameter.Value}\"");

            default:
                return(parameter.UntypedValue == null
                        ? localizer.Localize(NullString)
                        : localizer.Localize(UntypedObjectString, parameter.UntypedValue.ToString()));
            }
        }
Example #2
0
        internal void AppendString(string value)
        {
            outputBuilder.Append(StringLiteral.QuoteCharacter);

            if (value != null)
            {
                // Save on Append() operations by appending escape-char-less substrings
                // that are as large as possible.
                int firstNonEscapedCharPosition = 0;

                for (int i = 0; i < value.Length; i++)
                {
                    char c = value[i];
                    if (StringLiteral.CharacterMustBeEscaped(c))
                    {
                        // Non-empty substring between this character and the last?
                        if (firstNonEscapedCharPosition < i)
                        {
                            outputBuilder.Append(
                                value,
                                firstNonEscapedCharPosition,
                                i - firstNonEscapedCharPosition);
                        }

                        // Append the escape sequence.
                        outputBuilder.Append(StringLiteral.EscapedCharacterString(c));

                        firstNonEscapedCharPosition = i + 1;
                    }
                }

                if (firstNonEscapedCharPosition < value.Length)
                {
                    outputBuilder.Append(
                        value,
                        firstNonEscapedCharPosition,
                        value.Length - firstNonEscapedCharPosition);
                }
            }

            outputBuilder.Append(StringLiteral.QuoteCharacter);
        }
Example #3
0
 /// <summary>
 /// Creates a <see cref="PgnErrorInfo"/> for illegal control characters in tag values.
 /// </summary>
 /// <param name="illegalCharacter">
 /// The illegal control character.
 /// </param>
 /// <param name="position">
 /// The position of the illegal control character relative to the start position of the tag value.
 /// </param>
 public static PgnErrorInfo IllegalControlCharacterError(char illegalCharacter, int position)
 => new PgnErrorInfo(PgnErrorCode.IllegalControlCharacterInTagValue, position, 1, new[] { StringLiteral.EscapedCharacterString(illegalCharacter) });