Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/QtCore/qstringconverter_base.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright (C) 2022 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef QSTRINGCONVERTER_BASE_H
0005 #define QSTRINGCONVERTER_BASE_H
0006 
0007 #if 0
0008 // QStringConverter(Base) class are handled in qstringconverter
0009 #pragma qt_sync_stop_processing
0010 #endif
0011 
0012 #include <optional>
0013 
0014 #include <QtCore/qglobal.h> // QT_{BEGIN,END}_NAMESPACE
0015 #include <QtCore/qflags.h> // Q_DECLARE_FLAGS
0016 #include <QtCore/qcontainerfwd.h>
0017 
0018 #include <cstring>
0019 
0020 QT_BEGIN_NAMESPACE
0021 
0022 class QByteArrayView;
0023 class QChar;
0024 class QByteArrayView;
0025 class QStringView;
0026 
0027 class QStringConverterBase
0028 {
0029 public:
0030     enum class Flag {
0031         Default = 0,
0032         Stateless = 0x1,
0033         ConvertInvalidToNull = 0x2,
0034         WriteBom = 0x4,
0035         ConvertInitialBom = 0x8,
0036         UsesIcu = 0x10,
0037     };
0038     Q_DECLARE_FLAGS(Flags, Flag)
0039 
0040     struct State {
0041         constexpr State(Flags f = Flag::Default) noexcept
0042             : flags(f), state_data{0, 0, 0, 0} {}
0043         ~State() { clear(); }
0044 
0045         State(State &&other) noexcept
0046             : flags(other.flags),
0047               remainingChars(other.remainingChars),
0048               invalidChars(other.invalidChars),
0049               state_data{other.state_data[0], other.state_data[1],
0050                          other.state_data[2], other.state_data[3]},
0051               clearFn(other.clearFn)
0052         { other.clearFn = nullptr; }
0053         State &operator=(State &&other) noexcept
0054         {
0055             clear();
0056             flags = other.flags;
0057             remainingChars = other.remainingChars;
0058             invalidChars = other.invalidChars;
0059             std::memmove(state_data, other.state_data, sizeof state_data); // self-assignment-safe
0060             clearFn = other.clearFn;
0061             other.clearFn = nullptr;
0062             return *this;
0063         }
0064         Q_CORE_EXPORT void clear() noexcept;
0065         Q_CORE_EXPORT void reset() noexcept;
0066 
0067         Flags flags;
0068         int internalState = 0;
0069         qsizetype remainingChars = 0;
0070         qsizetype invalidChars = 0;
0071 
0072         union {
0073             uint state_data[4];
0074             void *d[2];
0075         };
0076         using ClearDataFn = void (*)(State *) noexcept;
0077         ClearDataFn clearFn = nullptr;
0078     private:
0079         Q_DISABLE_COPY(State)
0080     };
0081 protected:
0082     ~QStringConverterBase() = default;
0083 };
0084 Q_DECLARE_OPERATORS_FOR_FLAGS(QStringConverterBase::Flags)
0085 
0086 class QStringConverter : public QStringConverterBase
0087 {
0088 public:
0089 
0090     enum Encoding {
0091         Utf8,
0092         Utf16,
0093         Utf16LE,
0094         Utf16BE,
0095         Utf32,
0096         Utf32LE,
0097         Utf32BE,
0098         Latin1,
0099         System,
0100         LastEncoding = System
0101     };
0102 #ifdef Q_QDOC
0103     // document the flags here
0104     enum class Flag {
0105         Default = 0,
0106         Stateless = 0x1,
0107         ConvertInvalidToNull = 0x2,
0108         WriteBom = 0x4,
0109         ConvertInitialBom = 0x8,
0110         UsesIcu = 0x10,
0111     };
0112     Q_DECLARE_FLAGS(Flags, Flag)
0113 #endif
0114 
0115 protected:
0116 
0117     struct Interface
0118     {
0119         using DecoderFn = QChar * (*)(QChar *out, QByteArrayView in, State *state);
0120         using LengthFn = qsizetype (*)(qsizetype inLength);
0121         using EncoderFn = char * (*)(char *out, QStringView in, State *state);
0122         const char *name = nullptr;
0123         DecoderFn toUtf16 = nullptr;
0124         LengthFn toUtf16Len = nullptr;
0125         EncoderFn fromUtf16 = nullptr;
0126         LengthFn fromUtf16Len = nullptr;
0127     };
0128 
0129     constexpr QStringConverter() noexcept
0130         : iface(nullptr)
0131     {}
0132     constexpr explicit QStringConverter(Encoding encoding, Flags f)
0133         : iface(&encodingInterfaces[qsizetype(encoding)]), state(f)
0134     {}
0135     constexpr explicit QStringConverter(const Interface *i) noexcept
0136         : iface(i)
0137     {}
0138     Q_CORE_EXPORT explicit QStringConverter(const char *name, Flags f);
0139 
0140 
0141     ~QStringConverter() = default;
0142 
0143 public:
0144     QStringConverter(QStringConverter &&) = default;
0145     QStringConverter &operator=(QStringConverter &&) = default;
0146 
0147     bool isValid() const noexcept { return iface != nullptr; }
0148 
0149     void resetState() noexcept
0150     {
0151         state.reset();
0152     }
0153     bool hasError() const noexcept { return state.invalidChars != 0; }
0154 
0155     Q_CORE_EXPORT const char *name() const noexcept;
0156 
0157     Q_CORE_EXPORT static std::optional<Encoding> encodingForName(const char *name) noexcept;
0158     Q_CORE_EXPORT static const char *nameForEncoding(Encoding e);
0159     Q_CORE_EXPORT static std::optional<Encoding>
0160     encodingForData(QByteArrayView data, char16_t expectedFirstCharacter = 0) noexcept;
0161     Q_CORE_EXPORT static std::optional<Encoding> encodingForHtml(QByteArrayView data);
0162 
0163     Q_CORE_EXPORT static QStringList availableCodecs();
0164 
0165 protected:
0166     const Interface *iface;
0167     State state;
0168 private:
0169     Q_CORE_EXPORT static const Interface encodingInterfaces[Encoding::LastEncoding + 1];
0170 };
0171 
0172 QT_END_NAMESPACE
0173 
0174 #endif