mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Implement STRFMT and more printf-like format specifiers for string interpolation (#646)
Fixes #570 Fixes #178 Use errors for inapplicable format spec flags instead of -Wstring-format
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
ERROR: bracketed-symbols.asm(16):
|
||||
Print types are only allowed for numbers
|
||||
Formatting string as type 'X'
|
||||
ERROR: bracketed-symbols.asm(20):
|
||||
"Label" does not have a constant value
|
||||
ERROR: bracketed-symbols.asm(21):
|
||||
|
||||
24
test/asm/strfmt.asm
Normal file
24
test/asm/strfmt.asm
Normal file
@@ -0,0 +1,24 @@
|
||||
VAL EQUS STRFMT("Hello %s! I am %d years old today!", "world", $f)
|
||||
PRINTT "{VAL}\n"
|
||||
|
||||
N = -42
|
||||
PRINTT STRFMT("signed %010d == unsigned %010u\n", N, N)
|
||||
|
||||
N = 112
|
||||
FMT EQUS "X"
|
||||
PRINTT STRFMT("\tdb %#03{s:FMT} %% 26\t; %#03{FMT}\n", N, N % 26)
|
||||
|
||||
TEMPLATE EQUS "\"%s are %s\\n\""
|
||||
PRINTT STRFMT(TEMPLATE, "roses", "red")
|
||||
PRINTT STRFMT(TEMPLATE, "violets", "blue")
|
||||
PRINTT STRFMT(TEMPLATE, "void", 0, "extra")
|
||||
|
||||
PRINTT STRCAT(STRFMT(STRFMT("%%%s.%d%s", "", 9, "f"), _PI), \
|
||||
STRFMT(" ~ %s\n", STRFMT("%s%x", "thr", 238)))
|
||||
|
||||
PRINTT STRFMT("%d eol %", 1)
|
||||
PRINTT "\n"
|
||||
|
||||
PRINTT STRFMT("invalid %w spec\n", 42)
|
||||
|
||||
PRINTT STRFMT("one=%d two=%d three=%d\n", 1)
|
||||
11
test/asm/strfmt.err
Normal file
11
test/asm/strfmt.err
Normal file
@@ -0,0 +1,11 @@
|
||||
ERROR: strfmt.asm(14):
|
||||
Formatting number as type 's'
|
||||
ERROR: strfmt.asm(14):
|
||||
STRFMT: 1 unformatted argument(s)
|
||||
ERROR: strfmt.asm(19):
|
||||
STRFMT: Illegal '%' at end of format string
|
||||
ERROR: strfmt.asm(22):
|
||||
STRFMT: Invalid format spec for argument 1
|
||||
ERROR: strfmt.asm(24):
|
||||
STRFMT: Not enough arguments for format spec
|
||||
error: Assembly aborted (5 errors)!
|
||||
10
test/asm/strfmt.out
Normal file
10
test/asm/strfmt.out
Normal file
@@ -0,0 +1,10 @@
|
||||
Hello world! I am 15 years old today!
|
||||
signed -000000042 == unsigned 4294967254
|
||||
db $70 % 26 ; $08
|
||||
roses are red
|
||||
violets are blue
|
||||
void are 0
|
||||
3.141586304 ~ three
|
||||
1 eol %
|
||||
invalid % spec
|
||||
one=1 two=% three=%
|
||||
15
test/asm/string-formatting.asm
Normal file
15
test/asm/string-formatting.asm
Normal file
@@ -0,0 +1,15 @@
|
||||
n equ 300
|
||||
m equ -42
|
||||
f equ -123.0456
|
||||
s equs "hello"
|
||||
|
||||
printt "<{ -6d:n}> <{+06u:n}> <{5x:n}> <{#16b:n}>\n"
|
||||
printt "<{u:m}> <{+3d:m}> <{#016o:m}>\n"
|
||||
printt "<{f:_PI}> <{06f:f}> <{.10f:f}>\n"
|
||||
printt "<{#-10s:s}> <{10s:s}>\n"
|
||||
|
||||
foo: macro
|
||||
printt "<{\1}>\n"
|
||||
endm
|
||||
|
||||
foo -6d:n ; space is trimmed
|
||||
3
test/asm/string-formatting.err
Normal file
3
test/asm/string-formatting.err
Normal file
@@ -0,0 +1,3 @@
|
||||
ERROR: string-formatting.asm(9):
|
||||
Formatting string with prefix flag '#'
|
||||
error: Assembly aborted (1 errors)!
|
||||
5
test/asm/string-formatting.out
Normal file
5
test/asm/string-formatting.out
Normal file
@@ -0,0 +1,5 @@
|
||||
< 300 > <+00300> < 12c> < %100101100>
|
||||
<4294967254> <-42> <&000037777777726>
|
||||
<3.14159> <-00123> <-123.0455932618>
|
||||
<hello > < hello>
|
||||
<300 >
|
||||
Reference in New Issue
Block a user