icu_string_conversions.h revision c7f5f8508d98d5952d42ed7648c2a8f30a4da156
1// Copyright (c) 2009 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
5#ifndef BASE_I18N_ICU_STRING_CONVERSIONS_H_
6#define BASE_I18N_ICU_STRING_CONVERSIONS_H_
7
8#include <string>
9
10#include "base/string16.h"
11
12namespace base {
13
14// Defines the error handling modes of UTF16ToCodepage, CodepageToUTF16,
15// WideToCodepage and CodepageToWide.
16class OnStringConversionError {
17 public:
18  enum Type {
19    // The function will return failure. The output buffer will be empty.
20    FAIL,
21
22    // The offending characters are skipped and the conversion will proceed as
23    // if they did not exist.
24    SKIP,
25
26    // When converting to Unicode, the offending byte sequences are substituted
27    // by Unicode replacement character (U+FFFD). When converting from Unicode,
28    // this is the same as SKIP.
29    SUBSTITUTE,
30  };
31
32 private:
33  OnStringConversionError();
34};
35
36// Names of codepages (charsets) understood by icu.
37extern const char kCodepageLatin1[];  // a.k.a. ISO 8859-1
38extern const char kCodepageUTF8[];
39extern const char kCodepageUTF16BE[];
40extern const char kCodepageUTF16LE[];
41
42// Converts between UTF-16 strings and the encoding specified.  If the
43// encoding doesn't exist or the encoding fails (when on_error is FAIL),
44// returns false.
45bool UTF16ToCodepage(const string16& utf16,
46                     const char* codepage_name,
47                     OnStringConversionError::Type on_error,
48                     std::string* encoded);
49bool CodepageToUTF16(const std::string& encoded,
50                     const char* codepage_name,
51                     OnStringConversionError::Type on_error,
52                     string16* utf16);
53
54// Converts between wide strings and the encoding specified.  If the
55// encoding doesn't exist or the encoding fails (when on_error is FAIL),
56// returns false.
57bool WideToCodepage(const std::wstring& wide,
58                    const char* codepage_name,
59                    OnStringConversionError::Type on_error,
60                    std::string* encoded);
61bool CodepageToWide(const std::string& encoded,
62                    const char* codepage_name,
63                    OnStringConversionError::Type on_error,
64                    std::wstring* wide);
65
66}  // namespace base
67
68#endif  // BASE_I18N_ICU_STRING_CONVERSIONS_H_
69