sys_string_conversions.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/string16.h"
  13. #include "base/strings/string_piece.h"
  14. #include "build/build_config.h"
  15. #if defined(OS_APPLE)
  16. #include <CoreFoundation/CoreFoundation.h>
  17. #include "base/mac/scoped_cftyperef.h"
  18. #ifdef __OBJC__
  19. @class NSString;
  20. #else
  21. class NSString;
  22. #endif
  23. #endif // OS_APPLE
  24. namespace base {
  25. // Converts between wide and UTF-8 representations of a string. On error, the
  26. // result is system-dependent.
  27. BASE_EXPORT std::string SysWideToUTF8(const std::wstring& wide)
  28. WARN_UNUSED_RESULT;
  29. BASE_EXPORT std::wstring SysUTF8ToWide(StringPiece utf8) WARN_UNUSED_RESULT;
  30. // Converts between wide and the system multi-byte representations of a string.
  31. // DANGER: This will lose information and can change (on Windows, this can
  32. // change between reboots).
  33. BASE_EXPORT std::string SysWideToNativeMB(const std::wstring& wide)
  34. WARN_UNUSED_RESULT;
  35. BASE_EXPORT std::wstring SysNativeMBToWide(StringPiece native_mb)
  36. WARN_UNUSED_RESULT;
  37. // Windows-specific ------------------------------------------------------------
  38. #if defined(OS_WIN)
  39. // Converts between 8-bit and wide strings, using the given code page. The
  40. // code page identifier is one accepted by the Windows function
  41. // MultiByteToWideChar().
  42. BASE_EXPORT std::wstring SysMultiByteToWide(StringPiece mb, uint32_t code_page)
  43. WARN_UNUSED_RESULT;
  44. BASE_EXPORT std::string SysWideToMultiByte(const std::wstring& wide,
  45. uint32_t code_page)
  46. WARN_UNUSED_RESULT;
  47. #endif // defined(OS_WIN)
  48. // Mac-specific ----------------------------------------------------------------
  49. #if defined(OS_APPLE)
  50. // Converts between STL strings and CFStringRefs/NSStrings.
  51. // Creates a string, and returns it with a refcount of 1. You are responsible
  52. // for releasing it. Returns NULL on failure.
  53. BASE_EXPORT ScopedCFTypeRef<CFStringRef> SysUTF8ToCFStringRef(StringPiece utf8)
  54. WARN_UNUSED_RESULT;
  55. BASE_EXPORT ScopedCFTypeRef<CFStringRef> SysUTF16ToCFStringRef(
  56. StringPiece16 utf16) WARN_UNUSED_RESULT;
  57. // Same, but returns an autoreleased NSString.
  58. BASE_EXPORT NSString* SysUTF8ToNSString(StringPiece utf8) WARN_UNUSED_RESULT;
  59. BASE_EXPORT NSString* SysUTF16ToNSString(StringPiece16 utf16)
  60. WARN_UNUSED_RESULT;
  61. // Converts a CFStringRef to an STL string. Returns an empty string on failure.
  62. BASE_EXPORT std::string SysCFStringRefToUTF8(CFStringRef ref)
  63. WARN_UNUSED_RESULT;
  64. BASE_EXPORT string16 SysCFStringRefToUTF16(CFStringRef ref) WARN_UNUSED_RESULT;
  65. // Same, but accepts NSString input. Converts nil NSString* to the appropriate
  66. // string type of length 0.
  67. BASE_EXPORT std::string SysNSStringToUTF8(NSString* ref) WARN_UNUSED_RESULT;
  68. BASE_EXPORT string16 SysNSStringToUTF16(NSString* ref) WARN_UNUSED_RESULT;
  69. #endif // defined(OS_APPLE)
  70. } // namespace base
  71. #endif // BASE_STRINGS_SYS_STRING_CONVERSIONS_H_