Skip to content

Commit

Permalink
Add some more string test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangi42 committed Jan 28, 2025
1 parent c4b456b commit d6d5169
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 32 deletions.
7 changes: 0 additions & 7 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
#ifndef RGBDS_UTIL_HPP
#define RGBDS_UTIL_HPP

#include <stddef.h>
#include <stdint.h>
#include <vector>

char const *printChar(int c);

// @return The number of bytes read, or 0 if invalid data was found
size_t readUTF8Char(std::vector<int32_t> *dest, char const *src);

#endif // RGBDS_UTIL_HPP
21 changes: 21 additions & 0 deletions src/asm/charmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <unordered_map>
#include <utility>

#include "extern/utf8decoder.hpp"
#include "helpers.hpp"
#include "util.hpp"

Expand Down Expand Up @@ -183,6 +184,26 @@ std::vector<int32_t> charmap_Convert(std::string const &input) {
return output;
}

static size_t readUTF8Char(std::vector<int32_t> *dest, char const *src) {
uint32_t state = 0, codepoint;
size_t i = 0;

for (;;) {
if (decode(&state, &codepoint, src[i]) == 1) {
return 0;
}

if (dest) {
dest->push_back(src[i]);
}
i++;

if (state == 0) {
return i;
}
}
}

size_t charmap_ConvertNext(std::string_view &input, std::vector<int32_t> *output) {
// The goal is to match the longest mapping possible.
// For that, advance through the trie with each character read.
Expand Down
23 changes: 0 additions & 23 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <vector>

#include "extern/utf8decoder.hpp"

char const *printChar(int c) {
// "'A'" + '\0': 4 bytes
Expand Down Expand Up @@ -53,23 +50,3 @@ char const *printChar(int c) {
buf[4] = '\0';
return buf;
}

size_t readUTF8Char(std::vector<int32_t> *dest, char const *src) {
uint32_t state = 0, codepoint;
size_t i = 0;

for (;;) {
if (decode(&state, &codepoint, src[i]) == 1) {
return 0;
}

if (dest) {
dest->push_back(src[i]);
}
i++;

if (state == 0) {
return i;
}
}
}
1 change: 1 addition & 0 deletions test/asm/invalid-format.asm
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ println STRFMT("%q16s", "hello")

println STRFMT("%#d", 42)
println STRFMT("%.5d", 42)
println STRFMT("%q5d", 42)
4 changes: 3 additions & 1 deletion test/asm/invalid-format.err
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ error: invalid-format.asm(16):
Formatting type 'd' with exact flag '#'
error: invalid-format.asm(17):
Formatting type 'd' with fractional width
error: Assembly aborted (14 errors)!
error: invalid-format.asm(18):
Formatting type 'd' with fractional precision
error: Assembly aborted (15 errors)!
1 change: 1 addition & 0 deletions test/asm/invalid-format.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ hello
hello
42
42
42
14 changes: 14 additions & 0 deletions test/asm/invalid-utf-8-strings.asm
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,17 @@ println "\"{invalid}\" == \"{copy}\" ({d:n})"
DEF mid1 EQUS STRSUB("{invalid}", 5, 2)
DEF mid2 EQUS STRSUB("{invalid}", 9, 1)
println "\"{mid2}{mid1}\""

; characters:
; 1: U+0041 A
; 2: U+0020 space
; 3: invalid byte 0xFE
; 4: invalid byte 0x81
; 5: invalid byte 0xFF
; 6: U+0020 space
; 7: U+0042 B
REDEF invalid EQUS "A þÿ B"

DEF n = strlen("{invalid}")
DEF r = charlen("{invalid}")
println "\"{#s:invalid}\": {d:n} != {d:r}"
10 changes: 9 additions & 1 deletion test/asm/invalid-utf-8-strings.err
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@ error: invalid-utf-8-strings.asm(22):
STRSUB: Invalid UTF-8 byte 0xA3
error: invalid-utf-8-strings.asm(22):
STRSUB: Invalid UTF-8 byte 0xA4
error: Assembly aborted (22 errors)!
error: invalid-utf-8-strings.asm(35):
STRLEN: Invalid UTF-8 byte 0xFE
error: invalid-utf-8-strings.asm(35):
STRLEN: Invalid UTF-8 byte 0x81
error: invalid-utf-8-strings.asm(35):
STRLEN: Invalid UTF-8 byte 0xFF
error: invalid-utf-8-strings.asm(36):
Input string is not valid UTF-8
error: Assembly aborted (26 errors)!
1 change: 1 addition & 0 deletions test/asm/invalid-utf-8-strings.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
"aäb漢,a��b��!" == "aäb漢,a��b��!" (12)
"b,a"
"A ��� B": 7 != 2
2 changes: 2 additions & 0 deletions test/asm/string-formatting.asm
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ def f equ -123.0456
def pi equ 3.14159
def s equs "hello"
def t equs "\"\\t\" is '\t'"
def u equs "\t\r\0\n"

println "<{ -6d:n}> <{+06u:n}> <{5x:n}> <{#16b:n}>"
println "<{u:m}> <{+3d:m}> <{#016o:m}>"
println "<{f:pi}> <{06.f:f}> <{.10f:f}>"
println "\"{#-20s:t}\", \"{#20s:t}\", \"{20s:t}\""
println "{#s:u}"

macro foo
println "\1 <{\1}>"
Expand Down
1 change: 1 addition & 0 deletions test/asm/string-formatting.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<4294967254> <-42> <&000037777777726>
<3.14159> <-00123> <-123.0455932617>
"\"\\t\" is '\t' ", " \"\\t\" is '\t'", " "\t" is ' '"
\t\r\0\n
-6d:n <300 >

0 comments on commit d6d5169

Please sign in to comment.