/// <summary> /// Initializes a new instance of the <see cref="StringFormatterCommandArgument"/> structure. /// </summary> /// <param name="arglist">The argument list which produced this argument.</param> /// <param name="text">The argument's text.</param> internal StringFormatterCommandArgument(ref StringFormatterCommandArguments arglist, StringSegment text) { this.Text = text; this.ArgumentListStart = arglist.Start; this.ArgumentListLength = arglist.Length; }
/// <summary> /// Handles a command which has an associated formatter argument value of <see cref="Int32"/> type. /// </summary> /// <param name="formatter">The formatter which is parsing the command.</param> /// <param name="output">The output buffer.</param> /// <param name="command">The name of the command being handled.</param> /// <param name="arguments">The arguments for the command being handled.</param> /// <param name="value">The command's associated value.</param> public virtual void HandleCommandInt32(StringFormatter formatter, StringBuilder output, StringSegment command, StringFormatterCommandArguments arguments, Int32 value) { throw new NotSupportedException(CoreStrings.FmtCmdInvalidForArgument.Format(command, typeof(Int32))); }
/// <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); }
/// <summary> /// Handles a command which does not have an associated formatter argument value (that is, a command /// in the format of {foo} rather than {0:foo}). /// </summary> /// <param name="formatter">The formatter which is parsing the command.</param> /// <param name="output">The output buffer.</param> /// <param name="command">The name of the command being handled.</param> /// <param name="arguments">The arguments for the command being handled.</param> public virtual void HandleCommand(StringFormatter formatter, StringBuilder output, StringSegment command, StringFormatterCommandArguments arguments) { throw new NotSupportedException(CoreStrings.FmtCmdInvalidWithoutArgument.Format(command)); }
/// <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); }
/// <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 ?? "???"); }
/// <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); }
/// <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); } }