Browse Source

Add `TextTemplate` ability to paste formatted args

pull/8/head
Anton Tarasenko 2 years ago
parent
commit
f1532ce3a7
  1. 69
      sources/Text/TextTemplate.uc

69
sources/Text/TextTemplate.uc

@ -133,6 +133,7 @@ const CODEPOINT_AMPERSAND = 38; // '&'
protected function Finalizer() protected function Finalizer()
{ {
local int i; local int i;
// Clear user input // Clear user input
Reset(); Reset();
// Clear numeric labels // Clear numeric labels
@ -176,6 +177,7 @@ public final function bool IsInitialized()
public final function Initialize_S(string input) public final function Initialize_S(string input)
{ {
local MutableText wrapper; local MutableText wrapper;
wrapper = _.text.FromStringM(input); wrapper = _.text.FromStringM(input);
Initialize(wrapper); Initialize(wrapper);
_.memory.Free(wrapper); _.memory.Free(wrapper);
@ -247,6 +249,7 @@ private final function MatchTextLabel(
private final function MatchNumericLabel(Parser parser) private final function MatchNumericLabel(Parser parser)
{ {
local int nextNumericLabel; local int nextNumericLabel;
if (parser.MInteger(nextNumericLabel).Ok()) { if (parser.MInteger(nextNumericLabel).Ok()) {
labels[labels.length] = MakeNumericLabel(nextNumericLabel); labels[labels.length] = MakeNumericLabel(nextNumericLabel);
} }
@ -266,6 +269,7 @@ private final function NormalizeArguments()
local int nextArgument; local int nextArgument;
local int lowestArgument, lowestArgumentIndex; local int lowestArgument, lowestArgumentIndex;
local array<int> argumentsOrder, normalizedArguments; local array<int> argumentsOrder, normalizedArguments;
for (i = 0; i < labels.length; i += 1) for (i = 0; i < labels.length; i += 1)
{ {
if (labels[i].textLabel == none) if (labels[i].textLabel == none)
@ -305,6 +309,7 @@ private final function NormalizeArguments()
private final function Label MakeTextLabel(MutableText Label2) private final function Label MakeTextLabel(MutableText Label2)
{ {
local Label result; local Label result;
result.textLabel = Label2; result.textLabel = Label2;
result.insertionIndex = parts.length; result.insertionIndex = parts.length;
return result; return result;
@ -313,6 +318,7 @@ private final function Label MakeTextLabel(MutableText Label2)
private final function Label MakeNumericLabel(int Label2) private final function Label MakeNumericLabel(int Label2)
{ {
local Label result; local Label result;
result.numberLabel = Label2; result.numberLabel = Label2;
result.insertionIndex = parts.length; result.insertionIndex = parts.length;
return result; return result;
@ -412,6 +418,7 @@ public final function array<Text> GetTextArgs()
local int i, j; local int i, j;
local bool duplicate; local bool duplicate;
local array<Text> result; local array<Text> result;
if (!initialized) { if (!initialized) {
return result; return result;
} }
@ -448,6 +455,7 @@ public final function array<Text> GetTextArgs()
public final function TextTemplate Arg_S(string argument) public final function TextTemplate Arg_S(string argument)
{ {
local MutableText wrapper; local MutableText wrapper;
wrapper = _.text.FromStringM(argument); wrapper = _.text.FromStringM(argument);
Arg(wrapper); Arg(wrapper);
_.memory.Free(wrapper); _.memory.Free(wrapper);
@ -459,18 +467,32 @@ public final function TextTemplate Arg_S(string argument)
* *
* If all numeric arguments were already filled this call will do nothing. * If all numeric arguments were already filled this call will do nothing.
* *
* @param argument Value to replace next numeric argument in the caller * @param argument Value to replace next numeric argument in
* `TextTemplate`. `none` equals to an empty text. * the caller `TextTemplate`. `none` equals to an empty text.
* @param ignoreFormatting `false` means `argument` will be inserted along
* with its formatting information. Setting this option to `true` will
* insert it into template as plain text without any formatting
* (which should also be faster).
* @return Reference to the caller `TextTemplate` to allow for method chaining. * @return Reference to the caller `TextTemplate` to allow for method chaining.
*/ */
public final function TextTemplate Arg(BaseText argument) public final function TextTemplate Arg(
BaseText argument,
optional bool ignoreFormatting)
{ {
if (numericLabelsAmount <= numericArguments.length) { if (numericLabelsAmount <= numericArguments.length) {
return self; return self;
} }
if (argument != none) { if (argument != none)
{
if (ignoreFormatting) {
numericArguments[numericArguments.length] = argument.Copy(); numericArguments[numericArguments.length] = argument.Copy();
} }
else
{
numericArguments[numericArguments.length] =
argument.ToFormattedText();
}
}
else { else {
numericArguments[numericArguments.length] = none; numericArguments[numericArguments.length] = none;
} }
@ -489,6 +511,7 @@ public final function TextTemplate Arg(BaseText argument)
public final function TextTemplate ArgInt(int argument) public final function TextTemplate ArgInt(int argument)
{ {
local MutableText textRepresentation; local MutableText textRepresentation;
textRepresentation = _.text.FromIntM(argument); textRepresentation = _.text.FromIntM(argument);
Arg(textRepresentation); Arg(textRepresentation);
_.memory.Free(textRepresentation); _.memory.Free(textRepresentation);
@ -507,6 +530,7 @@ public final function TextTemplate ArgInt(int argument)
public final function TextTemplate ArgFloat(float argument) public final function TextTemplate ArgFloat(float argument)
{ {
local MutableText textRepresentation; local MutableText textRepresentation;
textRepresentation = _.text.FromFloatM(argument); textRepresentation = _.text.FromFloatM(argument);
Arg(textRepresentation); Arg(textRepresentation);
_.memory.Free(textRepresentation); _.memory.Free(textRepresentation);
@ -525,6 +549,7 @@ public final function TextTemplate ArgFloat(float argument)
public final function TextTemplate ArgBool(bool argument) public final function TextTemplate ArgBool(bool argument)
{ {
local MutableText textRepresentation; local MutableText textRepresentation;
textRepresentation = _.text.FromBoolM(argument); textRepresentation = _.text.FromBoolM(argument);
Arg(textRepresentation); Arg(textRepresentation);
_.memory.Free(textRepresentation); _.memory.Free(textRepresentation);
@ -543,6 +568,7 @@ public final function TextTemplate ArgBool(bool argument)
public final function TextTemplate ArgClass(class<Object> argument) public final function TextTemplate ArgClass(class<Object> argument)
{ {
local MutableText textRepresentation; local MutableText textRepresentation;
textRepresentation = _.text.FromClassM(argument); textRepresentation = _.text.FromClassM(argument);
Arg(textRepresentation); Arg(textRepresentation);
_.memory.Free(textRepresentation); _.memory.Free(textRepresentation);
@ -563,6 +589,7 @@ public final function TextTemplate ArgClass(class<Object> argument)
public final function TextTemplate TextArg_S(string label, string argument) public final function TextTemplate TextArg_S(string label, string argument)
{ {
local MutableText labelWrapper, argumentWrapper; local MutableText labelWrapper, argumentWrapper;
labelWrapper = _.text.FromStringM(label); labelWrapper = _.text.FromStringM(label);
argumentWrapper = _.text.FromStringM(argument); argumentWrapper = _.text.FromStringM(argument);
TextArg(labelWrapper, argumentWrapper); TextArg(labelWrapper, argumentWrapper);
@ -578,13 +605,21 @@ public final function TextTemplate TextArg_S(string label, string argument)
* previous value. * previous value.
* *
* @param label Label of the text argument to replace. * @param label Label of the text argument to replace.
* @param argument Value to replace specified text argument in the caller * @param argument Value to replace specified text argument in
* `TextTemplate` with. `none` is equal to an empty text. * the caller `TextTemplate` with. `none` is equal to an empty text.
* @param ignoreFormatting `false` means `argument` will be inserted along
* with its formatting information. Setting this option to `true` will
* insert it into template as plain text without any formatting
* (which should also be faster).
* @return Reference to the caller `TextTemplate` to allow for method chaining. * @return Reference to the caller `TextTemplate` to allow for method chaining.
*/ */
public final function TextTemplate TextArg(BaseText label, BaseText argument) public final function TextTemplate TextArg(
BaseText label,
BaseText argument,
optional bool ignoreFormatting)
{ {
local int i; local int i;
if (label == none) { if (label == none) {
return self; return self;
} }
@ -608,9 +643,15 @@ public final function TextTemplate TextArg(BaseText label, BaseText argument)
else { else {
textLabels[textLabels.length] = none; textLabels[textLabels.length] = none;
} }
if (argument != none) { if (argument != none)
{
if (ignoreFormatting) {
textArguments[i] = argument.Copy(); textArguments[i] = argument.Copy();
} }
else {
textArguments[i] = argument.ToFormattedText();
}
}
else { else {
textArguments[i] = none; textArguments[i] = none;
} }
@ -633,6 +674,7 @@ public final function TextTemplate TextArgInt_S(string label, int argument)
{ {
local MutableText labelWrapper; local MutableText labelWrapper;
local MutableText textRepresentation; local MutableText textRepresentation;
labelWrapper = _.text.FromStringM(label); labelWrapper = _.text.FromStringM(label);
textRepresentation = _.text.FromIntM(argument); textRepresentation = _.text.FromIntM(argument);
TextArg(labelWrapper, textRepresentation); TextArg(labelWrapper, textRepresentation);
@ -656,6 +698,7 @@ public final function TextTemplate TextArgInt_S(string label, int argument)
public final function TextTemplate TextArgInt(BaseText label, int argument) public final function TextTemplate TextArgInt(BaseText label, int argument)
{ {
local MutableText textRepresentation; local MutableText textRepresentation;
textRepresentation = _.text.FromIntM(argument); textRepresentation = _.text.FromIntM(argument);
TextArg(label, textRepresentation); TextArg(label, textRepresentation);
_.memory.Free(textRepresentation); _.memory.Free(textRepresentation);
@ -678,6 +721,7 @@ public final function TextTemplate TextArgFloat_S(string label, float argument)
{ {
local MutableText labelWrapper; local MutableText labelWrapper;
local MutableText textRepresentation; local MutableText textRepresentation;
labelWrapper = _.text.FromStringM(label); labelWrapper = _.text.FromStringM(label);
textRepresentation = _.text.FromFloatM(argument); textRepresentation = _.text.FromFloatM(argument);
TextArg(labelWrapper, textRepresentation); TextArg(labelWrapper, textRepresentation);
@ -701,6 +745,7 @@ public final function TextTemplate TextArgFloat_S(string label, float argument)
public final function TextTemplate TextArgFloat(BaseText label, float argument) public final function TextTemplate TextArgFloat(BaseText label, float argument)
{ {
local MutableText textRepresentation; local MutableText textRepresentation;
textRepresentation = _.text.FromFloatM(argument); textRepresentation = _.text.FromFloatM(argument);
TextArg(label, textRepresentation); TextArg(label, textRepresentation);
_.memory.Free(textRepresentation); _.memory.Free(textRepresentation);
@ -723,6 +768,7 @@ public final function TextTemplate TextArgBool_S(string label, bool argument)
{ {
local MutableText labelWrapper; local MutableText labelWrapper;
local MutableText textRepresentation; local MutableText textRepresentation;
labelWrapper = _.text.FromStringM(label); labelWrapper = _.text.FromStringM(label);
textRepresentation = _.text.FromBoolM(argument); textRepresentation = _.text.FromBoolM(argument);
TextArg(labelWrapper, textRepresentation); TextArg(labelWrapper, textRepresentation);
@ -746,6 +792,7 @@ public final function TextTemplate TextArgBool_S(string label, bool argument)
public final function TextTemplate TextArgBool(BaseText label, bool argument) public final function TextTemplate TextArgBool(BaseText label, bool argument)
{ {
local MutableText textRepresentation; local MutableText textRepresentation;
textRepresentation = _.text.FromBoolM(argument); textRepresentation = _.text.FromBoolM(argument);
TextArg(label, textRepresentation); TextArg(label, textRepresentation);
_.memory.Free(textRepresentation); _.memory.Free(textRepresentation);
@ -769,6 +816,7 @@ public final function TextTemplate TextArgClass_S(
{ {
local MutableText labelWrapper; local MutableText labelWrapper;
local MutableText textRepresentation; local MutableText textRepresentation;
labelWrapper = _.text.FromStringM(label); labelWrapper = _.text.FromStringM(label);
textRepresentation = _.text.FromClassM(argument); textRepresentation = _.text.FromClassM(argument);
TextArg(labelWrapper, textRepresentation); TextArg(labelWrapper, textRepresentation);
@ -793,6 +841,7 @@ public final function TextTemplate TextArgClass(
class<Object> argument) class<Object> argument)
{ {
local MutableText textRepresentation; local MutableText textRepresentation;
textRepresentation = _.text.FromClassM(argument); textRepresentation = _.text.FromClassM(argument);
TextArg(label, textRepresentation); TextArg(label, textRepresentation);
_.memory.Free(textRepresentation); _.memory.Free(textRepresentation);
@ -810,6 +859,7 @@ private final function Text BorrowNumericArg(int index)
private final function Text BorrowTextArg(BaseText Label) private final function Text BorrowTextArg(BaseText Label)
{ {
local int i; local int i;
if (Label == none) { if (Label == none) {
return none; return none;
} }
@ -877,6 +927,7 @@ public final function MutableText CollectM()
local int i, labelCounter; local int i, labelCounter;
local Label nextLabel; local Label nextLabel;
local MutableText builder; local MutableText builder;
if (!initialized) { if (!initialized) {
return none; return none;
} }
@ -930,6 +981,7 @@ public final function Text CollectFormatted()
{ {
local Text result; local Text result;
local MutableText source; local MutableText source;
source = CollectM(); source = CollectM();
if (source == none) { if (source == none) {
return none; return none;
@ -957,6 +1009,7 @@ public final function Text CollectFormatted()
public final function MutableText CollectFormattedM() public final function MutableText CollectFormattedM()
{ {
local MutableText result, source; local MutableText result, source;
source = CollectM(); source = CollectM();
if (source == none) { if (source == none) {
return none; return none;

Loading…
Cancel
Save