xtd 0.2.0
Loading...
Searching...
No Matches
__sprintf.h
Go to the documentation of this file.
1
3#pragma once
5#if !defined(__XTD_CORE_INTERNAL__)
6#error "Do not include this file: Internal use only"
7#endif
9
10#include "../types.h"
11#include <cstdarg>
12#include <string>
13
15template <typename char_t>
16inline std::basic_string<char_t> __sprintf(const char_t* fmt, ...) {return std::basic_string<char_t>();}
17
18template <>
19inline std::basic_string<char> __sprintf<char>(const char* fmt, ...) {
20 va_list args;
21 va_start(args, fmt);
22 std::basic_string<char> formatted_string(vsnprintf(nullptr, 0, fmt, args), 0);
23 va_end(args);
24 va_start(args, fmt);
25 vsnprintf(&formatted_string[0], formatted_string.size() + 1, fmt, args);
26 va_end(args);
27 if (formatted_string == "INF") formatted_string = {'i', 'n', 'f'};
28 if (formatted_string == "-INF") formatted_string = {'-', 'i', 'n', 'f'};
29 if (formatted_string == "NAN") formatted_string = {'n', 'a', 'n'};
30 return formatted_string;
31}
32
33template <>
34inline std::basic_string<xtd::wchar> __sprintf<xtd::wchar>(const xtd::wchar* fmt, ...) {
35 va_list args;
36 size_t size = 1024;
37 int length = 0;
38 std::basic_string<xtd::wchar> formatted_string;
39 bool error = false;
40 do {
41 formatted_string = std::basic_string<xtd::wchar>(size, 0);
42 va_start(args, fmt);
43 length = vswprintf(&formatted_string[0], formatted_string.size() + 1, fmt, args);
44 error = length < 0;
45 va_end(args);
46 size *= 2;
47 } while (error);
48 formatted_string.resize(length);
49 if (formatted_string == L"INF") formatted_string = {'i', 'n', 'f'};
50 if (formatted_string == L"-INF") formatted_string = {'-', 'i', 'n', 'f'};
51 if (formatted_string == L"NAN") formatted_string = {'n', 'a', 'n'};
52 return formatted_string;
53}
wchar_t wchar
Represents a wide character.
Definition wchar.h:24
size_t size
Represents a size of any object in bytes.
Definition size.h:23
@ error
Allows critical and error events through.