DebugServer2
Loading...
Searching...
No Matches
String.h
1//
2// Copyright (c) 2014-present, Facebook, Inc.
3// All rights reserved.
4//
5// This source code is licensed under the University of Illinois/NCSA Open
6// Source License found in the LICENSE file in the root directory of this
7// source tree. An additional grant of patent rights can be found in the
8// PATENTS file in the same directory.
9//
10
11#pragma once
12
13#include "DebugServer2/Utils/CompilerSupport.h"
14
15#include <cstdarg>
16#include <cstdio>
17#include <sstream>
18#include <string>
19
20#if defined(OS_WIN32)
21#include <windows.h>
22#endif
23
24#define STR_HELPER(S) #S
25#define STR(S) STR_HELPER(S)
26
27namespace ds2 {
28namespace Utils {
29
30// Android doesn't have std::to_string, define our own implementation.
31template <typename T> static inline std::string ToString(T val) {
32 std::ostringstream os;
33 os << val;
34 return os.str();
35}
36
37// We can't add the printf attribute directly on the inline function
38// definition, we have to put it on a separate function declaration.
39static inline int VSNPrintf(char *str, size_t size, char const *format,
40 va_list ap) DS2_ATTRIBUTE_PRINTF(3, 0);
41static inline int SNPrintf(char *str, size_t size, char const *format, ...)
42 DS2_ATTRIBUTE_PRINTF(3, 4);
43
44#if defined(OS_WIN32) && !defined(PLATFORM_MINGW)
45// MSVC does not have snprintf, and has a vsnprintf that does not have the same
46// semantics as the linux one, which means we have to provide wrappers for
47// both.
48static inline int VSNPrintf(char *str, size_t size, char const *format,
49 va_list ap) {
50 int res = -1;
51 if (size > 0) {
52 res = _vsnprintf_s(str, size, _TRUNCATE, format, ap);
53 }
54 if (res == -1) {
55 res = _vscprintf(format, ap);
56 }
57 return res;
58}
59#elif defined(OS_POSIX) || (defined(OS_WIN32) && defined(PLATFORM_MINGW))
60// The posix systems we support, as well as MinGW (which provides a gnu
61// environment on Windows, with GNU semantics) have a sane vsnprintf. Use that.
62static inline int VSNPrintf(char *str, size_t size, char const *format,
63 va_list ap) {
64 return ::vsnprintf(str, size, format, ap);
65}
66#endif
67
68static inline int SNPrintf(char *str, size_t size, char const *format, ...) {
69 va_list ap;
70 va_start(ap, format);
71 int res = VSNPrintf(str, size, format, ap);
72 va_end(ap);
73 return res;
74}
75
76#if defined(OS_WIN32)
77static inline std::wstring NarrowToWideString(std::string const &s) {
78 std::vector<wchar_t> res;
79 int size;
80
81 size = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, nullptr, 0);
82 DS2ASSERT(size != 0);
83 res.resize(size);
84 MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, res.data(), size);
85 return res.data();
86}
87
88static inline std::string WideToNarrowString(std::wstring const &s) {
89 std::vector<char> res;
90 int size;
91
92 size = WideCharToMultiByte(CP_UTF8, 0, s.c_str(), -1, nullptr, 0, nullptr,
93 nullptr);
94 DS2ASSERT(size != 0);
95 res.resize(size);
96 WideCharToMultiByte(CP_UTF8, 0, s.c_str(), -1, res.data(), size, nullptr,
97 nullptr);
98 return res.data();
99}
100#endif
101
102#if defined(COMPILER_MSVC)
103#pragma deprecated(sprintf, snprintf, vsnprintf)
104#elif defined(COMPILER_CLANG) || defined(COMPILER_GCC)
105#pragma GCC poison sprintf snprintf vsnprintf
106#endif
107} // namespace Utils
108} // namespace ds2