sys_string_conversions.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_STRINGS_SYS_STRING_CONVERSIONS_H_
  5. #define BASE_STRINGS_SYS_STRING_CONVERSIONS_H_
  6. // Provides system-dependent string type conversions for cases where it's
  7. // necessary to not use ICU. Generally, you should not need this in Chrome,
  8. // but it is used in some shared code. Dependencies should be minimal.
  9. #include <stdint.h>
  10. #include <string>
  11. #include "base/base_export.h"
  12. #include "base/strings/string_piece.h"
  13. #include "build/build_config.h"
  14. #if BUILDFLAG(IS_APPLE)
  15. #include <CoreFoundation/CoreFoundation.h>
  16. #include "base/mac/scoped_cftyperef.h"
  17. #ifdef __OBJC__
  18. @class NSString;
  19. #endif
  20. #endif // BUILDFLAG(IS_APPLE)
  21. namespace base {
  22. // Converts between wide and UTF-8 representations of a string. On error, the
  23. // result is system-dependent.
  24. [[nodiscard]] BASE_EXPORT std::string SysWideToUTF8(const std::wstring& wide);
  25. [[nodiscard]] BASE_EXPORT std::wstring SysUTF8ToWide(StringPiece utf8);
  26. // Converts between wide and the system multi-byte representations of a string.
  27. // DANGER: This will lose information and can change (on Windows, this can
  28. // change between reboots).
  29. [[nodiscard]] BASE_EXPORT std::string SysWideToNativeMB(
  30. const std::wstring& wide);
  31. [[nodiscard]] BASE_EXPORT std::wstring SysNativeMBToWide(StringPiece native_mb);
  32. // Windows-specific ------------------------------------------------------------
  33. #if BUILDFLAG(IS_WIN)
  34. // Converts between 8-bit and wide strings, using the given code page. The
  35. // code page identifier is one accepted by the Windows function
  36. // MultiByteToWideChar().
  37. [[nodiscard]] BASE_EXPORT std::wstring SysMultiByteToWide(StringPiece mb,
  38. uint32_t code_page);
  39. [[nodiscard]] BASE_EXPORT std::string SysWideToMultiByte(
  40. const std::wstring& wide,
  41. uint32_t code_page);
  42. #endif // BUILDFLAG(IS_WIN)
  43. // Mac-specific ----------------------------------------------------------------
  44. #if BUILDFLAG(IS_APPLE)
  45. // Converts between strings and CFStringRefs/NSStrings.
  46. // Converts a string to a CFStringRef. Returns null on failure.
  47. [[nodiscard]] BASE_EXPORT ScopedCFTypeRef<CFStringRef> SysUTF8ToCFStringRef(
  48. StringPiece utf8);
  49. [[nodiscard]] BASE_EXPORT ScopedCFTypeRef<CFStringRef> SysUTF16ToCFStringRef(
  50. StringPiece16 utf16);
  51. // Converts a CFStringRef to a string. Returns an empty string on failure. It is
  52. // not valid to call these with a null `ref`.
  53. [[nodiscard]] BASE_EXPORT std::string SysCFStringRefToUTF8(CFStringRef ref);
  54. [[nodiscard]] BASE_EXPORT std::u16string SysCFStringRefToUTF16(CFStringRef ref);
  55. #ifdef __OBJC__
  56. // Converts a string to an autoreleased NSString. Returns nil on failure.
  57. [[nodiscard]] BASE_EXPORT NSString* SysUTF8ToNSString(StringPiece utf8);
  58. [[nodiscard]] BASE_EXPORT NSString* SysUTF16ToNSString(StringPiece16 utf16);
  59. // Converts an NSString to a string. Returns an empty string on failure or if
  60. // `ref` is nil.
  61. [[nodiscard]] BASE_EXPORT std::string SysNSStringToUTF8(NSString* ref);
  62. [[nodiscard]] BASE_EXPORT std::u16string SysNSStringToUTF16(NSString* ref);
  63. #endif // __OBJC__
  64. #endif // BUILDFLAG(IS_APPLE)
  65. } // namespace base
  66. #endif // BASE_STRINGS_SYS_STRING_CONVERSIONS_H_