blob: a9cdd61a7b98262cce0eb5104cdd474823b97ca1 [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
Ali Tofigh7fa90572022-03-17 15:47:49 +010019#include "absl/strings/string_view.h"
20
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020021#if defined(WEBRTC_WIN)
22#include <malloc.h>
23#include <wchar.h>
Patrik Höglunda8005cf2017-12-13 16:05:42 +010024#include <windows.h>
Yves Gerey988cc082018-10-23 12:03:01 +020025
Henrik Kjellanderc0362762017-06-29 08:03:04 +020026#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020028#if defined(WEBRTC_POSIX)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029#include <stdlib.h>
Yves Gerey988cc082018-10-23 12:03:01 +020030#include <strings.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031#endif // WEBRTC_POSIX
32
33#include <string>
34
Ali Tofigh7fa90572022-03-17 15:47:49 +010035#include "absl/strings/string_view.h"
36
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020037namespace rtc {
38
39const size_t SIZE_UNKNOWN = static_cast<size_t>(-1);
40
Ali Tofigh7fa90572022-03-17 15:47:49 +010041// An absl::string_view comparator functor for use with container types such as
42// std::map that support heterogenous lookup.
43//
44// Example usage:
45// std::map<std::string, int, rtc::AbslStringViewCmp> my_map;
46struct AbslStringViewCmp {
47 using is_transparent = void;
48 bool operator()(absl::string_view a, absl::string_view b) const {
49 return a < b;
50 }
51};
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) {
Noah Richards4ed5b082019-07-30 11:56:08 -070066 if (len == 0)
67 return std::wstring();
Patrik Höglunda8005cf2017-12-13 16:05:42 +010068 int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len),
69 nullptr, 0);
Niels Möllerf25df352019-05-24 09:14:13 +020070 std::wstring ws(len16, 0);
71 ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), &*ws.begin(),
72 len16);
73 return ws;
Patrik Höglunda8005cf2017-12-13 16:05:42 +010074}
75
Ali Tofigh7fa90572022-03-17 15:47:49 +010076inline std::wstring ToUtf16(absl::string_view str) {
Patrik Höglunda8005cf2017-12-13 16:05:42 +010077 return ToUtf16(str.data(), str.length());
78}
79
80inline std::string ToUtf8(const wchar_t* wide, size_t len) {
Noah Richards4ed5b082019-07-30 11:56:08 -070081 if (len == 0)
82 return std::string();
Patrik Höglunda8005cf2017-12-13 16:05:42 +010083 int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len),
84 nullptr, 0, nullptr, nullptr);
Niels Möllerf25df352019-05-24 09:14:13 +020085 std::string ns(len8, 0);
86 ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), &*ns.begin(),
87 len8, nullptr, nullptr);
88 return ns;
Patrik Höglunda8005cf2017-12-13 16:05:42 +010089}
90
91inline std::string ToUtf8(const wchar_t* wide) {
92 return ToUtf8(wide, wcslen(wide));
93}
94
95inline std::string ToUtf8(const std::wstring& wstr) {
96 return ToUtf8(wstr.data(), wstr.length());
97}
98
99#endif // WEBRTC_WIN
100
Jonas Olssonabbe8412018-04-03 13:40:05 +0200101// TODO(jonasolsson): replace with absl::Hex when that becomes available.
Ali Tofigh62238092022-01-25 13:27:19 +0100102std::string ToHex(int i);
Jonas Olssond8c50782018-09-07 11:21:28 +0200103
Markus Handell3d46d0b2021-05-27 21:42:57 +0200104// CompileTimeString comprises of a string-like object which can be used as a
105// regular const char* in compile time and supports concatenation. Useful for
106// concatenating constexpr strings in for example macro declarations.
107namespace rtc_base_string_utils_internal {
108template <int NPlus1>
109struct CompileTimeString {
110 char string[NPlus1] = {0};
111 constexpr CompileTimeString() = default;
112 template <int MPlus1>
113 explicit constexpr CompileTimeString(const char (&chars)[MPlus1]) {
114 char* chars_pointer = string;
115 for (auto c : chars)
116 *chars_pointer++ = c;
117 }
118 template <int MPlus1>
119 constexpr auto Concat(CompileTimeString<MPlus1> b) {
120 CompileTimeString<NPlus1 + MPlus1 - 1> result;
121 char* chars_pointer = result.string;
122 for (auto c : string)
123 *chars_pointer++ = c;
124 chars_pointer = result.string + NPlus1 - 1;
125 for (auto c : b.string)
126 *chars_pointer++ = c;
127 result.string[NPlus1 + MPlus1 - 2] = 0;
128 return result;
129 }
130 constexpr operator const char*() { return string; }
131};
132} // namespace rtc_base_string_utils_internal
133
134// Makes a constexpr CompileTimeString<X> without having to specify X
135// explicitly.
136template <int N>
137constexpr auto MakeCompileTimeString(const char (&a)[N]) {
138 return rtc_base_string_utils_internal::CompileTimeString<N>(a);
139}
140
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200141} // namespace rtc
142
Steve Anton10542f22019-01-11 09:11:00 -0800143#endif // RTC_BASE_STRING_UTILS_H_