blob: 2f75c0978be11e76f183164432c3e2df0bd7ba06 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_STRINGUTILS_H_
12#define RTC_BASE_STRINGUTILS_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>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023#define alloca _alloca
Henrik Kjellanderc0362762017-06-29 08:03:04 +020024#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020026#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 Gerey665174f2018-06-19 15:03:05 +020040#define STACK_ARRAY(TYPE, LEN) \
41 static_cast<TYPE*>(::alloca((LEN) * sizeof(TYPE)))
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020042
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020043
44#if defined(WEBRTC_POSIX)
45
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020046inline int _strnicmp(const char* s1, const char* s2, size_t n) {
47 return strncasecmp(s1, s2, n);
48}
49
Jonas Olsson74395342018-04-03 12:22:07 +020050#endif // WEBRTC_POSIX
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020051
52///////////////////////////////////////////////////////////////////////////////
53// Traits simplifies porting string functions to be CTYPE-agnostic
54///////////////////////////////////////////////////////////////////////////////
55
56namespace rtc {
57
58const size_t SIZE_UNKNOWN = static_cast<size_t>(-1);
59
Niels Möllerd1892522018-10-17 13:39:01 +020060// Safe version of strncpy that always nul-terminate.
61size_t strcpyn(char* buffer,
Yves Gerey665174f2018-06-19 15:03:05 +020062 size_t buflen,
Niels Möllerd1892522018-10-17 13:39:01 +020063 const char* source,
64 size_t srclen = SIZE_UNKNOWN);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020065
Patrik Höglunda8005cf2017-12-13 16:05:42 +010066///////////////////////////////////////////////////////////////////////////////
67// UTF helpers (Windows only)
68///////////////////////////////////////////////////////////////////////////////
69
70#if defined(WEBRTC_WIN)
71
72inline std::wstring ToUtf16(const char* utf8, size_t len) {
73 int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len),
74 nullptr, 0);
75 wchar_t* ws = STACK_ARRAY(wchar_t, len16);
76 ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), ws, len16);
77 return std::wstring(ws, len16);
78}
79
80inline std::wstring ToUtf16(const std::string& str) {
81 return ToUtf16(str.data(), str.length());
82}
83
84inline std::string ToUtf8(const wchar_t* wide, size_t len) {
85 int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len),
86 nullptr, 0, nullptr, nullptr);
87 char* ns = STACK_ARRAY(char, len8);
88 ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), ns, len8,
89 nullptr, nullptr);
90 return std::string(ns, len8);
91}
92
93inline std::string ToUtf8(const wchar_t* wide) {
94 return ToUtf8(wide, wcslen(wide));
95}
96
97inline std::string ToUtf8(const std::wstring& wstr) {
98 return ToUtf8(wstr.data(), wstr.length());
99}
100
101#endif // WEBRTC_WIN
102
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200103// Replaces all occurrences of "search" with "replace".
Yves Gerey665174f2018-06-19 15:03:05 +0200104void replace_substrs(const char* search,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200105 size_t search_len,
Yves Gerey665174f2018-06-19 15:03:05 +0200106 const char* replace,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107 size_t replace_len,
Yves Gerey665174f2018-06-19 15:03:05 +0200108 std::string* s);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200109
110// True iff s1 starts with s2.
Yves Gerey665174f2018-06-19 15:03:05 +0200111bool starts_with(const char* s1, const char* s2);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200112
113// True iff s1 ends with s2.
Yves Gerey665174f2018-06-19 15:03:05 +0200114bool ends_with(const char* s1, const char* s2);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200115
116// Remove leading and trailing whitespaces.
117std::string string_trim(const std::string& s);
118
Jonas Olssonabbe8412018-04-03 13:40:05 +0200119// TODO(jonasolsson): replace with absl::Hex when that becomes available.
Jonas Olsson74395342018-04-03 12:22:07 +0200120std::string ToHex(const int i);
Jonas Olssond8c50782018-09-07 11:21:28 +0200121
122std::string LeftPad(char padding, unsigned length, std::string s);
123
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200124} // namespace rtc
125
Jonas Olsson74395342018-04-03 12:22:07 +0200126#endif // RTC_BASE_STRINGUTILS_H_