Skip to content

Commit ee8d2f5

Browse files
authored
pattern/bencode: Various fixes (#393)
* Fix bencode dictionary When parsing a bencode dictionary, the end character 'e' was never consumed. This caused a misinterpretation of the character as struct Value of an unknown type 'e'. * Fix bencode list A list was not included in the Value's parsing logic so it may have been mistakenly parsed as a string. * Fix std::ctype::isprint not including space The space character, 0x20, is considered as a printable character in ASCII and in >=C89. Adding it to the range of std::ctype::isprint also fixes other std::ctype functions that use it. * Fix bencode byte string formatting Byte strings do not render nicely in pattern data's value column if they contain non-printable characters. This commit makes the value of byte strings to be surrounded by quotation marks, and renders a warning text without quotation marks if the byte string contains non-printable characters.
1 parent 5b8dde1 commit ee8d2f5

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

includes/std/ctype.pat

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ namespace auto std::ctype {
8585
@return True if `c` is part of this range, false otherwise
8686
*/
8787
fn isprint(char c) {
88-
return c >= '0' && c <= '~';
88+
return c >= ' ' && c <= '~';
8989
};
9090

9191
/**

patterns/bencode.hexpat

+11-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,15 @@ namespace bencode {
3838
ASCIIDecimal length;
3939
char separator [[hidden]];
4040
char value[length];
41-
} [[sealed, format("bencode::format_string"), transform("bencode::format_string")]];
41+
} [[sealed, format("bencode::format_string")]];
4242

4343
fn format_string(String string) {
44-
return string.value;
44+
for (u64 i = 0, i < string.length, i = i + 1) {
45+
if (!std::ctype::isprint(string.value[i])) {
46+
return "Contains non-printable characters";
47+
}
48+
}
49+
return std::format("\"{}\"", string.value);
4550
};
4651

4752
using Bencode;
@@ -57,6 +62,10 @@ namespace bencode {
5762

5863
if (type == Type::Dictionary) {
5964
DictionaryEntry entry[while(std::mem::read_unsigned($, 1) != 'e')];
65+
char end;
66+
} else if (type == Type::List) {
67+
Value entry[while(std::mem::read_unsigned($, 1) != 'e')];
68+
char end;
6069
} else if (type == Type::Integer) {
6170
ASCIIDecimal value;
6271
char end;

0 commit comments

Comments
 (0)