string_utils.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  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. #ifndef RTC_BASE_STRING_UTILS_H_
  11. #define RTC_BASE_STRING_UTILS_H_
  12. #include <ctype.h>
  13. #include <stdarg.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #if defined(WEBRTC_WIN)
  17. #include <malloc.h>
  18. #include <wchar.h>
  19. #include <windows.h>
  20. #endif // WEBRTC_WIN
  21. #if defined(WEBRTC_POSIX)
  22. #include <stdlib.h>
  23. #include <strings.h>
  24. #endif // WEBRTC_POSIX
  25. #include <string>
  26. namespace rtc {
  27. const size_t SIZE_UNKNOWN = static_cast<size_t>(-1);
  28. // Safe version of strncpy that always nul-terminate.
  29. size_t strcpyn(char* buffer,
  30. size_t buflen,
  31. const char* source,
  32. size_t srclen = SIZE_UNKNOWN);
  33. ///////////////////////////////////////////////////////////////////////////////
  34. // UTF helpers (Windows only)
  35. ///////////////////////////////////////////////////////////////////////////////
  36. #if defined(WEBRTC_WIN)
  37. inline std::wstring ToUtf16(const char* utf8, size_t len) {
  38. if (len == 0)
  39. return std::wstring();
  40. int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len),
  41. nullptr, 0);
  42. std::wstring ws(len16, 0);
  43. ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), &*ws.begin(),
  44. len16);
  45. return ws;
  46. }
  47. inline std::wstring ToUtf16(const std::string& str) {
  48. return ToUtf16(str.data(), str.length());
  49. }
  50. inline std::string ToUtf8(const wchar_t* wide, size_t len) {
  51. if (len == 0)
  52. return std::string();
  53. int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len),
  54. nullptr, 0, nullptr, nullptr);
  55. std::string ns(len8, 0);
  56. ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), &*ns.begin(),
  57. len8, nullptr, nullptr);
  58. return ns;
  59. }
  60. inline std::string ToUtf8(const wchar_t* wide) {
  61. return ToUtf8(wide, wcslen(wide));
  62. }
  63. inline std::string ToUtf8(const std::wstring& wstr) {
  64. return ToUtf8(wstr.data(), wstr.length());
  65. }
  66. #endif // WEBRTC_WIN
  67. // Remove leading and trailing whitespaces.
  68. std::string string_trim(const std::string& s);
  69. // TODO(jonasolsson): replace with absl::Hex when that becomes available.
  70. std::string ToHex(const int i);
  71. } // namespace rtc
  72. #endif // RTC_BASE_STRING_UTILS_H_