1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:27:45 +00:00

LibC: Convert getopt and getopt_long to new StringView usage

This commit is contained in:
sin-ack 2022-07-11 20:52:03 +00:00 committed by Andreas Kling
parent 828060e631
commit e3da0adfe6

View file

@ -54,6 +54,14 @@ private:
void shift_argv();
bool find_next_option();
StringView current_arg() const
{
auto const* arg_ptr = m_argv[m_arg_index];
if (arg_ptr == NULL)
return {};
return { arg_ptr, strlen(arg_ptr) };
}
size_t m_argc { 0 };
char* const* m_argv { nullptr };
StringView m_short_options;
@ -98,7 +106,7 @@ int OptionParser::getopt()
int res = -1;
bool found_an_option = find_next_option();
StringView arg = m_argv[m_arg_index];
auto arg = current_arg();
if (!found_an_option) {
res = -1;
@ -154,7 +162,7 @@ bool OptionParser::lookup_short_option(char option, int& needs_value) const
int OptionParser::handle_short_option()
{
StringView arg = m_argv[m_arg_index];
StringView arg = current_arg();
VERIFY(arg.starts_with('-'));
if (s_index_into_multioption_argument == 0) {
@ -237,7 +245,7 @@ option const* OptionParser::lookup_long_option(char* raw) const
int OptionParser::handle_long_option()
{
VERIFY(StringView(m_argv[m_arg_index]).starts_with("--"));
VERIFY(current_arg().starts_with("--"sv));
// We cannot set optopt to anything sensible for long options, so set it to 0.
optopt = 0;
@ -311,7 +319,7 @@ void OptionParser::shift_argv()
bool OptionParser::find_next_option()
{
for (m_arg_index = optind; m_arg_index < m_argc && m_argv[m_arg_index]; m_arg_index++) {
StringView arg = m_argv[m_arg_index];
StringView arg = current_arg();
// Anything that doesn't start with a "-" is not an option.
if (!arg.starts_with('-')) {
if (m_stop_on_first_non_option)