henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_STRING_UTILS_H_ |
| 12 | #define RTC_BASE_STRING_UTILS_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #include <ctype.h> |
| 15 | #include <stdarg.h> |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 18 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 19 | #if defined(WEBRTC_WIN) |
| 20 | #include <malloc.h> |
| 21 | #include <wchar.h> |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 22 | #include <windows.h> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 23 | |
Henrik Kjellander | c036276 | 2017-06-29 08:03:04 +0200 | [diff] [blame] | 24 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 25 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 26 | #if defined(WEBRTC_POSIX) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 27 | #include <stdlib.h> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 28 | #include <strings.h> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 29 | #endif // WEBRTC_POSIX |
| 30 | |
| 31 | #include <string> |
| 32 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 33 | namespace rtc { |
| 34 | |
| 35 | const size_t SIZE_UNKNOWN = static_cast<size_t>(-1); |
| 36 | |
Niels Möller | d189252 | 2018-10-17 13:39:01 +0200 | [diff] [blame] | 37 | // Safe version of strncpy that always nul-terminate. |
| 38 | size_t strcpyn(char* buffer, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 39 | size_t buflen, |
Niels Möller | d189252 | 2018-10-17 13:39:01 +0200 | [diff] [blame] | 40 | const char* source, |
| 41 | size_t srclen = SIZE_UNKNOWN); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 42 | |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 43 | /////////////////////////////////////////////////////////////////////////////// |
| 44 | // UTF helpers (Windows only) |
| 45 | /////////////////////////////////////////////////////////////////////////////// |
| 46 | |
| 47 | #if defined(WEBRTC_WIN) |
| 48 | |
| 49 | inline std::wstring ToUtf16(const char* utf8, size_t len) { |
Noah Richards | 4ed5b08 | 2019-07-30 11:56:08 -0700 | [diff] [blame] | 50 | if (len == 0) |
| 51 | return std::wstring(); |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 52 | int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), |
| 53 | nullptr, 0); |
Niels Möller | f25df35 | 2019-05-24 09:14:13 +0200 | [diff] [blame] | 54 | std::wstring ws(len16, 0); |
| 55 | ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), &*ws.begin(), |
| 56 | len16); |
| 57 | return ws; |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 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) { |
Noah Richards | 4ed5b08 | 2019-07-30 11:56:08 -0700 | [diff] [blame] | 65 | if (len == 0) |
| 66 | return std::string(); |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 67 | int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), |
| 68 | nullptr, 0, nullptr, nullptr); |
Niels Möller | f25df35 | 2019-05-24 09:14:13 +0200 | [diff] [blame] | 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; |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 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 // WEBRTC_WIN |
| 84 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 85 | // Remove leading and trailing whitespaces. |
| 86 | std::string string_trim(const std::string& s); |
| 87 | |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 88 | // TODO(jonasolsson): replace with absl::Hex when that becomes available. |
Jonas Olsson | 7439534 | 2018-04-03 12:22:07 +0200 | [diff] [blame] | 89 | std::string ToHex(const int i); |
Jonas Olsson | d8c5078 | 2018-09-07 11:21:28 +0200 | [diff] [blame] | 90 | |
| 91 | std::string LeftPad(char padding, unsigned length, std::string s); |
| 92 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 93 | } // namespace rtc |
| 94 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 95 | #endif // RTC_BASE_STRING_UTILS_H_ |