Home | History | Annotate | Download | only in i18n
      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 
     12 namespace base {
     13 
     14 // Defines the error handling modes of UTF16ToCodepage, CodepageToUTF16,
     15 // WideToCodepage and CodepageToWide.
     16 class 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.
     37 extern const char kCodepageLatin1[];  // a.k.a. ISO 8859-1
     38 extern const char kCodepageUTF8[];
     39 extern const char kCodepageUTF16BE[];
     40 extern 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.
     45 bool UTF16ToCodepage(const string16& utf16,
     46                      const char* codepage_name,
     47                      OnStringConversionError::Type on_error,
     48                      std::string* encoded);
     49 bool 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.
     57 bool WideToCodepage(const std::wstring& wide,
     58                     const char* codepage_name,
     59                     OnStringConversionError::Type on_error,
     60                     std::string* encoded);
     61 bool 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