Esempio n. 1
0
        /// <summary>
        /// Appends a formatter argument to the specified output buffer.
        /// </summary>
        private void AppendArgument(String input, StringBuilder output, Int32 position,
                                    ref StringFormatterArgument argument, ref StringFormatterCommandInfo cmdInfo)
        {
            StringFormatterArgumentHandler handler;

            if (argumentHandlers.TryGetValue(argument.Type, out handler))
            {
                handler(this, input, output, position, ref argument, ref cmdInfo);
            }
            else
            {
                if (argument.Reference != null)
                {
                    output.Append(argument.Reference.ToString());
                }
                else
                {
                    output.Append("???");
                }
            }
        }
Esempio n. 2
0
        private static void ArgumentHandler_LocalizedString(StringFormatter formatter,
                                                            String input, StringBuilder output, Int32 position, ref StringFormatterArgument argument, ref StringFormatterCommandInfo cmd)
        {
            var localizedString = (LocalizedString)argument.Reference;

            if (cmd.CommandHandler != null)
            {
                cmd.CommandHandler.HandleCommandLocalizedString(formatter,
                                                                output, cmd.CommandName, cmd.CommandArguments, localizedString);
            }
            else
            {
                output.Append((String)localizedString ?? "???");
            }
        }
Esempio n. 3
0
        private static void ArgumentHandler_Double(StringFormatter formatter,
                                                   String input, StringBuilder output, Int32 position, ref StringFormatterArgument argument, ref StringFormatterCommandInfo cmd)
        {
            var raw   = argument.Value;
            var value = *(Double *)&raw;

            if (cmd.CommandHandler != null)
            {
                cmd.CommandHandler.HandleCommandDouble(formatter,
                                                       output, cmd.CommandName, cmd.CommandArguments, value);
            }
            else
            {
                output.Concat((Single)value);
            }
        }
Esempio n. 4
0
        private static void ArgumentHandler_Char(StringFormatter formatter,
                                                 String input, StringBuilder output, Int32 position, ref StringFormatterArgument argument, ref StringFormatterCommandInfo cmd)
        {
            var raw   = argument.Value;
            var value = *(char *)&raw;

            if (cmd.CommandHandler != null)
            {
                cmd.CommandHandler.HandleCommandChar(formatter,
                                                     output, cmd.CommandName, cmd.CommandArguments, value);
            }
            else
            {
                output.Append(value);
            }
        }
Esempio n. 5
0
        private static void ArgumentHandler_Boolean(StringFormatter formatter,
                                                    String input, StringBuilder output, Int32 position, ref StringFormatterArgument argument, ref StringFormatterCommandInfo cmd)
        {
            var raw   = argument.Value;
            var value = *(bool *)&raw;

            if (cmd.CommandHandler != null)
            {
                cmd.CommandHandler.HandleCommandBoolean(formatter,
                                                        output, cmd.CommandName, cmd.CommandArguments, value);
            }
            else
            {
                output.Append(value ? Boolean.TrueString : Boolean.FalseString);
            }
        }
Esempio n. 6
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(NucleusStrings.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);
            }
        }