xref: /aosp_15_r20/external/libchrome/base/i18n/icu_string_conversions.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_I18N_ICU_STRING_CONVERSIONS_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_I18N_ICU_STRING_CONVERSIONS_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <string>
9*635a8641SAndroid Build Coastguard Worker 
10*635a8641SAndroid Build Coastguard Worker #include "base/i18n/base_i18n_export.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/i18n/i18n_constants.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/strings/string16.h"
13*635a8641SAndroid Build Coastguard Worker 
14*635a8641SAndroid Build Coastguard Worker namespace base {
15*635a8641SAndroid Build Coastguard Worker 
16*635a8641SAndroid Build Coastguard Worker // Defines the error handling modes of UTF16ToCodepage and CodepageToUTF16.
17*635a8641SAndroid Build Coastguard Worker class OnStringConversionError {
18*635a8641SAndroid Build Coastguard Worker  public:
19*635a8641SAndroid Build Coastguard Worker   enum Type {
20*635a8641SAndroid Build Coastguard Worker     // The function will return failure. The output buffer will be empty.
21*635a8641SAndroid Build Coastguard Worker     FAIL,
22*635a8641SAndroid Build Coastguard Worker 
23*635a8641SAndroid Build Coastguard Worker     // The offending characters are skipped and the conversion will proceed as
24*635a8641SAndroid Build Coastguard Worker     // if they did not exist.
25*635a8641SAndroid Build Coastguard Worker     SKIP,
26*635a8641SAndroid Build Coastguard Worker 
27*635a8641SAndroid Build Coastguard Worker     // When converting to Unicode, the offending byte sequences are substituted
28*635a8641SAndroid Build Coastguard Worker     // by Unicode replacement character (U+FFFD). When converting from Unicode,
29*635a8641SAndroid Build Coastguard Worker     // this is the same as SKIP.
30*635a8641SAndroid Build Coastguard Worker     SUBSTITUTE,
31*635a8641SAndroid Build Coastguard Worker   };
32*635a8641SAndroid Build Coastguard Worker 
33*635a8641SAndroid Build Coastguard Worker  private:
34*635a8641SAndroid Build Coastguard Worker   OnStringConversionError() = delete;
35*635a8641SAndroid Build Coastguard Worker };
36*635a8641SAndroid Build Coastguard Worker 
37*635a8641SAndroid Build Coastguard Worker // Converts between UTF-16 strings and the encoding specified.  If the
38*635a8641SAndroid Build Coastguard Worker // encoding doesn't exist or the encoding fails (when on_error is FAIL),
39*635a8641SAndroid Build Coastguard Worker // returns false.
40*635a8641SAndroid Build Coastguard Worker BASE_I18N_EXPORT bool UTF16ToCodepage(const string16& utf16,
41*635a8641SAndroid Build Coastguard Worker                                       const char* codepage_name,
42*635a8641SAndroid Build Coastguard Worker                                       OnStringConversionError::Type on_error,
43*635a8641SAndroid Build Coastguard Worker                                       std::string* encoded);
44*635a8641SAndroid Build Coastguard Worker BASE_I18N_EXPORT bool CodepageToUTF16(const std::string& encoded,
45*635a8641SAndroid Build Coastguard Worker                                       const char* codepage_name,
46*635a8641SAndroid Build Coastguard Worker                                       OnStringConversionError::Type on_error,
47*635a8641SAndroid Build Coastguard Worker                                       string16* utf16);
48*635a8641SAndroid Build Coastguard Worker 
49*635a8641SAndroid Build Coastguard Worker // Converts from any codepage to UTF-8 and ensures the resulting UTF-8 is
50*635a8641SAndroid Build Coastguard Worker // normalized.
51*635a8641SAndroid Build Coastguard Worker BASE_I18N_EXPORT bool ConvertToUtf8AndNormalize(const std::string& text,
52*635a8641SAndroid Build Coastguard Worker                                                 const std::string& charset,
53*635a8641SAndroid Build Coastguard Worker                                                 std::string* result);
54*635a8641SAndroid Build Coastguard Worker 
55*635a8641SAndroid Build Coastguard Worker }  // namespace base
56*635a8641SAndroid Build Coastguard Worker 
57*635a8641SAndroid Build Coastguard Worker #endif  // BASE_I18N_ICU_STRING_CONVERSIONS_H_
58