webrtc/rtc_base/string_utils.h
Niels Möller f25df35e14 Reland "Delete STACK_ARRAY macro, and use of alloca"
This is a reland of 74b373f04a

Original change's description:
> Delete STACK_ARRAY macro, and use of alloca
> 
> Refactor the few uses of STACK_ARRAY to avoid an extra copy
> on the stack.
> 
> Bug: webrtc:6424
> Change-Id: I5c8f3c0381523db0ead31207d949df9a286c76ba
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/137806
> Commit-Queue: Niels Moller <nisse@webrtc.org>
> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#28038}

Bug: webrtc:6424
Change-Id: Id635ccdfae12157cbb3ab9089c5e4a9f77f742ce
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/138211
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28052}
2019-05-24 09:33:06 +00:00

91 lines
2.6 KiB
C++

/*
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef RTC_BASE_STRING_UTILS_H_
#define RTC_BASE_STRING_UTILS_H_
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#if defined(WEBRTC_WIN)
#include <malloc.h>
#include <wchar.h>
#include <windows.h>
#endif // WEBRTC_WIN
#if defined(WEBRTC_POSIX)
#include <stdlib.h>
#include <strings.h>
#endif // WEBRTC_POSIX
#include <string>
namespace rtc {
const size_t SIZE_UNKNOWN = static_cast<size_t>(-1);
// Safe version of strncpy that always nul-terminate.
size_t strcpyn(char* buffer,
size_t buflen,
const char* source,
size_t srclen = SIZE_UNKNOWN);
///////////////////////////////////////////////////////////////////////////////
// UTF helpers (Windows only)
///////////////////////////////////////////////////////////////////////////////
#if defined(WEBRTC_WIN)
inline std::wstring ToUtf16(const char* utf8, size_t len) {
int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len),
nullptr, 0);
std::wstring ws(len16, 0);
::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), &*ws.begin(),
len16);
return ws;
}
inline std::wstring ToUtf16(const std::string& str) {
return ToUtf16(str.data(), str.length());
}
inline std::string ToUtf8(const wchar_t* wide, size_t len) {
int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len),
nullptr, 0, nullptr, nullptr);
std::string ns(len8, 0);
::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), &*ns.begin(),
len8, nullptr, nullptr);
return ns;
}
inline std::string ToUtf8(const wchar_t* wide) {
return ToUtf8(wide, wcslen(wide));
}
inline std::string ToUtf8(const std::wstring& wstr) {
return ToUtf8(wstr.data(), wstr.length());
}
#endif // WEBRTC_WIN
// Remove leading and trailing whitespaces.
std::string string_trim(const std::string& s);
// TODO(jonasolsson): replace with absl::Hex when that becomes available.
std::string ToHex(const int i);
std::string LeftPad(char padding, unsigned length, std::string s);
} // namespace rtc
#endif // RTC_BASE_STRING_UTILS_H_