1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-01 12:57:51 +02:00

Implemented JSON5 line ending escapings

This commit is contained in:
Ivan Savenko 2024-10-26 14:21:23 +00:00
parent 72b0062ae3
commit ec3acec8cc

View File

@ -158,40 +158,58 @@ bool JsonParser::extractEscaping(std::string & str)
switch(input[pos]) switch(input[pos])
{ {
case '\r':
if(settings.mode == JsonParsingSettings::JsonFormatMode::JSON5 && input.size() > pos && input[pos+1] == '\n')
{
pos += 2;
return true;
}
break;
case '\n':
if(settings.mode == JsonParsingSettings::JsonFormatMode::JSON5)
{
pos += 1;
return true;
}
break;
case '\"': case '\"':
str += '\"'; str += '\"';
break; pos++;
return true;
case '\\': case '\\':
str += '\\'; str += '\\';
break; pos++;
return true;
case 'b': case 'b':
str += '\b'; str += '\b';
break; pos++;
return true;
case 'f': case 'f':
str += '\f'; str += '\f';
break; pos++;
return true;
case 'n': case 'n':
str += '\n'; str += '\n';
break; pos++;
return true;
case 'r': case 'r':
str += '\r'; str += '\r';
break; pos++;
return true;
case 't': case 't':
str += '\t'; str += '\t';
break; pos++;
return true;
case '/': case '/':
str += '/'; str += '/';
break; pos++;
default: return true;
return error("Unknown escape sequence!", true);
} }
return true; return error("Unknown escape sequence!", true);
} }
bool JsonParser::extractString(std::string & str) bool JsonParser::extractString(std::string & str)
{ {
//TODO: JSON5 - line breaks escaping
if(settings.mode < JsonParsingSettings::JsonFormatMode::JSON5) if(settings.mode < JsonParsingSettings::JsonFormatMode::JSON5)
{ {
if(input[pos] != '\"') if(input[pos] != '\"')
@ -216,27 +234,30 @@ bool JsonParser::extractString(std::string & str)
pos++; pos++;
return true; return true;
} }
if(input[pos] == '\\') // Escaping else if(input[pos] == '\\') // Escaping
{ {
str.append(&input[first], pos - first); str.append(&input[first], pos - first);
pos++; pos++;
if(pos == input.size()) if(pos == input.size())
break; break;
extractEscaping(str); extractEscaping(str);
first = pos + 1; first = pos;
} }
if(input[pos] == '\n') // end-of-line else if(input[pos] == '\n') // end-of-line
{ {
str.append(&input[first], pos - first); str.append(&input[first], pos - first);
return error("Closing quote not found!", true); return error("Closing quote not found!", true);
} }
if(static_cast<unsigned char>(input[pos]) < ' ') // control character else if(static_cast<unsigned char>(input[pos]) < ' ') // control character
{ {
str.append(&input[first], pos - first); str.append(&input[first], pos - first);
first = pos + 1; pos++;
first = pos;
error("Illegal character in the string!", true); error("Illegal character in the string!", true);
} }
pos++; else
pos++;
} }
return error("Unterminated string!"); return error("Unterminated string!");
} }