Software / code / prosody
Comparison
util/format.lua @ 9687:8c92ef4270c9
util.format: Use pack from util.table
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sat, 08 Dec 2018 16:35:39 +0100 |
| parent | 9656:3da6cc927ee6 |
| child | 9693:6ed0d6224d64 |
comparison
equal
deleted
inserted
replaced
| 9686:e52e4e6e7ffb | 9687:8c92ef4270c9 |
|---|---|
| 1 -- | 1 -- |
| 2 -- A string.format wrapper that gracefully handles invalid arguments | 2 -- A string.format wrapper that gracefully handles invalid arguments |
| 3 -- | 3 -- |
| 4 | 4 |
| 5 local tostring = tostring; | 5 local tostring = tostring; |
| 6 local select = select; | |
| 7 local unpack = table.unpack or unpack; -- luacheck: ignore 113/unpack | 6 local unpack = table.unpack or unpack; -- luacheck: ignore 113/unpack |
| 7 local pack = require "util.table".pack; -- TODO table.pack in 5.2+ | |
| 8 local type = type; | 8 local type = type; |
| 9 | 9 |
| 10 local function format(formatstring, ...) | 10 local function format(formatstring, ...) |
| 11 local args, args_length = { ... }, select('#', ...); | 11 local args = pack(...); |
| 12 local args_length = args.n; | |
| 12 | 13 |
| 13 -- format specifier spec: | 14 -- format specifier spec: |
| 14 -- 1. Start: '%%' | 15 -- 1. Start: '%%' |
| 15 -- 2. Flags: '[%-%+ #0]' | 16 -- 2. Flags: '[%-%+ #0]' |
| 16 -- 3. Width: '%d?%d?' | 17 -- 3. Width: '%d?%d?' |