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 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 63 | template <class CTYPE> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 64 | struct Traits { |
| 65 | // STL string type |
Jonas Olsson | 7439534 | 2018-04-03 12:22:07 +0200 | [diff] [blame] | 66 | // typedef XXX string; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 67 | // Null-terminated string |
Jonas Olsson | 7439534 | 2018-04-03 12:22:07 +0200 | [diff] [blame] | 68 | // inline static const CTYPE* empty_str(); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | /////////////////////////////////////////////////////////////////////////////// |
| 72 | // String utilities which work with char or wchar_t |
| 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | |
| 75 | template <class CTYPE> |
| 76 | inline const CTYPE* nonnull(const CTYPE* str, const CTYPE* def_str = nullptr) { |
| 77 | return str ? str : (def_str ? def_str : Traits<CTYPE>::empty_str()); |
| 78 | } |
| 79 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 80 | template <class CTYPE> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 81 | const CTYPE* strchr(const CTYPE* str, const CTYPE* chs) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 82 | for (size_t i = 0; str[i]; ++i) { |
| 83 | for (size_t j = 0; chs[j]; ++j) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 84 | if (str[i] == chs[j]) { |
| 85 | return str + i; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | return 0; |
| 90 | } |
| 91 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 92 | template <class CTYPE> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 93 | const CTYPE* strchrn(const CTYPE* str, size_t slen, CTYPE ch) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 94 | for (size_t i = 0; i < slen && str[i]; ++i) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 95 | if (str[i] == ch) { |
| 96 | return str + i; |
| 97 | } |
| 98 | } |
| 99 | return 0; |
| 100 | } |
| 101 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 102 | template <class CTYPE> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 103 | size_t strlenn(const CTYPE* buffer, size_t buflen) { |
| 104 | size_t bufpos = 0; |
| 105 | while (buffer[bufpos] && (bufpos < buflen)) { |
| 106 | ++bufpos; |
| 107 | } |
| 108 | return bufpos; |
| 109 | } |
| 110 | |
| 111 | // Safe versions of strncpy, strncat, snprintf and vsnprintf that always |
| 112 | // null-terminate. |
| 113 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 114 | template <class CTYPE> |
| 115 | size_t strcpyn(CTYPE* buffer, |
| 116 | size_t buflen, |
| 117 | const CTYPE* source, |
| 118 | size_t srclen = SIZE_UNKNOWN) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 119 | if (buflen <= 0) |
| 120 | return 0; |
| 121 | |
| 122 | if (srclen == SIZE_UNKNOWN) { |
| 123 | srclen = strlenn(source, buflen - 1); |
| 124 | } else if (srclen >= buflen) { |
| 125 | srclen = buflen - 1; |
| 126 | } |
| 127 | memcpy(buffer, source, srclen * sizeof(CTYPE)); |
| 128 | buffer[srclen] = 0; |
| 129 | return srclen; |
| 130 | } |
| 131 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 132 | template <class CTYPE> |
| 133 | size_t strcatn(CTYPE* buffer, |
| 134 | size_t buflen, |
| 135 | const CTYPE* source, |
| 136 | size_t srclen = SIZE_UNKNOWN) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 137 | if (buflen <= 0) |
| 138 | return 0; |
| 139 | |
| 140 | size_t bufpos = strlenn(buffer, buflen - 1); |
| 141 | return bufpos + strcpyn(buffer + bufpos, buflen - bufpos, source, srclen); |
| 142 | } |
| 143 | |
| 144 | // Some compilers (clang specifically) require vsprintfn be defined before |
| 145 | // sprintfn. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 146 | template <class CTYPE> |
| 147 | size_t vsprintfn(CTYPE* buffer, |
| 148 | size_t buflen, |
| 149 | const CTYPE* format, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 150 | va_list args) { |
| 151 | int len = vsnprintf(buffer, buflen, format, args); |
| 152 | if ((len < 0) || (static_cast<size_t>(len) >= buflen)) { |
| 153 | len = static_cast<int>(buflen - 1); |
| 154 | buffer[len] = 0; |
| 155 | } |
| 156 | return len; |
| 157 | } |
| 158 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 159 | template <class CTYPE> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 160 | size_t sprintfn(CTYPE* buffer, size_t buflen, const CTYPE* format, ...); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 161 | template <class CTYPE> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 162 | size_t sprintfn(CTYPE* buffer, size_t buflen, const CTYPE* format, ...) { |
| 163 | va_list args; |
| 164 | va_start(args, format); |
| 165 | size_t len = vsprintfn(buffer, buflen, format, args); |
| 166 | va_end(args); |
| 167 | return len; |
| 168 | } |
| 169 | |
| 170 | /////////////////////////////////////////////////////////////////////////////// |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 171 | // Traits<char> specializations |
| 172 | /////////////////////////////////////////////////////////////////////////////// |
| 173 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 174 | template <> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 175 | struct Traits<char> { |
| 176 | typedef std::string string; |
| 177 | inline static const char* empty_str() { return ""; } |
| 178 | }; |
| 179 | |
| 180 | /////////////////////////////////////////////////////////////////////////////// |
| 181 | // Traits<wchar_t> specializations (Windows only, currently) |
| 182 | /////////////////////////////////////////////////////////////////////////////// |
| 183 | |
| 184 | #if defined(WEBRTC_WIN) |
| 185 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 186 | template <> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 187 | struct Traits<wchar_t> { |
| 188 | typedef std::wstring string; |
| 189 | inline static const wchar_t* empty_str() { return L""; } |
| 190 | }; |
| 191 | |
Henrik Kjellander | c036276 | 2017-06-29 08:03:04 +0200 | [diff] [blame] | 192 | #endif // WEBRTC_WIN |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 193 | |
Patrik Höglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 194 | /////////////////////////////////////////////////////////////////////////////// |
| 195 | // UTF helpers (Windows only) |
| 196 | /////////////////////////////////////////////////////////////////////////////// |
| 197 | |
| 198 | #if defined(WEBRTC_WIN) |
| 199 | |
| 200 | inline std::wstring ToUtf16(const char* utf8, size_t len) { |
| 201 | int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), |
| 202 | nullptr, 0); |
| 203 | wchar_t* ws = STACK_ARRAY(wchar_t, len16); |
| 204 | ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), ws, len16); |
| 205 | return std::wstring(ws, len16); |
| 206 | } |
| 207 | |
| 208 | inline std::wstring ToUtf16(const std::string& str) { |
| 209 | return ToUtf16(str.data(), str.length()); |
| 210 | } |
| 211 | |
| 212 | inline std::string ToUtf8(const wchar_t* wide, size_t len) { |
| 213 | int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), |
| 214 | nullptr, 0, nullptr, nullptr); |
| 215 | char* ns = STACK_ARRAY(char, len8); |
| 216 | ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), ns, len8, |
| 217 | nullptr, nullptr); |
| 218 | return std::string(ns, len8); |
| 219 | } |
| 220 | |
| 221 | inline std::string ToUtf8(const wchar_t* wide) { |
| 222 | return ToUtf8(wide, wcslen(wide)); |
| 223 | } |
| 224 | |
| 225 | inline std::string ToUtf8(const std::wstring& wstr) { |
| 226 | return ToUtf8(wstr.data(), wstr.length()); |
| 227 | } |
| 228 | |
| 229 | #endif // WEBRTC_WIN |
| 230 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 231 | // Replaces all occurrences of "search" with "replace". |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 232 | void replace_substrs(const char* search, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 233 | size_t search_len, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 234 | const char* replace, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 235 | size_t replace_len, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 236 | std::string* s); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 237 | |
| 238 | // True iff s1 starts with s2. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 239 | bool starts_with(const char* s1, const char* s2); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 240 | |
| 241 | // True iff s1 ends with s2. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 242 | bool ends_with(const char* s1, const char* s2); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 243 | |
| 244 | // Remove leading and trailing whitespaces. |
| 245 | std::string string_trim(const std::string& s); |
| 246 | |
Jonas Olsson | abbe841 | 2018-04-03 13:40:05 +0200 | [diff] [blame] | 247 | // TODO(jonasolsson): replace with absl::Hex when that becomes available. |
Jonas Olsson | 7439534 | 2018-04-03 12:22:07 +0200 | [diff] [blame] | 248 | std::string ToHex(const int i); |
Jonas Olsson | d8c5078 | 2018-09-07 11:21:28 +0200 | [diff] [blame] | 249 | |
| 250 | std::string LeftPad(char padding, unsigned length, std::string s); |
| 251 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 252 | } // namespace rtc |
| 253 | |
Jonas Olsson | 7439534 | 2018-04-03 12:22:07 +0200 | [diff] [blame] | 254 | #endif // RTC_BASE_STRINGUTILS_H_ |