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