PLYwoot
Header-only C++17 library for parsing and writing PLY files
Loading...
Searching...
No Matches
std.hpp
Go to the documentation of this file.
1/*
2 This file is part of PLYwoot, a header-only PLY parser.
3
4 Copyright (C) 2023-2025, Ton van den Heuvel
5
6 PLYwoot is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef PLYWOOT_STD_HPP
21#define PLYWOOT_STD_HPP
22
24
25#ifdef PLYWOOT_USE_FAST_FLOAT
26#include <fast_float/fast_float.h>
27#endif
28
29#ifdef PLYWOOT_USE_FAST_INT
30#include <fast_int/fast_int.hpp>
31#endif
32
33#include <algorithm>
34#include <cstdlib>
35#include <cstring>
36#include <iterator>
37#include <sstream>
38#include <string>
39
40namespace plywoot::detail {
41
49template<typename Ptr>
50constexpr Ptr align(Ptr ptr, std::size_t alignment)
51{
52 // Some explanation on the code below; -x is x in two's complement, which
53 // means that an alignment value x of power two is converted to (~x + 1).
54 // For example, for an alignment value of 4, this turns 0b000100 into
55 // 0b111100. The factor (uintptr + alignment - 1u) guarantees that the
56 // alignment bit is set unless (uintptr % alignment == 0).
57 const auto uintptr = reinterpret_cast<uintptr_t>(ptr);
58 return reinterpret_cast<Ptr>((uintptr + alignment - 1u) & -alignment);
59}
60
68template<>
69constexpr std::size_t align(std::size_t ptr, std::size_t alignment)
70{
71 // Some explanation on the code below; -x is x in two's complement, which
72 // means that an alignment value x of power two is converted to (~x + 1).
73 // For example, for an alignment value of 4, this turns 0b000100 into
74 // 0b111100. The factor (ptr + alignment - 1u) guarantees that the
75 // alignment bit is set unless (ptr % alignment == 0).
76 return (ptr + alignment - 1u) & -alignment;
77}
78
89template<typename Number>
90inline Number to_number(const char *first, const char *last, const char **end)
91{
92#ifdef PLYWOOT_USE_FAST_INT
93 Number n{};
94 *end = fast_int::from_chars(first, last, n).ptr;
95 return n;
96#else
97 return std::strtoll(first, const_cast<char **>(end), 10);
98#endif
99}
100
111template<>
112inline float to_number<>(const char *first, const char *last, const char **end)
113{
114#ifdef PLYWOOT_USE_FAST_FLOAT
115 float x;
116 *end = fast_float::from_chars(first, last, x).ptr;
117 return x;
118#else
119 return std::strtof(first, const_cast<char **>(end));
120#endif
121}
122
133template<>
134inline double to_number<double>(const char *first, const char *last, const char **end)
135{
136#ifdef PLYWOOT_USE_FAST_FLOAT
137 double x;
138 *end = fast_float::from_chars(first, last, x).ptr;
139 return x;
140#else
141 return std::strtof(first, const_cast<char **>(end));
142#endif
143}
144
151inline bool starts_with(const std::string &s, const char *prefix) { return s.rfind(prefix, 0) == 0; }
152
159template<typename T>
160std::string to_string(const T &t)
161{
162 std::stringstream oss;
163 oss << t;
164 return oss.str();
165}
166
167}
168
169#endif
Number to_number(const char *first, const char *last, const char **end)
Definition std.hpp:90
constexpr Ptr align(Ptr ptr, std::size_t alignment)
Definition std.hpp:50
bool starts_with(const std::string &s, const char *prefix)
Definition std.hpp:151
std::string to_string(const T &t)
Definition std.hpp:160