1
0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-06-03 03:23:33 +02:00

Refactor editor gotoroom shortcut to not use split()

Even if split() didn't use the STL, using this function here is a bit
unnecessary, because a simple SDL_strchr() suffices. Refactoring split()
to not use the STL will break this caller anyway, so I might as well
just refactor this to not use split() in the first place.

This refactor also properly checks if the inputs are valid integers. And
since split() is no longer used, it also rejects inputs ending with a
trailing comma as being invalid, too; this didn't happen previously.
It's intentional that I used is_number() here instead of
is_positive_num(), thus accepting negative numbers; in the future it
might be possible to have negative room coordinates.
This commit is contained in:
Misa 2021-02-11 17:02:32 -08:00 committed by Ethan Lee
parent fe8d163041
commit 3fcdc084d0

View File

@ -4114,15 +4114,34 @@ void editorinput()
{
case TEXT_GOTOROOM:
{
std::vector<std::string> coords = split(key.keybuffer, ',');
if (coords.size() != 2)
char coord_x[16];
char coord_y[16];
const char* comma = SDL_strchr(key.keybuffer.c_str(), ',');
bool valid_input = comma != NULL;
if (valid_input)
{
SDL_strlcpy(
coord_x,
key.keybuffer.c_str(),
VVV_min(comma - key.keybuffer.c_str() + 1, sizeof(coord_x))
);
SDL_strlcpy(coord_y, &comma[1], sizeof(coord_y));
valid_input = is_number(coord_x) && is_number(coord_y);
}
if (!valid_input)
{
ed.note = "[ ERROR: Invalid format ]";
ed.notedelay = 45;
break;
}
ed.levx = clamp(help.Int(coords[0].c_str()) - 1, 0, ed.mapwidth - 1);
ed.levy = clamp(help.Int(coords[1].c_str()) - 1, 0, ed.mapheight - 1);
ed.levx = clamp(help.Int(coord_x) - 1, 0, ed.mapwidth - 1);
ed.levy = clamp(help.Int(coord_y) - 1, 0, ed.mapheight - 1);
graphics.backgrounddrawn = false;
break;
}