String Formatting in Lua

-- Lua offers several ways to format strings. Here are some examples of
-- common string formatting tasks.

-- Define a table to represent a point structure
local point = {x = 1, y = 2}

-- Lua uses the string.format function for most formatting tasks
-- For basic string interpolation, we can use the .. operator
print("struct1: " .. tostring(point))

-- To include table field names, we can create a custom function
local function tableToString(t)
    local result = "{"
    for k, v in pairs(t) do
        result = result .. k .. ":" .. tostring(v) .. " "
    end
    return result .. "}"
end
print("struct2: " .. tableToString(point))

-- To print the type of a value, use type()
print("type: " .. type(point))

-- Formatting booleans
print("bool: " .. tostring(true))

-- Formatting integers
print("int: " .. string.format("%d", 123))

-- This prints a binary representation (Lua doesn't have a built-in binary formatter)
print("bin: " .. string.format("%s", string.format("%08b", 14)))

-- This prints the character corresponding to the given integer
print("char: " .. string.char(33))

-- Hex encoding
print("hex: " .. string.format("%x", 456))

-- Formatting floats
print("float1: " .. string.format("%.6f", 78.9))

-- Scientific notation
print("float2: " .. string.format("%.6e", 123400000.0))
print("float3: " .. string.format("%.6E", 123400000.0))

-- Basic string printing
print("str1: " .. "\"string\"")

-- To double-quote strings
print("str2: " .. string.format("%q", "\"string\""))

-- Hex representation of a string
print("str3: " .. (("hex this"):gsub(".", function(c) return string.format("%02x", string.byte(c)) end)))

-- Lua doesn't have direct pointer representations, so we'll skip that example

-- Controlling width and precision
print("width1: " .. string.format("|%6d|%6d|", 12, 345))
print("width2: " .. string.format("|%6.2f|%6.2f|", 1.2, 3.45))
print("width3: " .. string.format("|%-6.2f|%-6.2f|", 1.2, 3.45))
print("width4: " .. string.format("|%6s|%6s|", "foo", "b"))
print("width5: " .. string.format("|%-6s|%-6s|", "foo", "b"))

-- Lua's string.format returns a formatted string without printing it
local s = string.format("sprintf: a %s", "string")
print(s)

-- To print to different outputs, you can use io.write or io.stderr:write
io.stderr:write(string.format("io: an %s\n", "error"))

This Lua code demonstrates various string formatting techniques that are similar to those in the original Go example. Here are some key differences and explanations:

  1. Lua uses string.format for most formatting tasks, which is similar to C’s printf.
  2. Lua doesn’t have a built-in way to print struct-like tables with field names, so we created a custom tableToString function.
  3. Lua doesn’t have a built-in binary formatter, so we use a workaround with string.format.
  4. Pointer representation is not applicable in Lua, so that example is skipped.
  5. Lua doesn’t have a direct equivalent to Go’s Fprintf, but we can use io.stderr:write to write to standard error.

The output of this Lua script will be similar to the Go version, with slight differences due to language-specific features and limitations.