String Formatting in UnrealScript
class StringFormatting extends Object;
struct Point
{
var int X, Y;
};
static function Main()
{
local Point P;
local string S;
P.X = 1;
P.Y = 2;
// UnrealScript doesn't have a direct equivalent to Go's fmt.Printf,
// so we'll use `Log` function for output and string concatenation for formatting
// Printing struct values
Log("struct1: " $ P);
Log("struct2: X=" $ P.X $ " Y=" $ P.Y);
// UnrealScript doesn't have a direct equivalent to %#v in Go
Log("struct3: (Point) X=" $ P.X $ " Y=" $ P.Y);
// Printing types
Log("type: " $ P.Class);
// Formatting booleans
Log("bool: " $ True);
// Formatting integers
Log("int: " $ 123);
// UnrealScript doesn't have built-in binary formatting
// Log("bin: " $ 14);
// Printing characters
Log("char: " $ Chr(33));
// Hexadecimal formatting
Log("hex: " $ Hex(456));
// Floating-point formatting
Log("float1: " $ 78.9);
// UnrealScript doesn't have built-in scientific notation formatting
// Log("float2: " $ 123400000.0);
// Log("float3: " $ 123400000.0);
// String formatting
Log("str1: " $ "\"string\"");
Log("str2: " $ "\"string\"");
// UnrealScript doesn't have built-in hex encoding for strings
// Log("str3: " $ "hex this");
// Pointer representation (not directly available in UnrealScript)
// Log("pointer: " $ &P);
// Width formatting isn't directly supported in UnrealScript
// We can simulate it with padding functions
// Sprintf-like functionality
S = "sprintf: a " $ "string";
Log(S);
// UnrealScript doesn't have direct file output, so we'll use Log for all output
Log("io: an error");
}
// Helper function to pad strings (simulating width formatting)
static function string PadRight(string Str, int Width)
{
while (Len(Str) < Width)
{
Str = Str $ " ";
}
return Str;
}
// Helper function for left-padding
static function string PadLeft(string Str, int Width)
{
while (Len(Str) < Width)
{
Str = " " $ Str;
}
return Str;
}
This UnrealScript code demonstrates various string formatting techniques, adapting the concepts from the original Go example. Here are some key points about the translation:
UnrealScript doesn’t have a direct equivalent to Go’s
fmt.Printf
, so we use theLog
function for output and string concatenation for formatting.Some Go formatting options (like binary representation, scientific notation, or hex encoding of strings) don’t have direct equivalents in UnrealScript and are commented out.
Width formatting isn’t directly supported in UnrealScript, so we’ve added helper functions
PadRight
andPadLeft
to simulate this functionality.UnrealScript doesn’t have pointers in the same way as Go, so pointer-related formatting is omitted.
File output isn’t directly available in UnrealScript in the same way as in Go, so all output uses the
Log
function.The
Main
function is defined as a static function in theStringFormatting
class, as UnrealScript doesn’t have standalone functions.
This code provides a close approximation of the string formatting capabilities demonstrated in the Go example, adapted to the constraints and conventions of UnrealScript.