blob: b4ce209a990d160998b489beb3654f39c8cf66f8 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_STRING_UTILS_H_
12#define RTC_BASE_STRING_UTILS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <ctype.h>
15#include <stdarg.h>
16#include <stdio.h>
17#include <string.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019#if defined(WEBRTC_WIN)
20#include <malloc.h>
21#include <wchar.h>
Patrik Höglunda8005cf2017-12-13 16:05:42 +010022#include <windows.h>
Yves Gerey988cc082018-10-23 12:03:01 +020023
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024#define alloca _alloca
Henrik Kjellanderc0362762017-06-29 08:03:04 +020025#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020027#if defined(WEBRTC_POSIX)
28#ifdef BSD
29#include <stdlib.h>
30#else // BSD
31#include <alloca.h>
32#endif // !BSD
Yves Gerey988cc082018-10-23 12:03:01 +020033#include <strings.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020034#endif // WEBRTC_POSIX
35
36#include <string>
37
38///////////////////////////////////////////////////////////////////////////////
39// Generic string/memory utilities
40///////////////////////////////////////////////////////////////////////////////
41
Yves Gerey665174f2018-06-19 15:03:05 +020042#define STACK_ARRAY(TYPE, LEN) \
43 static_cast<TYPE*>(::alloca((LEN) * sizeof(TYPE)))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020044
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020045///////////////////////////////////////////////////////////////////////////////
46// Traits simplifies porting string functions to be CTYPE-agnostic
47///////////////////////////////////////////////////////////////////////////////
48
49namespace rtc {
50
51const size_t SIZE_UNKNOWN = static_cast<size_t>(-1);
52
Niels Möllerd1892522018-10-17 13:39:01 +020053// Safe version of strncpy that always nul-terminate.
54size_t strcpyn(char* buffer,
Yves Gerey665174f2018-06-19 15:03:05 +020055 size_t buflen,
Niels Möllerd1892522018-10-17 13:39:01 +020056 const char* source,
57 size_t srclen = SIZE_UNKNOWN);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020058
Patrik Höglunda8005cf2017-12-13 16:05:42 +010059///////////////////////////////////////////////////////////////////////////////
60// UTF helpers (Windows only)
61///////////////////////////////////////////////////////////////////////////////
62
63#if defined(WEBRTC_WIN)
64
65inline 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
73inline std::wstring ToUtf16(const std::string& str) {
74 return ToUtf16(str.data(), str.length());
75}
76
77inline 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
86inline std::string ToUtf8(const wchar_t* wide) {
87 return ToUtf8(wide, wcslen(wide));
88}
89
90inline std::string ToUtf8(const std::wstring& wstr) {
91 return ToUtf8(wstr.data(), wstr.length());
92}
93
94#endif // WEBRTC_WIN
95
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020096// Replaces all occurrences of "search" with "replace".
Yves Gerey665174f2018-06-19 15:03:05 +020097void replace_substrs(const char* search,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020098 size_t search_len,
Yves Gerey665174f2018-06-19 15:03:05 +020099 const char* replace,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200100 size_t replace_len,
Yves Gerey665174f2018-06-19 15:03:05 +0200101 std::string* s);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200102
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200103// Remove leading and trailing whitespaces.
104std::string string_trim(const std::string& s);
105
Jonas Olssonabbe8412018-04-03 13:40:05 +0200106// TODO(jonasolsson): replace with absl::Hex when that becomes available.
Jonas Olsson74395342018-04-03 12:22:07 +0200107std::string ToHex(const int i);
Jonas Olssond8c50782018-09-07 11:21:28 +0200108
109std::string LeftPad(char padding, unsigned length, std::string s);
110
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200111} // namespace rtc
112
Steve Anton10542f22019-01-11 09:11:00 -0800113#endif // RTC_BASE_STRING_UTILS_H_