zoukankan      html  css  js  c++  java
  • Qt

    在Qt中与字符串操作相关的类有QLatin1Char,QLatin1String,QChar,QString,QByteArrayQStringRef, QStringList,QStringMatcher,QByteArrayMatcher

    (1)QLatin1Char

    QLatin1Char是个结构体,存储一个8位ASCII/Latin-1编码的字符,数据的存储类型为char

    1 struct QLatin1Char
    2 {
    3 public:
    4     inline explicit QLatin1Char(char c) : ch(c) {}
    5     ......
    6 private:
    7     char ch;
    8 };

    (2)QLatin1String

    QLatin1String类提供了对US-ASCII/Latin-1编码字符串常量的封装

     1 class Q_CORE_EXPORT QLatin1String
     2 {
     3 public:
     4     inline explicit QLatin1String(const char *s) : chars(s) {}
     5     ......
     6     inline const char *latin1() const { return chars; }
     7     ......
     8 private:
     9     const char *chars;
    10 };

    从类声明中可以看出,QLatin1String将数据存储为const char*的数据类型

    (3)QChar

    1 class Q_CORE_EXPORT QChar 
    2 {
    3     ......
    4 private:
    5     ......
    6     ushort ucs;
    7 };

    QChar存储一个16位(2个字节)的Unicode字符,数据的存储类型为ushort(unsigned short)

    (4)QString

    QString是Unicode编码的字符串,存储一系列16位的QChar,每一个QChar对应一个Unicode 4.0编码的字符

     1 class Q_CORE_EXPORT QString
     2 {
     3     ......
     4 private:
     5     ......
     6     struct Data {
     7         QBasicAtomicInt ref;
     8         int alloc, size;
     9         ushort *data; // QT5: put that after the bit field to fill alignment gap; don't use sizeof any more then
    10         ushort clean : 1;
    11         ushort simpletext : 1;
    12         ushort righttoleft : 1;
    13         ushort asciiCache : 1;
    14         ushort capacity : 1;
    15         ushort reserved : 11;
    16         // ### Qt5: try to ensure that "array" is aligned to 16 bytes on both 32- and 64-bit
    17         ushort array[1];
    18     };
    19     static Data shared_null;
    20     static Data shared_empty;
    21     Data *d;
    22     ......
    23 };
     1 QString::QString(const QChar *unicode, int size)
     2 {
     3    if (!unicode) {
     4         d = &shared_null;
     5         d->ref.ref();
     6     } else if (size <= 0) {
     7         d = &shared_empty;
     8         d->ref.ref();
     9     } else {
    10         d = (Data*) qMalloc(sizeof(Data)+size*sizeof(QChar));
    11         Q_CHECK_PTR(d);
    12         d->ref = 1;
    13         d->alloc = d->size = size;
    14         d->clean = d->asciiCache = d->simpletext = d->righttoleft = d->capacity = 0;
    15         d->data = d->array;
    16         memcpy(d->array, unicode, size * sizeof(QChar));
    17         d->array[size] = '';
    18     }
    19 }

    (5)QByteArray

    QByteArray是个字节数组,可以存储原始字节(包括一系列'')和传统的8位''结尾的字符串,每一个字节存储为char类型的数据;

    虽然QString用得更普遍和方便,但当需要存储原始的二进制数据或者内存保护要求严格时,用QByteArray比用QString更合适;

     1 class Q_CORE_EXPORT QByteArray
     2 {
     3 private:
     4     struct Data {
     5         QBasicAtomicInt ref;
     6         int alloc, size;
     7         // ### Qt 5.0: We need to add the missing capacity bit
     8         // (like other tool classes have), to maintain the
     9         // reserved memory on resize.
    10         char *data;
    11         char array[1];
    12     };
    13     ......
    14 private:
    15     ......
    16     static Data shared_null;
    17     static Data shared_empty;
    18     Data *d;
    19     ......
    20 }

    QByteArray始终会自动将数组最后一个字节存储为'',但不包括在数组长度的计算中;

     1 QByteArray::QByteArray(const char *str)
     2 {
     3     if (!str) {
     4         d = &shared_null;
     5     } else if (!*str) {
     6         d = &shared_empty;
     7     } else {
     8         int len = qstrlen(str);
     9         d = static_cast<Data *>(qMalloc(sizeof(Data)+len));
    10         Q_CHECK_PTR(d);
    11         d->ref = 0;;
    12         d->alloc = d->size = len;
    13         d->data = d->array;
    14         memcpy(d->array, str, len+1); // include null terminator
    15     }
    16     d->ref.ref();
    17 }

    (6)QStringRef

     QStringRef提供了对QString子字符串的封装,在一个QString字符串中指定起始位置和长度即可构造一个QStringRef

    1 class Q_CORE_EXPORT QStringRef
    2 {
    3     const QString *m_string;
    4     int m_position;
    5     int m_size;
    6 public:
    7     inline QStringRef():m_string(0), m_position(0), m_size(0){}
    8     ......
    9 };

    (7)QStringList

    1 class QStringList : public QList<QString>
    2 {
    3     ......
    4 };

    (8)QStringMatcher

     字符串匹配类,当需要循环反复匹配一个字符串或者一串字符,则可以用到这个类

     1 class Q_CORE_EXPORT QStringMatcher
     2 {
     3     ......
     4 private:
     5     QStringMatcherPrivate *d_ptr;
     6     QString q_pattern;
     7     Qt::CaseSensitivity q_cs;
     8 #ifdef Q_CC_RVCT
     9 // explicitly allow anonymous unions for RVCT to prevent compiler warnings
    10 #  pragma push
    11 #  pragma anon_unions
    12 #endif
    13     struct Data {
    14         uchar q_skiptable[256];
    15         const QChar *uc;
    16         int len;
    17     };
    18     union {
    19         uint q_data[256];
    20         Data p;
    21     };
    22 #ifdef Q_CC_RVCT
    23 #  pragma pop
    24 #endif    
    25 }

    (9)QByteArrayMatcher

    QByteArrayMatcher与QStringMatcher类似,不同之处在于它匹配的对象不是字符,是字节

     1 class Q_CORE_EXPORT QByteArrayMatcher
     2 {
     3     ......
     4 private:
     5     QByteArrayMatcherPrivate *d;
     6     QByteArray q_pattern;
     7 #ifdef Q_CC_RVCT
     8 // explicitly allow anonymous unions for RVCT to prevent compiler warnings
     9 #  pragma push
    10 #  pragma anon_unions
    11 #endif
    12     struct Data {
    13         uchar q_skiptable[256];
    14         const uchar *p;
    15         int l;
    16     };
    17     union {
    18         uint dummy[256];
    19         Data p;
    20     };
    21 #ifdef Q_CC_RVCT
    22 #  pragma pop
    23 #endif
    24 };
  • 相关阅读:
    人民币贬值之后该买什么
    elasticsearch系列(一): elasticsearch安装(docker方式)
    sql处理字符串: replace替换, substring_index拆分和处理json
    ab并发测试
    java反射--PropertyDescriptor类、Introspector类、BeanUtils库
    sql特殊用法: 查询添加, 查询更新, 添加或更新
    es查看和删除索引
    windows同步文件夹-增量同步最新修改
    mvn打包-指定模块
    grep过滤文本,awk,sort去重显示
  • 原文地址:https://www.cnblogs.com/paullam/p/3710487.html
Copyright © 2011-2022 走看看