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
*
* Redistribution and use in source and binary forms, with or without
@ -29,22 +29,21 @@
*/
#include "NumberParser.hxx"
#include "StringView.hxx"
#include <algorithm>
#include <iterator>
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];
*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;
char *endptr;
const auto result = ParseInt64(buffer, &endptr, base);
if (endptr_r != nullptr)
*endptr_r = s.data + (endptr - buffer);
*endptr_r = s.data() + (endptr - buffer);
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
*
* Redistribution and use in source and binary forms, with or without
@ -28,16 +28,14 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef NUMBER_PARSER_HXX
#define NUMBER_PARSER_HXX
#pragma once
#include <cassert>
#include <cstdint>
#include <string_view>
#include <stdlib.h>
struct StringView;
static inline unsigned
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
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
ParseDouble(const char *p, char **endptr=nullptr) noexcept
@ -91,5 +89,3 @@ ParseFloat(const char *p, char **endptr=nullptr) noexcept
return strtof(p, endptr);
#endif
}
#endif