summaryrefslogtreecommitdiff
path: root/cpp/include/IceUtil/StringConverter.h
diff options
context:
space:
mode:
Diffstat (limited to 'cpp/include/IceUtil/StringConverter.h')
-rw-r--r--cpp/include/IceUtil/StringConverter.h102
1 files changed, 46 insertions, 56 deletions
diff --git a/cpp/include/IceUtil/StringConverter.h b/cpp/include/IceUtil/StringConverter.h
index 59621a58071..00af6bcf3c6 100644
--- a/cpp/include/IceUtil/StringConverter.h
+++ b/cpp/include/IceUtil/StringConverter.h
@@ -17,26 +17,15 @@
namespace IceUtil
{
-enum ConversionFlags
-{
- strictConversion = 0,
- lenientConversion
-};
-
-typedef unsigned char Byte;
-
-ICE_API bool
-isLegalUTF8Sequence(const Byte* source, const Byte* end);
-
//
-// Provides bytes to toUTF8. Can raise std::bad_alloc or Ice::MemoryLimitException
+// Provides bytes to toUTF8. Can raise std::bad_alloc or Ice::MemoryLimitException
// when too many bytes are requested.
//
class ICE_API UTF8Buffer
{
public:
virtual Byte* getMoreBytes(size_t howMany, Byte* firstUnused) = 0;
-
+
virtual ~UTF8Buffer() {}
};
@@ -46,8 +35,12 @@ public:
// It report errors by raising IllegalConversionException or an exception raised
// by UTF8Buffer
//
+
template<typename charT>
-class BasicStringConverter : public IceUtil::Shared
+class BasicStringConverter
+#ifndef ICE_CPP11_MAPPING
+ : public IceUtil::Shared
+#endif
{
public:
//
@@ -62,50 +55,31 @@ public:
//
virtual void fromUTF8(const Byte* sourceStart, const Byte* sourceEnd,
std::basic_string<charT>& target) const = 0;
+
+ virtual ~BasicStringConverter()
+ {
+ }
};
typedef BasicStringConverter<char> StringConverter;
-typedef IceUtil::Handle<StringConverter> StringConverterPtr;
+ICE_DEFINE_PTR(StringConverterPtr, StringConverter);
typedef BasicStringConverter<wchar_t> WstringConverter;
-typedef IceUtil::Handle<WstringConverter> WstringConverterPtr;
+ICE_DEFINE_PTR(WstringConverterPtr, WstringConverter);
//
-// Converts to and from UTF-16 or UTF-32 depending on sizeof(wchar_t)
+// Create a WstringConverter that converts to and from UTF-16 or UTF-32
+// depending on sizeof(wchar_t).
//
-class ICE_API UnicodeWstringConverter : public WstringConverter
-{
-public:
-
- UnicodeWstringConverter(ConversionFlags = lenientConversion);
-
- virtual Byte* toUTF8(const wchar_t*, const wchar_t*, UTF8Buffer&) const;
-
- virtual void fromUTF8(const Byte*, const Byte*, std::wstring&) const;
-
-private:
- const ConversionFlags _conversionFlags;
-};
+//
+ICE_API WstringConverterPtr createUnicodeWstringConverter();
#ifdef _WIN32
-
//
-// Converts to/from UTF-8 using MultiByteToWideChar and WideCharToMultiByte
+// Create a StringConverter that converts to and from narrow chars
+// in the given code page, using MultiByteToWideChar and WideCharToMultiByte
//
-class ICE_API WindowsStringConverter : public StringConverter
-{
-public:
-
- explicit WindowsStringConverter(unsigned int);
-
- virtual Byte* toUTF8(const char*, const char*, UTF8Buffer&) const;
-
- virtual void fromUTF8(const Byte*, const Byte*, std::string& target) const;
-
-private:
- unsigned int _cp;
- UnicodeWstringConverter _unicodeWstringConverter;
-};
+ICE_API StringConverterPtr createWindowsStringConverter(unsigned int);
#endif
//
@@ -132,26 +106,28 @@ ICE_API void setProcessWstringConverter(const WstringConverterPtr&);
//
// Converts the given wide string to a narrow string
//
-// If the StringConverter parameter is null, the result's narrow
-// string encoding is UTF-8.
-// If the WstringConverter parameter is null, the input's wstring
+// If the StringConverter parameter is null, the result's narrow
+// string encoding is UTF-8.
+// If the WstringConverter parameter is null, the input's wstring
// encoding is UTF-16 or UTF-32 depending on the size of wchar_t.
//
ICE_API std::string
-wstringToString(const std::wstring&, const StringConverterPtr& = 0,
- const WstringConverterPtr& = 0, ConversionFlags = lenientConversion);
+wstringToString(const std::wstring&,
+ const StringConverterPtr& = 0,
+ const WstringConverterPtr& = 0);
//
// Converts the given narrow string to a wide string
//
-// If the StringConverter parameter is null, the input's narrow string
-// encoding is UTF-8.
-// If the WstringConverter parameter is null, the result's wstring
+// If the StringConverter parameter is null, the input's narrow string
+// encoding is UTF-8.
+// If the WstringConverter parameter is null, the result's wstring
// encoding is UTF-16 or UTF-32 depending on the size of wchar_t.
//
ICE_API std::wstring
-stringToWstring(const std::string&, const StringConverterPtr& = 0,
- const WstringConverterPtr& = 0, ConversionFlags = lenientConversion);
+stringToWstring(const std::string&,
+ const StringConverterPtr& = 0,
+ const WstringConverterPtr& = 0);
//
@@ -172,4 +148,18 @@ UTF8ToNative(const std::string&, const StringConverterPtr&);
}
+namespace IceUtilInternal
+{
+//
+// Convert from UTF-8 to UTF-16/32
+//
+ICE_API std::vector<unsigned short> toUTF16(const std::vector<IceUtil::Byte>&);
+ICE_API std::vector<unsigned int> toUTF32(const std::vector<IceUtil::Byte>&);
+
+//
+// Convert from UTF-32 to UTF-8
+//
+ICE_API std::vector<IceUtil::Byte> fromUTF32(const std::vector<unsigned int>&);
+}
+
#endif