input.cpp, inputdev.cpp: Convert a few more functions to use std::string_view

This commit is contained in:
AJR 2021-02-03 17:44:17 -05:00
parent c47ed0112e
commit 0c57408ed8
5 changed files with 38 additions and 39 deletions

View File

@ -871,21 +871,21 @@ std::string input_manager::code_to_token(input_code code) const
// token
//-------------------------------------------------
input_code input_manager::code_from_token(const char *_token)
input_code input_manager::code_from_token(std::string_view _token)
{
// copy the token and break it into pieces
std::string token[6];
int numtokens;
for (numtokens = 0; numtokens < ARRAY_LENGTH(token); )
int numtokens = 0;
while (numtokens < ARRAY_LENGTH(token))
{
// make a token up to the next underscore
char *score = (char *)strchr(_token, '_');
token[numtokens++].assign(_token, (score == nullptr) ? strlen(_token) : (score - _token));
std::string_view::size_type score = _token.find('_');
token[numtokens++].assign(_token, (std::string_view::npos == score) ? _token.length() : score);
// if we hit the end, we're done, else advance our pointer
if (score == nullptr)
if (std::string_view::npos == score)
break;
_token = score + 1;
_token.remove_prefix(score + 1);
}
// first token should be the devclass
@ -1228,53 +1228,52 @@ std::string input_manager::seq_to_tokens(const input_seq &seq) const
// of a sequence
//-------------------------------------------------
void input_manager::seq_from_tokens(input_seq &seq, const char *string)
void input_manager::seq_from_tokens(input_seq &seq, std::string_view string)
{
// start with a blank sequence
seq.reset();
// loop until we're done
std::vector<char> strcopy(string, string + std::strlen(string) + 1);
char *str = &strcopy[0];
unsigned operators = 0;
while (1)
{
// trim any leading spaces
while (*str != 0 && isspace(u8(*str)))
str++;
while (!string.empty() && isspace(u8(string[0])))
string.remove_prefix(1);
// bail if we're done
if (*str == 0)
if (string.empty())
return;
// find the end of the token and make it upper-case along the way
char *strtemp;
for (strtemp = str; *strtemp != 0 && !isspace(u8(*strtemp)); strtemp++)
*strtemp = toupper(u8(*strtemp));
char origspace = *strtemp;
*strtemp = 0;
std::string token;
while (!string.empty() && !isspace(u8(string[0])))
{
token.push_back(toupper(u8(string[0])));
string.remove_prefix(1);
}
// look for common stuff
input_code code;
bool is_operator;
if (strcmp(str, "OR") == 0)
if (token == "OR")
{
code = input_seq::or_code;
is_operator = true;
}
else if (strcmp(str, "NOT") == 0)
else if (token == "NOT")
{
code = input_seq::not_code;
is_operator = true;
}
else if (strcmp(str, "DEFAULT") == 0)
else if (token == "DEFAULT")
{
code = input_seq::default_code;
is_operator = false;
}
else
{
code = code_from_token(str);
code = code_from_token(token);
is_operator = false;
}
@ -1288,7 +1287,7 @@ void input_manager::seq_from_tokens(input_seq &seq, const char *string)
{
if (code.device_class() < DEVICE_CLASS_FIRST_VALID)
{
osd_printf_warning("Input: Dropping invalid input token %s\n", str);
osd_printf_warning("Input: Dropping invalid input token %s\n", token);
while (operators)
{
seq.backspace();
@ -1303,9 +1302,9 @@ void input_manager::seq_from_tokens(input_seq &seq, const char *string)
}
// advance
if (origspace == 0)
if (string.empty())
return;
str = strtemp + 1;
string.remove_prefix(1);
}
}
@ -1321,23 +1320,23 @@ bool input_manager::map_device_to_controller(const devicemap_table_type *devicem
for (devicemap_table_type::const_iterator it = devicemap_table->begin(); it != devicemap_table->end(); it++)
{
const char *deviceid = it->first.c_str();
const char *controllername = it->second.c_str();
std::string_view deviceid = it->first;
std::string_view controllername = it->second;
// tokenize the controller name into device class and index (i.e. controller name should be of the form "GUNCODE_1")
std::string token[2];
int numtokens;
const char *_token = controllername;
for (numtokens = 0; numtokens < ARRAY_LENGTH(token); )
int numtokens = 0;
std::string_view _token = controllername;
while (numtokens < ARRAY_LENGTH(token))
{
// make a token up to the next underscore
char *score = (char *)strchr(_token, '_');
token[numtokens++].assign(_token, (score == nullptr) ? strlen(_token) : (score - _token));
std::string_view::size_type score = _token.find('_');
token[numtokens++].assign(_token, (std::string_view::npos == score) ? _token.length() : score);
// if we hit the end, we're done, else advance our pointer
if (score == nullptr)
if (std::string_view::npos == score)
break;
_token = score + 1;
_token.remove_prefix(score + 1);
}
if (2 != numtokens)
return false;

View File

@ -512,7 +512,7 @@ public:
input_code code_from_itemid(input_item_id itemid) const;
std::string code_name(input_code code) const;
std::string code_to_token(input_code code) const;
input_code code_from_token(const char *_token);
input_code code_from_token(std::string_view _token);
const char *standard_token(input_item_id itemid) const;
// input sequence readers
@ -523,7 +523,7 @@ public:
input_seq seq_clean(const input_seq &seq) const;
std::string seq_name(const input_seq &seq) const;
std::string seq_to_tokens(const input_seq &seq) const;
void seq_from_tokens(input_seq &seq, const char *_token);
void seq_from_tokens(input_seq &seq, std::string_view _token);
// misc
bool map_device_to_controller(const devicemap_table_type *devicemap_table = nullptr);

View File

@ -329,7 +329,7 @@ input_item_id input_device::add_item(const char *name, input_item_id itemid, ite
// substring search
//-------------------------------------------------
bool input_device::match_device_id(const char *deviceid) const
bool input_device::match_device_id(std::string_view deviceid) const
{
std::string deviceidupper(strmakeupper(deviceid));
std::string idupper(strmakeupper(m_id));

View File

@ -164,7 +164,7 @@ public:
// helpers
s32 adjust_absolute(s32 value) const { return adjust_absolute_value(value); }
bool match_device_id(const char *deviceid) const;
bool match_device_id(std::string_view deviceid) const;
protected:
// specific overrides

View File

@ -357,7 +357,7 @@ void lua_engine::initialize_input(sol::table &emu)
input_type["seq_name"] = &input_manager::seq_name;
input_type["seq_to_tokens"] = &input_manager::seq_to_tokens;
input_type["seq_from_tokens"] =
[] (input_manager &input, const char *tokens)
[] (input_manager &input, std::string_view tokens)
{
input_seq seq;
input.seq_from_tokens(seq, tokens);