blob: 9534d59e041d3b58b5d261b72b9429ef3853519e [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 17:11:0011#ifndef RTC_BASE_STRING_UTILS_H_
12#define RTC_BASE_STRING_UTILS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Henrik Kjellanderec78f1c2017-06-29 05:52:5014#include <stdio.h>
15#include <string.h>
henrike@webrtc.orgf0488722014-05-13 18:00:2616
Ali Tofigh7fa90572022-03-17 14:47:4917#include "absl/strings/string_view.h"
18
Henrik Kjellanderec78f1c2017-06-29 05:52:5019#if defined(WEBRTC_WIN)
20#include <malloc.h>
21#include <wchar.h>
Patrik Höglunda8005cf2017-12-13 15:05:4222#include <windows.h>
Yves Gerey988cc082018-10-23 10:03:0123
Henrik Kjellanderc0362762017-06-29 06:03:0424#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:2625
Henrik Kjellanderec78f1c2017-06-29 05:52:5026#if defined(WEBRTC_POSIX)
Henrik Kjellanderec78f1c2017-06-29 05:52:5027#include <stdlib.h>
Yves Gerey988cc082018-10-23 10:03:0128#include <strings.h>
Henrik Kjellanderec78f1c2017-06-29 05:52:5029#endif // WEBRTC_POSIX
30
31#include <string>
32
Ali Tofigh7fa90572022-03-17 14:47:4933#include "absl/strings/string_view.h"
34
Henrik Kjellanderec78f1c2017-06-29 05:52:5035namespace rtc {
36
37const size_t SIZE_UNKNOWN = static_cast<size_t>(-1);
38
Ali Tofigh7fa90572022-03-17 14:47:4939// An absl::string_view comparator functor for use with container types such as
40// std::map that support heterogenous lookup.
41//
42// Example usage:
43// std::map<std::string, int, rtc::AbslStringViewCmp> my_map;
44struct AbslStringViewCmp {
45 using is_transparent = void;
46 bool operator()(absl::string_view a, absl::string_view b) const {
47 return a < b;
48 }
49};
50
Niels Möllerd1892522018-10-17 11:39:0151// Safe version of strncpy that always nul-terminate.
Ali Tofighe5b22202022-03-24 23:08:3252size_t strcpyn(char* buffer, size_t buflen, absl::string_view source);
Henrik Kjellanderec78f1c2017-06-29 05:52:5053
Patrik Höglunda8005cf2017-12-13 15:05:4254///////////////////////////////////////////////////////////////////////////////
55// UTF helpers (Windows only)
56///////////////////////////////////////////////////////////////////////////////
57
58#if defined(WEBRTC_WIN)
59
60inline std::wstring ToUtf16(const char* utf8, size_t len) {
Noah Richards4ed5b082019-07-30 18:56:0861 if (len == 0)
62 return std::wstring();
Patrik Höglunda8005cf2017-12-13 15:05:4263 int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len),
64 nullptr, 0);
Niels Möllerf25df352019-05-24 07:14:1365 std::wstring ws(len16, 0);
66 ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), &*ws.begin(),
67 len16);
68 return ws;
Patrik Höglunda8005cf2017-12-13 15:05:4269}
70
Ali Tofigh7fa90572022-03-17 14:47:4971inline std::wstring ToUtf16(absl::string_view str) {
Patrik Höglunda8005cf2017-12-13 15:05:4272 return ToUtf16(str.data(), str.length());
73}
74
75inline std::string ToUtf8(const wchar_t* wide, size_t len) {
Noah Richards4ed5b082019-07-30 18:56:0876 if (len == 0)
77 return std::string();
Patrik Höglunda8005cf2017-12-13 15:05:4278 int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len),
79 nullptr, 0, nullptr, nullptr);
Niels Möllerf25df352019-05-24 07:14:1380 std::string ns(len8, 0);
81 ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), &*ns.begin(),
82 len8, nullptr, nullptr);
83 return ns;
Patrik Höglunda8005cf2017-12-13 15:05:4284}
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
Jonas Olssonabbe8412018-04-03 11:40:0596// TODO(jonasolsson): replace with absl::Hex when that becomes available.
Ali Tofigh62238092022-01-25 12:27:1997std::string ToHex(int i);
Jonas Olssond8c50782018-09-07 09:21:2898
Markus Handell3d46d0b2021-05-27 19:42:5799// CompileTimeString comprises of a string-like object which can be used as a
100// regular const char* in compile time and supports concatenation. Useful for
101// concatenating constexpr strings in for example macro declarations.
102namespace rtc_base_string_utils_internal {
103template <int NPlus1>
104struct CompileTimeString {
105 char string[NPlus1] = {0};
106 constexpr CompileTimeString() = default;
107 template <int MPlus1>
108 explicit constexpr CompileTimeString(const char (&chars)[MPlus1]) {
109 char* chars_pointer = string;
110 for (auto c : chars)
111 *chars_pointer++ = c;
112 }
113 template <int MPlus1>
114 constexpr auto Concat(CompileTimeString<MPlus1> b) {
115 CompileTimeString<NPlus1 + MPlus1 - 1> result;
116 char* chars_pointer = result.string;
117 for (auto c : string)
118 *chars_pointer++ = c;
119 chars_pointer = result.string + NPlus1 - 1;
120 for (auto c : b.string)
121 *chars_pointer++ = c;
122 result.string[NPlus1 + MPlus1 - 2] = 0;
123 return result;
124 }
125 constexpr operator const char*() { return string; }
126};
127} // namespace rtc_base_string_utils_internal
128
129// Makes a constexpr CompileTimeString<X> without having to specify X
130// explicitly.
131template <int N>
132constexpr auto MakeCompileTimeString(const char (&a)[N]) {
133 return rtc_base_string_utils_internal::CompileTimeString<N>(a);
134}
135
Henrik Kjellanderec78f1c2017-06-29 05:52:50136} // namespace rtc
137
Steve Anton10542f22019-01-11 17:11:00138#endif // RTC_BASE_STRING_UTILS_H_