/// <inheritdoc/>
            public override void HandleCommandUInt32(StringFormatter formatter,
                                                     StringBuilder output, StringSegment command, StringFormatterCommandArguments arguments, UInt32 value)
            {
                var padding = arguments.GetArgument(0).GetValueAsInt32();

                output.Concat(value, (UInt32)padding);
            }
示例#2
0
            /// <inheritdoc/>
            public override void HandleCommandDouble(StringFormatter formatter,
                                                     StringBuilder output, StringSegment command, StringFormatterCommandArguments arguments, Double value)
            {
                var decimals = arguments.GetArgument(0).GetValueAsInt32();

                output.Concat((Single)value, (UInt32)decimals);
            }
            /// <inheritdoc/>
            public override void HandleCommandLocalizedString(StringFormatter formatter,
                                                              StringBuilder output, StringSegment command, StringFormatterCommandArguments arguments, LocalizedString value)
            {
                if (value == null)
                {
                    throw new FormatException(CoreStrings.FmtCmdInvalidForGeneratedStrings.Format("variant"));
                }

                var variantName = arguments.GetArgument(0).Text;
                var variant     = value.GetVariant(ref variantName);

                output.Append(variant);
            }
            /// <inheritdoc/>
            public override void HandleCommandLocalizedString(StringFormatter formatter,
                                                              StringBuilder output, StringSegment command, StringFormatterCommandArguments arguments, LocalizedString value)
            {
                var targetArgumentArg   = arguments.GetArgument(0);
                var targetArgumentIndex = targetArgumentArg.GetValueAsInt32();
                var targetMatchRuleArg  = arguments.GetNextArgument(ref targetArgumentArg);
                var targetMatchRule     = targetMatchRuleArg.Text;

                // Make sure our target is a localized string variant.
                var target = formatter.GetArgument(targetArgumentIndex);

                if (target.Reference == null || !(target.Reference is LocalizedStringVariant))
                {
                    throw new FormatException(CoreStrings.FmtCmdInvalidForArgument.Format("match", target.Reference?.GetType()));
                }

                // Run the specified match evaluator.
                var match = Localization.MatchVariant(value, (LocalizedStringVariant)target.Reference, targetMatchRule);

                output.Append(match ?? "???");
            }
示例#5
0
        /// <summary>
        /// Attempts to process a format specifier.
        /// </summary>
        /// <param name="input">The input string.</param>
        /// <param name="output">The output buffer.</param>
        /// <param name="ix">The index at which the specifier begins.</param>
        /// <param name="length">The length of the specifier.</param>
        private void ProcessFormatSpecifier(String input, StringBuilder output, Int32 ix, Int32 length)
        {
            // Parse the command string to pull out its name and arguments.
            var argument  = default(StringFormatterArgument);
            var cmdStart  = ix + 1;
            var cmdLength = 0;

            for (int i = 1; i < length; i++)
            {
                var c = input[ix + i];
                if (c == '}')
                {
                    break;
                }
                cmdLength++;
            }

            var cmdName = default(StringSegment);
            var cmdArgs = new StringFormatterCommandArguments(input, cmdStart, cmdLength);

            var arg0               = cmdArgs.GetArgument(0);
            var arg0Text           = arg0.Text;
            var arg0Value          = 0;
            var arg0ValueIsInteger = arg0.TryGetValueAsInt32(out arg0Value);

            // If the command starts with a reference to an argument, parse
            // that out so we can get at the command name.
            var referencesGenerated = arg0Text == "?";
            var referencesArgument  = referencesGenerated || arg0ValueIsInteger;

            if (referencesArgument)
            {
                if (cmdArgs.Count > 1)
                {
                    cmdName = cmdArgs.GetArgument(1).Text;
                    cmdArgs = cmdArgs.Discard(2);
                }
            }
            else
            {
                cmdName = arg0Text;
                cmdArgs = cmdArgs.Discard(1);
            }

            // Determine if we have a custom handler for this command.
            var matchingCommandHandler = default(StringFormatterCommandHandler);

            if (!cmdName.IsEmpty)
            {
                for (int i = commandHandlers.Count - 1; i >= 0; i--)
                {
                    var commandHandler = commandHandlers[i];
                    if (commandHandler.CanHandleCommand(cmdName))
                    {
                        matchingCommandHandler = commandHandler;
                        break;
                    }
                }

                if (matchingCommandHandler == null)
                {
                    throw new FormatException(CoreStrings.FmtCmdUnrecognized.Format(cmdName));
                }
            }

            // If we can't convert the first argument to an integer, just write out the whole command string.
            if (!referencesArgument)
            {
                if (matchingCommandHandler != null)
                {
                    matchingCommandHandler.HandleCommand(this, output, cmdName, cmdArgs);
                    return;
                }

                output.AppendSubstring(input, ix, length);
                return;
            }

            // Append the selected argument to the buffer.
            var cmdInfo = new StringFormatterCommandInfo(cmdName, cmdArgs, matchingCommandHandler);

            if (referencesGenerated)
            {
                argument = new StringFormatterArgument();
                ArgumentHandler_LocalizedString(this,
                                                input, output, arg0Text.Start + arg0Text.Length, ref argument, ref cmdInfo);
            }
            else
            {
                argument = GetArgument(arg0Value);
                AppendArgument(input, output, arg0Text.Start + arg0Text.Length, ref argument, ref cmdInfo);
            }
        }