blob: d062675a940a32305348ff25751deac7c2ea610b [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 <ctype.h>
15#include <stdarg.h>
16#include <stdio.h>
17#include <string.h>
henrike@webrtc.orgf0488722014-05-13 18:00:2618
Ali Tofigh7fa90572022-03-17 14:47:4919#include "absl/strings/string_view.h"
20
Henrik Kjellanderec78f1c2017-06-29 05:52:5021#if defined(WEBRTC_WIN)
22#include <malloc.h>
23#include <wchar.h>
Patrik Höglunda8005cf2017-12-13 15:05:4224#include <windows.h>
Yves Gerey988cc082018-10-23 10:03:0125
Henrik Kjellanderc0362762017-06-29 06:03:0426#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:2627
Henrik Kjellanderec78f1c2017-06-29 05:52:5028#if defined(WEBRTC_POSIX)
Henrik Kjellanderec78f1c2017-06-29 05:52:5029#include <stdlib.h>
Yves Gerey988cc082018-10-23 10:03:0130#include <strings.h>
Henrik Kjellanderec78f1c2017-06-29 05:52:5031#endif // WEBRTC_POSIX
32
33#include <string>
34
Ali Tofigh7fa90572022-03-17 14:47:4935#include "absl/strings/string_view.h"
36
Henrik Kjellanderec78f1c2017-06-29 05:52:5037namespace rtc {
38
39const size_t SIZE_UNKNOWN = static_cast<size_t>(-1);
40
Ali Tofigh7fa90572022-03-17 14:47:4941// 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 11:39:0153// Safe version of strncpy that always nul-terminate.
Ali Tofighe5b22202022-03-24 23:08:3254size_t strcpyn(char* buffer, size_t buflen, absl::string_view source);
Henrik Kjellanderec78f1c2017-06-29 05:52:5055
Patrik Höglunda8005cf2017-12-13 15:05:4256///////////////////////////////////////////////////////////////////////////////
57// UTF helpers (Windows only)
58///////////////////////////////////////////////////////////////////////////////
59
60#if defined(WEBRTC_WIN)
61
62inline std::wstring ToUtf16(const char* utf8, size_t len) {
Noah Richards4ed5b082019-07-30 18:56:0863 if (len == 0)
64 return std::wstring();
Patrik Höglunda8005cf2017-12-13 15:05:4265 int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len),
66 nullptr, 0);
Niels Möllerf25df352019-05-24 07:14:1367 std::wstring ws(len16, 0);
68 ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), &*ws.begin(),
69 len16);
70 return ws;
Patrik Höglunda8005cf2017-12-13 15:05:4271}
72
Ali Tofigh7fa90572022-03-17 14:47:4973inline std::wstring ToUtf16(absl::string_view str) {
Patrik Höglunda8005cf2017-12-13 15:05:4274 return ToUtf16(str.data(), str.length());
75}
76
77inline std::string ToUtf8(const wchar_t* wide, size_t len) {
Noah Richards4ed5b082019-07-30 18:56:0878 if (len == 0)
79 return std::string();
Patrik Höglunda8005cf2017-12-13 15:05:4280 int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len),
81 nullptr, 0, nullptr, nullptr);
Niels Möllerf25df352019-05-24 07:14:1382 std::string ns(len8, 0);
83 ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), &*ns.begin(),
84 len8, nullptr, nullptr);
85 return ns;
Patrik Höglunda8005cf2017-12-13 15:05:4286}
87
88inline std::string ToUtf8(const wchar_t* wide) {
89 return ToUtf8(wide, wcslen(wide));
90}
91
92inline std::string ToUtf8(const std::wstring& wstr) {
93 return ToUtf8(wstr.data(), wstr.length());
94}
95
96#endif // WEBRTC_WIN
97
Jonas Olssonabbe8412018-04-03 11:40:0598// TODO(jonasolsson): replace with absl::Hex when that becomes available.
Ali Tofigh62238092022-01-25 12:27:1999std::string ToHex(int i);
Jonas Olssond8c50782018-09-07 09:21:28100
Markus Handell3d46d0b2021-05-27 19:42:57101// CompileTimeString comprises of a string-like object which can be used as a
102// regular const char* in compile time and supports concatenation. Useful for
103// concatenating constexpr strings in for example macro declarations.
104namespace rtc_base_string_utils_internal {
105template <int NPlus1>
106struct CompileTimeString {
107 char string[NPlus1] = {0};
108 constexpr CompileTimeString() = default;
109 template <int MPlus1>
110 explicit constexpr CompileTimeString(const char (&chars)[MPlus1]) {
111 char* chars_pointer = string;
112 for (auto c : chars)
113 *chars_pointer++ = c;
114 }
115 template <int MPlus1>
116 constexpr auto Concat(CompileTimeString<MPlus1> b) {
117 CompileTimeString<NPlus1 + MPlus1 - 1> result;
118 char* chars_pointer = result.string;
119 for (auto c : string)
120 *chars_pointer++ = c;
121 chars_pointer = result.string + NPlus1 - 1;
122 for (auto c : b.string)
123 *chars_pointer++ = c;
124 result.string[NPlus1 + MPlus1 - 2] = 0;
125 return result;
126 }
127 constexpr operator const char*() { return string; }
128};
129} // namespace rtc_base_string_utils_internal
130
131// Makes a constexpr CompileTimeString<X> without having to specify X
132// explicitly.
133template <int N>
134constexpr auto MakeCompileTimeString(const char (&a)[N]) {
135 return rtc_base_string_utils_internal::CompileTimeString<N>(a);
136}
137
Henrik Kjellanderec78f1c2017-06-29 05:52:50138} // namespace rtc
139
Steve Anton10542f22019-01-11 17:11:00140#endif // RTC_BASE_STRING_UTILS_H_