blob: 45f3a3acd08b33b28da9e5cebca3855b05b4f4e0 [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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#ifndef RTC_BASE_STRINGUTILS_H_
12#define RTC_BASE_STRINGUTILS_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
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 Kjellanderec78f1c2017-06-29 05:52:5024#define alloca _alloca
Henrik Kjellanderc0362762017-06-29 06:03:0425#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:2626
Henrik Kjellanderec78f1c2017-06-29 05:52:5027#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 10:03:0133#include <strings.h>
Henrik Kjellanderec78f1c2017-06-29 05:52:5034#endif // WEBRTC_POSIX
35
36#include <string>
37
38///////////////////////////////////////////////////////////////////////////////
39// Generic string/memory utilities
40///////////////////////////////////////////////////////////////////////////////
41
Yves Gerey665174f2018-06-19 13:03:0542#define STACK_ARRAY(TYPE, LEN) \
43 static_cast<TYPE*>(::alloca((LEN) * sizeof(TYPE)))
Henrik Kjellanderec78f1c2017-06-29 05:52:5044
Henrik Kjellanderec78f1c2017-06-29 05:52:5045
46#if defined(WEBRTC_POSIX)
47
Henrik Kjellanderec78f1c2017-06-29 05:52:5048inline int _strnicmp(const char* s1, const char* s2, size_t n) {
49 return strncasecmp(s1, s2, n);
50}
51
Jonas Olsson74395342018-04-03 10:22:0752#endif // WEBRTC_POSIX
Henrik Kjellanderec78f1c2017-06-29 05:52:5053
54///////////////////////////////////////////////////////////////////////////////
55// Traits simplifies porting string functions to be CTYPE-agnostic
56///////////////////////////////////////////////////////////////////////////////
57
58namespace rtc {
59
60const size_t SIZE_UNKNOWN = static_cast<size_t>(-1);
61
Niels Möllerd1892522018-10-17 11:39:0162// Safe version of strncpy that always nul-terminate.
63size_t strcpyn(char* buffer,
Yves Gerey665174f2018-06-19 13:03:0564 size_t buflen,
Niels Möllerd1892522018-10-17 11:39:0165 const char* source,
66 size_t srclen = SIZE_UNKNOWN);
Henrik Kjellanderec78f1c2017-06-29 05:52:5067
Patrik Höglunda8005cf2017-12-13 15:05:4268///////////////////////////////////////////////////////////////////////////////
69// UTF helpers (Windows only)
70///////////////////////////////////////////////////////////////////////////////
71
72#if defined(WEBRTC_WIN)
73
74inline std::wstring ToUtf16(const char* utf8, size_t len) {
75 int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len),
76 nullptr, 0);
77 wchar_t* ws = STACK_ARRAY(wchar_t, len16);
78 ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), ws, len16);
79 return std::wstring(ws, len16);
80}
81
82inline std::wstring ToUtf16(const std::string& str) {
83 return ToUtf16(str.data(), str.length());
84}
85
86inline std::string ToUtf8(const wchar_t* wide, size_t len) {
87 int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len),
88 nullptr, 0, nullptr, nullptr);
89 char* ns = STACK_ARRAY(char, len8);
90 ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), ns, len8,
91 nullptr, nullptr);
92 return std::string(ns, len8);
93}
94
95inline std::string ToUtf8(const wchar_t* wide) {
96 return ToUtf8(wide, wcslen(wide));
97}
98
99inline std::string ToUtf8(const std::wstring& wstr) {
100 return ToUtf8(wstr.data(), wstr.length());
101}
102
103#endif // WEBRTC_WIN
104
Henrik Kjellanderec78f1c2017-06-29 05:52:50105// Replaces all occurrences of "search" with "replace".
Yves Gerey665174f2018-06-19 13:03:05106void replace_substrs(const char* search,
Henrik Kjellanderec78f1c2017-06-29 05:52:50107 size_t search_len,
Yves Gerey665174f2018-06-19 13:03:05108 const char* replace,
Henrik Kjellanderec78f1c2017-06-29 05:52:50109 size_t replace_len,
Yves Gerey665174f2018-06-19 13:03:05110 std::string* s);
Henrik Kjellanderec78f1c2017-06-29 05:52:50111
112// True iff s1 starts with s2.
Yves Gerey665174f2018-06-19 13:03:05113bool starts_with(const char* s1, const char* s2);
Henrik Kjellanderec78f1c2017-06-29 05:52:50114
115// True iff s1 ends with s2.
Yves Gerey665174f2018-06-19 13:03:05116bool ends_with(const char* s1, const char* s2);
Henrik Kjellanderec78f1c2017-06-29 05:52:50117
118// Remove leading and trailing whitespaces.
119std::string string_trim(const std::string& s);
120
Jonas Olssonabbe8412018-04-03 11:40:05121// TODO(jonasolsson): replace with absl::Hex when that becomes available.
Jonas Olsson74395342018-04-03 10:22:07122std::string ToHex(const int i);
Jonas Olssond8c50782018-09-07 09:21:28123
124std::string LeftPad(char padding, unsigned length, std::string s);
125
Henrik Kjellanderec78f1c2017-06-29 05:52:50126} // namespace rtc
127
Jonas Olsson74395342018-04-03 10:22:07128#endif // RTC_BASE_STRINGUTILS_H_