Skip to content

Commit

Permalink
Merge pull request #1988 from AlanDrake/ags4-removebracketnewline
Browse files Browse the repository at this point in the history
Removes special cases for [ as a newline character
  • Loading branch information
ivan-mogilko authored Jul 12, 2023
2 parents 2abcf82 + 028d093 commit 6d7ec60
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 64 deletions.
45 changes: 3 additions & 42 deletions Common/font/fonts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,40 +370,6 @@ int get_text_lines_surf_height(size_t fontNumber, size_t numlines)

namespace AGS { namespace Common { SplitLines Lines; } }

// Replaces AGS-specific linebreak tags with common '\n'
static void unescape_script_string(const char *cstr, std::string &out)
{
out.clear();
// Handle the special case of the first char
if (cstr[0] == '[')
{
out.push_back('\n');
cstr++;
}
// Replace all other occurrences as they're found
// NOTE: we do not need to decode utf8 here, because
// we are only searching for low-code ascii chars.
const char *off;
for (off = cstr; *off; ++off)
{
if (*off != '[') continue;
if (*(off - 1) == '\\')
{
// convert \[ into [
out.insert(out.end(), cstr, off - 1);
out.push_back('[');
}
else
{
// convert [ into \n
out.insert(out.end(), cstr, off);
out.push_back('\n');
}
cstr = off + 1;
}
out.insert(out.end(), cstr, off + 1);
}

// Break up the text into lines
size_t split_lines(const char *todis, SplitLines &lines, int wii, int fonnt, size_t max_lines) {
// NOTE: following hack accomodates for the legacy math mistake in split_lines.
Expand All @@ -417,24 +383,19 @@ size_t split_lines(const char *todis, SplitLines &lines, int wii, int fonnt, siz

lines.Reset();

std::string &line_buf = lines.LineBuf[0];
std::string &test_buf = lines.LineBuf[1];

// Do all necessary preliminary conversions: unescape, etc
unescape_script_string(todis, line_buf);
std::string &test_buf = lines.LineBuf[0];

// TODO: we NEED a proper utf8 string class, and refact all this mess!!
// in this case we perhaps could use custom ITERATOR types that read
// and write utf8 chars in std::strings or similar containers.
test_buf.clear();
const char *end_ptr = &line_buf.back(); // buffer end ptr
const char *theline = &line_buf.front(); // sub-line ptr
const char *theline = todis; // sub-line ptr
const char *scan_ptr = theline; // a moving scan pos
const char *prev_ptr = scan_ptr; // previous scan pos
const char *last_whitespace = nullptr; // last found whitespace

while (true) {
if (scan_ptr == end_ptr) {
if (*scan_ptr == 0) {
// end of the text, add the last line if necessary
if (scan_ptr > theline) {
lines.Add(test_buf.c_str());
Expand Down
4 changes: 0 additions & 4 deletions Compiler/script/cc_compiledscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ int ccCompiledScript::add_string(const char*strr) {
ch = strr[src];
if (ch == 'n') {ch = '\n';}
else if (ch == 'r') {ch = '\r';}
else if (ch == '[') { // pass through as \[
*write_ptr = '\\';
write_ptr++;
}
}
*write_ptr = ch;
write_ptr++;
Expand Down
21 changes: 6 additions & 15 deletions Compiler/script2/cs_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,6 @@ void AGS::Scanner::ReadInCharLit(std::string &symstring, CodeCell &value)
if (Failed())
break; // to error processing

if ('[' == lit_char)
// "\\[" is equivalent to two characters, so can't be used as a single character
UserError("'\\[' is not allowed in single quotes, use '[' instead");

EscapedChar2Char(lit_char, symstring, lit_char);
}

Expand Down Expand Up @@ -557,16 +553,11 @@ void AGS::Scanner::ReadInStringLit(std::string &symstring, std::string &valstrin
symstring.push_back(ch);
if (EOFReached() || Failed() || '\n' == ch || '\r' == ch)
break; // to error msg
if ('[' == ch)
{
valstring.append("\\[");
}
else
{
int converted;
EscapedChar2Char(ch, symstring, converted);
valstring.push_back(converted);
}

int converted;
EscapedChar2Char(ch, symstring, converted);
valstring.push_back(converted);

continue;
}

Expand Down Expand Up @@ -621,7 +612,7 @@ void AGS::Scanner::ReadInStringLit(std::string &symstring, std::string &valstrin
else if (Failed())
UserError("Read error while scanning a string literal (file corrupt?)");
else
UserError("Line ended within a string literal, this isn't allowed (use '[' for newline)");
UserError("Line ended within a string literal, this isn't allowed.");
}

void AGS::Scanner::ReadInIdentifier(std::string &symstring)
Expand Down
6 changes: 3 additions & 3 deletions Compiler/test2/cc_scanner_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,13 +750,13 @@ TEST_F(Scan, String1)
{
// Should scan advanced escape sequences within string.

std::string Input = "\"Oh, \\the \\brow\\n \\fo\\x5e jumps \\[ove\\r] the \\100\\azy dog.\"";
std::string Input = "\"Oh, \\the \\brow\\n \\fo\\x5e jumps [ove\\r] the \\100\\azy dog.\"";
AGS::Scanner scanner(Input, token_list, string_collector, sym, mh);

ASSERT_LE(0, scanner.GetNextSymstringT(symstring, sct, value));
ASSERT_LE(0, value);
EXPECT_STREQ("Oh, \the \brow\n \fo^ jumps \\[ove\r] the @\azy dog.", &(string_collector.strings[value]));
EXPECT_STREQ("\"Oh, \\the \\brow\\n \\fo\\x5e jumps \\[ove\\r] the \\100\\azy dog.\"", symstring.c_str());
EXPECT_STREQ("Oh, \the \brow\n \fo^ jumps [ove\r] the @\azy dog.", &(string_collector.strings[value]));
EXPECT_STREQ("\"Oh, \\the \\brow\\n \\fo\\x5e jumps [ove\\r] the \\100\\azy dog.\"", symstring.c_str());
}

TEST_F(Scan, UnknownKeywordAfterReadonly) {
Expand Down

0 comments on commit 6d7ec60

Please sign in to comment.