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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_STRINGUTILS_H_ |
| 12 | #define RTC_BASE_STRINGUTILS_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> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 23 | #define alloca _alloca |
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) |
| 27 | #ifdef BSD |
| 28 | #include <stdlib.h> |
| 29 | #else // BSD |
| 30 | #include <alloca.h> |
| 31 | #endif // !BSD |
| 32 | #endif // WEBRTC_POSIX |
| 33 | |
| 34 | #include <string> |
| 35 | |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | // Generic string/memory utilities |
| 38 | /////////////////////////////////////////////////////////////////////////////// |
| 39 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 40 | #define STACK_ARRAY(TYPE, LEN) \ |
| 41 | static_cast<TYPE*>(::alloca((LEN) * sizeof(TYPE))) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 42 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 43 | |
| 44 | #if defined(WEBRTC_POSIX) |
| 45 | |
| 46 | inline int _stricmp(const char* s1, const char* s2) { |
| 47 | return strcasecmp(s1, s2); |
| 48 | } |
| 49 | inline int _strnicmp(const char* s1, const char* s2, size_t n) { |
| 50 | return strncasecmp(s1, s2, n); |
| 51 | } |
| 52 | |
Jonas Olsson | 7439534 | 2018-04-03 12:22:07 +0200 | [diff] [blame] | 53 | #endif // WEBRTC_POSIX |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 54 | |
| 55 | /////////////////////////////////////////////////////////////////////////////// |
| 56 | // Traits simplifies porting string functions to be CTYPE-agnostic |
| 57 | /////////////////////////////////////////////////////////////////////////////// |
| 58 | |
| 59 | namespace rtc { |
| 60 | |
| 61 | const size_t SIZE_UNKNOWN = static_cast<size_t>(-1); |
| 62 | |
Niels Möller | d189252 | 2018-10-17 13:39:01 +0200 | [diff] [blame^] | 63 | // Safe version of strncpy that always nul-terminate. |
| 64 | size_t strcpyn(char* buffer, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 65 | size_t buflen, |
Niels Möller | d189252 | 2018-10-17 13:39:01 +0200 | [diff] [blame^] | 66 | const char* source, |
| 67 | size_t srclen = SIZE_UNKNOWN); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 68 | |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 69 | /////////////////////////////////////////////////////////////////////////////// |
| 70 | // UTF helpers (Windows only) |
| 71 | /////////////////////////////////////////////////////////////////////////////// |
| 72 | |
| 73 | #if defined(WEBRTC_WIN) |
| 74 | |
| 75 | inline std::wstring ToUtf16(const char* utf8, size_t len) { |
| 76 | int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), |
| 77 | nullptr, 0); |
| 78 | wchar_t* ws = STACK_ARRAY(wchar_t, len16); |
| 79 | ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), ws, len16); |
| 80 | return std::wstring(ws, len16); |
| 81 | } |
| 82 | |
| 83 | inline std::wstring ToUtf16(const std::string& str) { |
| 84 | return ToUtf16(str.data(), str.length()); |
| 85 | } |
| 86 | |
| 87 | inline std::string ToUtf8(const wchar_t* wide, size_t len) { |
| 88 | int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), |
| 89 | nullptr, 0, nullptr, nullptr); |
| 90 | char* ns = STACK_ARRAY(char, len8); |
| 91 | ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), ns, len8, |
| 92 | nullptr, nullptr); |
| 93 | return std::string(ns, len8); |
| 94 | } |
| 95 | |
| 96 | inline std::string ToUtf8(const wchar_t* wide) { |
| 97 | return ToUtf8(wide, wcslen(wide)); |
| 98 | } |
| 99 | |
| 100 | inline std::string ToUtf8(const std::wstring& wstr) { |
| 101 | return ToUtf8(wstr.data(), wstr.length()); |
| 102 | } |
| 103 | |
| 104 | #endif // WEBRTC_WIN |
| 105 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 106 | // Replaces all occurrences of "search" with "replace". |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 107 | void replace_substrs(const char* search, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 108 | size_t search_len, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 109 | const char* replace, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 110 | size_t replace_len, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 111 | std::string* s); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 112 | |
| 113 | // True iff s1 starts with s2. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 114 | bool starts_with(const char* s1, const char* s2); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 115 | |
| 116 | // True iff s1 ends with s2. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 117 | bool ends_with(const char* s1, const char* s2); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 118 | |
| 119 | // Remove leading and trailing whitespaces. |
| 120 | std::string string_trim(const std::string& s); |
| 121 | |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 122 | // TODO(jonasolsson): replace with absl::Hex when that becomes available. |
Jonas Olsson | 7439534 | 2018-04-03 12:22:07 +0200 | [diff] [blame] | 123 | std::string ToHex(const int i); |
Jonas Olsson | d8c5078 | 2018-09-07 11:21:28 +0200 | [diff] [blame] | 124 | |
| 125 | std::string LeftPad(char padding, unsigned length, std::string s); |
| 126 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 127 | } // namespace rtc |
| 128 | |
Jonas Olsson | 7439534 | 2018-04-03 12:22:07 +0200 | [diff] [blame] | 129 | #endif // RTC_BASE_STRINGUTILS_H_ |