util/NumberParser: use std::string_view

This commit is contained in:
Max Kellermann 2022-07-01 11:20:29 +02:00
parent 671b7e079f
commit f32d752ccb
2 changed files with 8 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2009-2019 Max Kellermann <max.kellermann@gmail.com> * Copyright 2009-2022 Max Kellermann <max.kellermann@gmail.com>
* http://www.musicpd.org * http://www.musicpd.org
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -29,22 +29,21 @@
*/ */
#include "NumberParser.hxx" #include "NumberParser.hxx"
#include "StringView.hxx"
#include <algorithm> #include <algorithm>
#include <iterator> #include <iterator>
int64_t int64_t
ParseInt64(StringView s, const char **endptr_r, int base) noexcept ParseInt64(std::string_view s, const char **endptr_r, int base) noexcept
{ {
char buffer[32]; char buffer[32];
*std::copy_n(s.data, std::min(s.size, std::size(buffer) - 1), *std::copy_n(s.data(), std::min(s.size(), std::size(buffer) - 1),
buffer) = 0; buffer) = 0;
char *endptr; char *endptr;
const auto result = ParseInt64(buffer, &endptr, base); const auto result = ParseInt64(buffer, &endptr, base);
if (endptr_r != nullptr) if (endptr_r != nullptr)
*endptr_r = s.data + (endptr - buffer); *endptr_r = s.data() + (endptr - buffer);
return result; return result;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2009-2019 Max Kellermann <max.kellermann@gmail.com> * Copyright 2009-2022 Max Kellermann <max.kellermann@gmail.com>
* http://www.musicpd.org * http://www.musicpd.org
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -28,16 +28,14 @@
* OF THE POSSIBILITY OF SUCH DAMAGE. * OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef NUMBER_PARSER_HXX #pragma once
#define NUMBER_PARSER_HXX
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>
#include <string_view>
#include <stdlib.h> #include <stdlib.h>
struct StringView;
static inline unsigned static inline unsigned
ParseUnsigned(const char *p, char **endptr=nullptr, int base=10) noexcept ParseUnsigned(const char *p, char **endptr=nullptr, int base=10) noexcept
{ {
@ -71,7 +69,7 @@ ParseInt64(const char *p, char **endptr=nullptr, int base=10) noexcept
} }
int64_t int64_t
ParseInt64(StringView s, const char **endptr_r=nullptr, int base=10) noexcept; ParseInt64(std::string_view s, const char **endptr_r=nullptr, int base=10) noexcept;
static inline double static inline double
ParseDouble(const char *p, char **endptr=nullptr) noexcept ParseDouble(const char *p, char **endptr=nullptr) noexcept
@ -91,5 +89,3 @@ ParseFloat(const char *p, char **endptr=nullptr) noexcept
return strtof(p, endptr); return strtof(p, endptr);
#endif #endif
} }
#endif