String Formatting in AngelScript

class Point {
    int x;
    int y;
}

void main() {
    // AngelScript offers several ways to format strings
    // For example, this prints an instance of our Point class
    Point p;
    p.x = 1;
    p.y = 2;
    Print("struct1: {" + p.x + ", " + p.y + "}\n");

    // To print the type of a value, we can use the typeid keyword
    Print("type: " + typeid(p).GetName() + "\n");

    // Formatting booleans is straightforward
    Print("bool: " + true + "\n");

    // There are several ways to format integers
    Print("int: " + 123 + "\n");

    // This prints a binary representation
    Print("bin: " + ToBinaryString(14) + "\n");

    // This prints the character corresponding to the given integer
    Print("char: " + String(char(33)) + "\n");

    // For hex encoding, we can use a custom function
    Print("hex: " + ToHexString(456) + "\n");

    // For floating-point numbers, we can use string formatting
    Print("float1: " + FormatFloat(78.9) + "\n");

    // Scientific notation can be achieved with a custom function
    Print("float2: " + FormatScientific(123400000.0) + "\n");

    // For basic string printing
    Print("str1: \"string\"\n");

    // To print a representation of a pointer, we can use the address-of operator
    Print("pointer: " + AddressOf(p) + "\n");

    // When formatting numbers, you might want to control the width and precision
    // This can be achieved with custom functions
    Print("width1: |" + PadLeft(12, 6) + "|" + PadLeft(345, 6) + "|\n");

    // For floats with specific width and precision
    Print("width2: |" + FormatFloat(1.2, 6, 2) + "|" + FormatFloat(3.45, 6, 2) + "|\n");

    // For left-justified formatting
    Print("width3: |" + PadRight(FormatFloat(1.2, 2), 6) + "|" + PadRight(FormatFloat(3.45, 2), 6) + "|\n");

    // Controlling width when formatting strings
    Print("width4: |" + PadLeft("foo", 6) + "|" + PadLeft("b", 6) + "|\n");

    // Left-justified string formatting
    Print("width5: |" + PadRight("foo", 6) + "|" + PadRight("b", 6) + "|\n");

    // In AngelScript, we typically use string concatenation or interpolation for formatting
    string s = "sprintf: a " + "string";
    Print(s + "\n");

    // To print to other outputs, you would typically use a different function or redirect the output
    Print("io: an error\n");
}

// Helper functions for formatting

string ToBinaryString(int n) {
    string result;
    while (n > 0) {
        result = ((n & 1) == 1 ? "1" : "0") + result;
        n >>= 1;
    }
    return result.IsEmpty() ? "0" : result;
}

string ToHexString(int n) {
    string result;
    while (n > 0) {
        int digit = n & 0xF;
        result = String(char(digit < 10 ? digit + 48 : digit + 55)) + result;
        n >>= 4;
    }
    return result.IsEmpty() ? "0" : result;
}

string FormatFloat(float f, int precision = 6) {
    return "" + f; // This is a simplification, actual implementation would respect precision
}

string FormatScientific(float f) {
    // This is a simplified version, actual implementation would be more complex
    return "" + f;
}

string PadLeft(int n, int width) {
    string s = "" + n;
    while (s.length() < width) s = " " + s;
    return s;
}

string PadRight(string s, int width) {
    while (s.length() < width) s += " ";
    return s;
}

string FormatFloat(float f, int width, int precision) {
    string s = FormatFloat(f, precision);
    return PadLeft(s, width);
}

string AddressOf(ref Point p) {
    // This is a placeholder, actual implementation would depend on how AngelScript handles memory addresses
    return "0x12345678";
}

This AngelScript code demonstrates various string formatting techniques. Note that AngelScript doesn’t have built-in formatting functions as extensive as some other languages, so we’ve had to implement several helper functions to achieve similar functionality.

Some key differences and notes:

  1. AngelScript uses Print for console output instead of fmt.Printf.
  2. String concatenation is used extensively for formatting.
  3. Custom functions are implemented for more complex formatting tasks like binary and hexadecimal conversion.
  4. Pointer representation is simplified, as AngelScript’s handling of memory addresses may differ.
  5. Some advanced formatting options may not be directly available and would require more complex custom implementations.

Remember to compile and run this code according to your AngelScript environment’s specifications.