| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#ifndef RTC_BASE_STRING_UTILS_H_ |
| 12 |
#define RTC_BASE_STRING_UTILS_H_ |
| 13 |
|
| 14 |
#include <ctype.h> |
| 15 |
#include <stdarg.h> |
| 16 |
#include <stdio.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#if defined(WEBRTC_WIN) |
| 20 |
#include <malloc.h> |
| 21 |
#include <wchar.h> |
| 22 |
#include <windows.h> |
| 23 |
|
| 24 |
#endif |
| 25 |
|
| 26 |
#if defined(WEBRTC_POSIX) |
| 27 |
#include <stdlib.h> |
| 28 |
#include <strings.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
#include <string> |
| 32 |
|
| 33 |
namespace rtc { |
| 34 |
|
| 35 |
const size_t SIZE_UNKNOWN = static_cast<size_t>(-1); |
| 36 |
|
| 37 |
|
| 38 |
size_t strcpyn(char* buffer, |
| 39 |
size_t buflen, |
| 40 |
const char* source, |
| 41 |
size_t srclen = SIZE_UNKNOWN); |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
#if defined(WEBRTC_WIN) |
| 48 |
|
| 49 |
inline std::wstring ToUtf16(const char* utf8, size_t len) { |
| 50 |
if (len == 0) |
| 51 |
return std::wstring(); |
| 52 |
int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), |
| 53 |
nullptr, 0); |
| 54 |
std::wstring ws(len16, 0); |
| 55 |
::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), &*ws.begin(), |
| 56 |
len16); |
| 57 |
return ws; |
| 58 |
} |
| 59 |
|
| 60 |
inline std::wstring ToUtf16(const std::string& str) { |
| 61 |
return ToUtf16(str.data(), str.length()); |
| 62 |
} |
| 63 |
|
| 64 |
inline std::string ToUtf8(const wchar_t* wide, size_t len) { |
| 65 |
if (len == 0) |
| 66 |
return std::string(); |
| 67 |
int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), |
| 68 |
nullptr, 0, nullptr, nullptr); |
| 69 |
std::string ns(len8, 0); |
| 70 |
::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), &*ns.begin(), |
| 71 |
len8, nullptr, nullptr); |
| 72 |
return ns; |
| 73 |
} |
| 74 |
|
| 75 |
inline std::string ToUtf8(const wchar_t* wide) { |
| 76 |
return ToUtf8(wide, wcslen(wide)); |
| 77 |
} |
| 78 |
|
| 79 |
inline std::string ToUtf8(const std::wstring& wstr) { |
| 80 |
return ToUtf8(wstr.data(), wstr.length()); |
| 81 |
} |
| 82 |
|
| 83 |
#endif |
| 84 |
|
| 85 |
|
| 86 |
std::string string_trim(const std::string& s); |
| 87 |
|
| 88 |
|
| 89 |
std::string ToHex(const int i); |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
#endif |