Skip to content

Commit

Permalink
pg/Serial: pass std::string_view to Parse()
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Nov 14, 2023
1 parent 7ee4d53 commit 6f890f1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
20 changes: 9 additions & 11 deletions src/pg/Serial.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,30 @@
// author: Max Kellermann <[email protected]>

#include "Serial.hxx"
#include "util/NumberParser.hxx"

#include <stdexcept>
#include <cstdlib>

namespace Pg {

Serial
Serial::Parse(const char *s)
Serial::Parse(std::string_view s)
{
char *endptr;
auto value = std::strtoul(s, &endptr, 10);
if (endptr == s || *endptr != 0)
const auto value = ParseInteger<value_type>(s);
if (!value)
throw std::invalid_argument("Failed to parse serial");

return Serial(value);
return Serial{*value};
}

BigSerial
BigSerial::Parse(const char *s)
BigSerial::Parse(std::string_view s)
{
char *endptr;
auto value = std::strtoul(s, &endptr, 10);
if (endptr == s || *endptr != 0)
const auto value = ParseInteger<value_type>(s);
if (!value)
throw std::invalid_argument("Failed to parse serial");

return BigSerial{value};
return BigSerial{*value};
}

}
5 changes: 3 additions & 2 deletions src/pg/Serial.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <cstdint>
#include <string_view>

namespace Pg {

Expand Down Expand Up @@ -32,7 +33,7 @@ public:
* std::invalid_argument on error.
*/
[[nodiscard]]
static Serial Parse(const char *s);
static Serial Parse(std::string_view s);
};

/**
Expand All @@ -59,7 +60,7 @@ public:
* std::invalid_argument on error.
*/
[[nodiscard]]
static BigSerial Parse(const char *s);
static BigSerial Parse(std::string_view s);
};

}

0 comments on commit 6f890f1

Please sign in to comment.