| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #ifndef RTC_BASE_STRINGUTILS_H_ |
| 12 | #define RTC_BASE_STRINGUTILS_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 | #define alloca _alloca |
| 25 | #endif // WEBRTC_WIN |
| 26 |
| 27 | #if defined(WEBRTC_POSIX) |
| 28 | #ifdef BSD |
| 29 | #include <stdlib.h> |
| 30 | #else // BSD |
| 31 | #include <alloca.h> |
| 32 | #endif // !BSD |
| 33 | #include <strings.h> |
| 34 | #endif // WEBRTC_POSIX |
| 35 |
| 36 | #include <string> |
| 37 |
| 38 | /////////////////////////////////////////////////////////////////////////////// |
| 39 | // Generic string/memory utilities |
| 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 |
| 42 | #define STACK_ARRAY(TYPE, LEN) \ |
| 43 | static_cast<TYPE*>(::alloca((LEN) * sizeof(TYPE))) |
| 44 |
| 45 | /////////////////////////////////////////////////////////////////////////////// |
| 46 | // Traits simplifies porting string functions to be CTYPE-agnostic |
| 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 |
| 49 | namespace rtc { |
| 50 |
| 51 | const size_t SIZE_UNKNOWN = static_cast<size_t>(-1); |
| 52 |
| 53 | // Safe version of strncpy that always nul-terminate. |
| 54 | size_t strcpyn(char* buffer, |
| 55 | size_t buflen, |
| 56 | const char* source, |
| 57 | size_t srclen = SIZE_UNKNOWN); |
| 58 |
| 59 | /////////////////////////////////////////////////////////////////////////////// |
| 60 | // UTF helpers (Windows only) |
| 61 | /////////////////////////////////////////////////////////////////////////////// |
| 62 |
| 63 | #if defined(WEBRTC_WIN) |
| 64 |
| 65 | inline std::wstring ToUtf16(const char* utf8, size_t len) { |
| 66 | int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), |
| 67 | nullptr, 0); |
| 68 | wchar_t* ws = STACK_ARRAY(wchar_t, len16); |
| 69 | ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), ws, len16); |
| 70 | return std::wstring(ws, len16); |
| 71 | } |
| 72 |
| 73 | inline std::wstring ToUtf16(const std::string& str) { |
| 74 | return ToUtf16(str.data(), str.length()); |
| 75 | } |
| 76 |
| 77 | inline std::string ToUtf8(const wchar_t* wide, size_t len) { |
| 78 | int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), |
| 79 | nullptr, 0, nullptr, nullptr); |
| 80 | char* ns = STACK_ARRAY(char, len8); |
| 81 | ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), ns, len8, |
| 82 | nullptr, nullptr); |
| 83 | return std::string(ns, len8); |
| 84 | } |
| 85 |
| 86 | inline std::string ToUtf8(const wchar_t* wide) { |
| 87 | return ToUtf8(wide, wcslen(wide)); |
| 88 | } |
| 89 |
| 90 | inline std::string ToUtf8(const std::wstring& wstr) { |
| 91 | return ToUtf8(wstr.data(), wstr.length()); |
| 92 | } |
| 93 |
| 94 | #endif // WEBRTC_WIN |
| 95 |
| 96 | // Replaces all occurrences of "search" with "replace". |
| 97 | void replace_substrs(const char* search, |
| 98 | size_t search_len, |
| 99 | const char* replace, |
| 100 | size_t replace_len, |
| 101 | std::string* s); |
| 102 |
| 103 | // True iff s1 starts with s2. |
| 104 | bool starts_with(const char* s1, const char* s2); |
| 105 |
| 106 | // True iff s1 ends with s2. |
| 107 | bool ends_with(const char* s1, const char* s2); |
| 108 |
| 109 | // Remove leading and trailing whitespaces. |
| 110 | std::string string_trim(const std::string& s); |
| 111 |
| 112 | // TODO(jonasolsson): replace with absl::Hex when that becomes available. |
| 113 | std::string ToHex(const int i); |
| 114 |
| 115 | std::string LeftPad(char padding, unsigned length, std::string s); |
| 116 |
| 117 | } // namespace rtc |
| 118 |
| 119 | #endif // RTC_BASE_STRINGUTILS_H_ |
| 120 |