format.h

main 101 lines · 4 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1/*
2 * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#ifndef STDEXT_FORMAT_H
24#define STDEXT_FORMAT_H
25
26#include "traits.h"
27
28#include <string>
29#include <cstdio>
30#include <tuple>
31#include <iostream>
32#include <sstream>
33#include <iomanip>
34
35namespace stdext {
36
37template<class T> void print_ostream(std::ostringstream& stream, const T& last) { stream << last; }
38template<class T, class... Args>
39void print_ostream(std::ostringstream& stream, const T& first, const Args&... rest) { stream << "\t" << first; print_ostream(stream, rest...); }
40template<class... T>
41
42// Utility for printing variables just like lua
43void print(const T&... args) { std::ostringstream buf; print_ostream(buf, args...); std::cout << buf.str() << std::endl; }
44
45template<typename T>
46typename std::enable_if<std::is_integral<T>::value ||
47 std::is_pointer<T>::value ||
48 std::is_floating_point<T>::value ||
49 std::is_enum<T>::value, T>::type sprintf_cast(const T& t) { return t; }
50inline const char *sprintf_cast(const char *s) { return s; }
51inline const char *sprintf_cast(const std::string& s) { return s.c_str(); }
52
53template<int N> struct expand_snprintf {
54 template<typename Tuple, typename... Args> static int call(char *s, size_t maxlen, const char *format, const Tuple& tuple, const Args&... args) {
55 return expand_snprintf<N-1>::call(s, maxlen, format, tuple, sprintf_cast(std::get<N-1>(tuple)), args...); }};
56template<> struct expand_snprintf<0> {
57 template<typename Tuple, typename... Args> static int call(char *s, size_t maxlen, const char *format, const Tuple& tuple, const Args&... args) {
58#ifdef _MSC_VER
59 return _snprintf(s, maxlen, format, args...);
60#else
61 return snprintf(s, maxlen, format, args...);
62#endif
63 }
64};
65
66// Improved snprintf that accepts std::string and other types
67template<typename... Args>
68int snprintf(char *s, size_t maxlen, const char *format, const Args&... args) {
69 std::tuple<typename replace_extent<Args>::type...> tuple(args...);
70 return expand_snprintf<std::tuple_size<decltype(tuple)>::value>::call(s, maxlen, format, tuple);
71}
72
73template<typename... Args>
74inline int snprintf(char *s, size_t maxlen, const char *format) {
75 std::strncpy(s, format, maxlen);
76 s[maxlen-1] = 0;
77 return strlen(s);
78}
79
80template<typename... Args>
81inline std::string format() { return std::string(); }
82
83template<typename... Args>
84inline std::string format(const std::string& format) { return format; }
85
86// Format strings with the sprintf style, accepting std::string and string convertible types for %s
87template<typename... Args>
88std::string format(const std::string& format, const Args&... args) {
89 int n = snprintf(NULL, 0, format.c_str(), args...);
90 VALIDATE(n != -1);
91 std::string buffer(n + 1, '\0');
92 n = snprintf(&buffer[0], buffer.size(), format.c_str(), args...);
93 VALIDATE(n != -1);
94 buffer.resize(n);
95 return buffer;
96}
97
98}
99
100#endif
101
Top