1 | // Copyright (C) 2019 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #include "codechunk.h" |
5 | |
6 | QT_BEGIN_NAMESPACE |
7 | |
8 | enum { Other, Alnum, Gizmo, Comma, LBrace, RBrace, RAngle, Colon, Paren }; |
9 | |
10 | // entries 128 and above are Other |
11 | static const int charCategory[256] = { Other, Other, Other, Other, Other, Other, Other, Other, |
12 | Other, Other, Other, Other, Other, Other, Other, Other, |
13 | Other, Other, Other, Other, Other, Other, Other, Other, |
14 | Other, Other, Other, Other, Other, Other, Other, Other, |
15 | // ! " # $ % & ' |
16 | Other, Other, Other, Other, Other, Gizmo, Gizmo, Other, |
17 | // ( ) * + , - . / |
18 | Paren, Paren, Gizmo, Gizmo, Comma, Other, Other, Gizmo, |
19 | // 0 1 2 3 4 5 6 7 |
20 | Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, |
21 | // 8 9 : ; < = > ? |
22 | Alnum, Alnum, Colon, Other, Other, Gizmo, RAngle, Gizmo, |
23 | // @ A B C D E F G |
24 | Other, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, |
25 | // H I J K L M N O |
26 | Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, |
27 | // P Q R S T U V W |
28 | Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, |
29 | // X Y Z [ \ ] ^ _ |
30 | Alnum, Alnum, Alnum, Other, Other, Other, Gizmo, Alnum, |
31 | // ` a b c d e f g |
32 | Other, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, |
33 | // h i j k l m n o |
34 | Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, |
35 | // p q r s t u v w |
36 | Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, |
37 | // x y z { | } ~ |
38 | Alnum, Alnum, Alnum, LBrace, Gizmo, RBrace, Other, Other }; |
39 | |
40 | static const bool needSpace[9][9] = { |
41 | /* [ a + , { } > : ) */ |
42 | /* [ */ { false, false, false, false, false, true, false, false, false }, |
43 | /* a */ { false, true, true, false, false, true, false, false, false }, |
44 | /* + */ { false, true, false, false, false, true, false, true, false }, |
45 | /* , */ { true, true, true, true, true, true, true, true, false }, |
46 | /* { */ { false, false, false, false, false, false, false, false, false }, |
47 | /* } */ { false, false, false, false, false, false, false, false, false }, |
48 | /* > */ { true, true, true, false, true, true, true, false, false }, |
49 | /* : */ { false, false, true, true, true, true, true, false, false }, |
50 | /* ( */ { false, false, false, false, false, false, false, false, false }, |
51 | }; |
52 | |
53 | static int category(QChar ch) |
54 | { |
55 | return charCategory[static_cast<int>(ch.toLatin1())]; |
56 | } |
57 | |
58 | /*! |
59 | \class CodeChunk |
60 | |
61 | \brief The CodeChunk class represents a tiny piece of C++ code. |
62 | |
63 | \note I think this class should be eliminated (mws 11/12/2018 |
64 | |
65 | The class provides conversion between a list of lexemes and a string. It adds |
66 | spaces at the right place for consistent style. The tiny pieces of code it |
67 | represents are data types, enum values, and default parameter values. |
68 | |
69 | Apart from the piece of code itself, there are two bits of metainformation |
70 | stored in CodeChunk: the base and the hotspot. The base is the part of the |
71 | piece that may be a hypertext link. The base of |
72 | |
73 | QMap<QString, QString> |
74 | |
75 | is QMap. |
76 | |
77 | The hotspot is the place the variable name should be inserted in the case of a |
78 | variable (or parameter) declaration. The hotspot of |
79 | |
80 | char * [] |
81 | |
82 | is between '*' and '[]'. |
83 | */ |
84 | |
85 | /*! |
86 | Appends \a lexeme to the current string contents, inserting |
87 | a space if appropriate. |
88 | */ |
89 | void CodeChunk::append(const QString &lexeme) |
90 | { |
91 | if (!m_str.isEmpty() && !lexeme.isEmpty()) { |
92 | /* |
93 | Should there be a space or not between the code chunk so far and the |
94 | new lexeme? |
95 | */ |
96 | int cat1 = category(ch: m_str.at(i: m_str.size() - 1)); |
97 | int cat2 = category(ch: lexeme[0]); |
98 | if (needSpace[cat1][cat2]) |
99 | m_str += QLatin1Char(' '); |
100 | } |
101 | m_str += lexeme; |
102 | } |
103 | |
104 | QT_END_NAMESPACE |
105 | |