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-2026, 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 + 1u));
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 + 1u);
77}
78
89template<typename Number>
90#ifdef PLYWOOT_USE_FAST_INT
91inline Number to_number(const char *first, const char *last, const char **end)
92#else
93inline Number to_number(const char *first, const char *, const char **end)
94#endif
95{
96#ifdef PLYWOOT_USE_FAST_INT
97 Number n{};
98 *end = fast_int::from_chars(first, last, n).ptr;
99 return n;
100#else
101 return static_cast<Number>(std::strtoll(first, const_cast<char **>(end), 10));
102#endif
103}
104
115template<>
116#ifdef PLYWOOT_USE_FAST_FLOAT
117inline float to_number<>(const char *first, const char *last, const char **end)
118#else
119inline float to_number<>(const char *first, const char *, const char **end)
120#endif
121{
122#ifdef PLYWOOT_USE_FAST_FLOAT
123 float x;
124 *end = fast_float::from_chars(first, last, x).ptr;
125 return x;
126#else
127 return std::strtof(first, const_cast<char **>(end));
128#endif
129}
130
141template<>
142#ifdef PLYWOOT_USE_FAST_FLOAT
143inline double to_number<double>(const char *first, const char *last, const char **end)
144#else
145inline double to_number<double>(const char *first, const char *, const char **end)
146#endif
147{
148#ifdef PLYWOOT_USE_FAST_FLOAT
149 double x;
150 *end = fast_float::from_chars(first, last, x).ptr;
151 return x;
152#else
153 return std::strtof(first, const_cast<char **>(end));
154#endif
155}
156
163inline bool starts_with(const std::string &s, const char *prefix) { return s.rfind(prefix, 0) == 0; }
164
171template<typename T>
172std::string to_string(const T &t)
173{
174 std::stringstream oss;
175 oss << t;
176 return oss.str();
177}
178
179}
180
181#endif
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:163
std::string to_string(const T &t)
Definition std.hpp:172
Number to_number(const char *first, const char *, const char **end)
Definition std.hpp:93