Changeset View
Changeset View
Standalone View
Standalone View
src/engine/termgenerator.cpp
Show All 16 Lines | |||||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
18 | * | 18 | * | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "termgenerator.h" | 21 | #include "termgenerator.h" | ||
22 | #include "document.h" | 22 | #include "document.h" | ||
23 | 23 | | |||
24 | #include <QTextBoundaryFinder> | 24 | #include <QTextBoundaryFinder> | ||
25 | #include <QStringList> | 25 | #include <QByteArray> | ||
26 | 26 | | |||
27 | using namespace Baloo; | 27 | using namespace Baloo; | ||
28 | 28 | | |||
29 | TermGenerator::TermGenerator(Document& doc) | 29 | TermGenerator::TermGenerator(Document& doc) | ||
30 | : m_doc(doc) | 30 | : m_doc(doc) | ||
31 | , m_position(1) | 31 | , m_position(1) | ||
32 | { | 32 | { | ||
33 | } | 33 | } | ||
34 | 34 | | |||
35 | void TermGenerator::indexText(const QString& text) | 35 | void TermGenerator::indexText(const QString& text) | ||
36 | { | 36 | { | ||
37 | indexText(text, QByteArray()); | 37 | indexText(text, QByteArray()); | ||
38 | } | 38 | } | ||
39 | 39 | | |||
40 | QStringList TermGenerator::termList(const QString& text_) | 40 | QByteArrayList TermGenerator::termList(const QString& text_) | ||
41 | { | 41 | { | ||
42 | QString text(text_); | 42 | QString text(text_); | ||
43 | text.replace(QLatin1Char('_'), QLatin1Char(' ')); | 43 | text.replace(QLatin1Char('_'), QLatin1Char(' ')); | ||
44 | 44 | | |||
45 | int start = 0; | 45 | int start = 0; | ||
46 | int end = 0; | 46 | int end = 0; | ||
47 | 47 | | |||
48 | QStringList list; | 48 | QByteArrayList list; | ||
49 | QTextBoundaryFinder bf(QTextBoundaryFinder::Word, text); | 49 | QTextBoundaryFinder bf(QTextBoundaryFinder::Word, text); | ||
50 | for (; bf.position() != -1; bf.toNextBoundary()) { | 50 | for (; bf.position() != -1; bf.toNextBoundary()) { | ||
51 | if (bf.boundaryReasons() & QTextBoundaryFinder::StartOfItem) { | 51 | if (bf.boundaryReasons() & QTextBoundaryFinder::StartOfItem) { | ||
52 | start = bf.position(); | 52 | start = bf.position(); | ||
53 | continue; | 53 | continue; | ||
54 | } | 54 | } | ||
55 | else if (bf.boundaryReasons() & QTextBoundaryFinder::EndOfItem) { | 55 | else if (bf.boundaryReasons() & QTextBoundaryFinder::EndOfItem) { | ||
56 | end = bf.position(); | 56 | end = bf.position(); | ||
Show All 10 Lines | 66 | for (const QChar& ch : denormalized) { | |||
67 | auto cat = ch.category(); | 67 | auto cat = ch.category(); | ||
68 | if (cat != QChar::Mark_NonSpacing && cat != QChar::Mark_SpacingCombining && cat != QChar::Mark_Enclosing) { | 68 | if (cat != QChar::Mark_NonSpacing && cat != QChar::Mark_SpacingCombining && cat != QChar::Mark_Enclosing) { | ||
69 | cleanString.append(ch); | 69 | cleanString.append(ch); | ||
70 | } | 70 | } | ||
71 | } | 71 | } | ||
72 | 72 | | |||
73 | str = cleanString.normalized(QString::NormalizationForm_KC); | 73 | str = cleanString.normalized(QString::NormalizationForm_KC); | ||
74 | if (!str.isEmpty()) { | 74 | if (!str.isEmpty()) { | ||
75 | list << str; | 75 | list << str.toUtf8(); | ||
76 | } | 76 | } | ||
77 | } | 77 | } | ||
78 | } | 78 | } | ||
79 | 79 | | |||
80 | return list; | 80 | return list; | ||
81 | } | 81 | } | ||
82 | 82 | | |||
83 | void TermGenerator::indexText(const QString& text, const QByteArray& prefix) | 83 | void TermGenerator::indexText(const QString& text, const QByteArray& prefix) | ||
84 | { | 84 | { | ||
85 | QStringList terms = termList(text); | 85 | const QByteArrayList terms = termList(text); | ||
86 | for (const QString& term : terms) { | 86 | for (const QByteArray& term : terms) { | ||
87 | QByteArray arr = term.toUtf8(); | 87 | QByteArray finalArr = prefix + term; | ||
88 | | ||||
89 | QByteArray finalArr = prefix + arr; | | |||
90 | finalArr = finalArr.mid(0, maxTermSize); | 88 | finalArr = finalArr.mid(0, maxTermSize); | ||
91 | 89 | | |||
92 | m_doc.addPositionTerm(finalArr, m_position); | 90 | m_doc.addPositionTerm(finalArr, m_position); | ||
93 | m_position++; | 91 | m_position++; | ||
94 | } | 92 | } | ||
95 | } | 93 | } | ||
96 | 94 | | |||
97 | void TermGenerator::indexFileNameText(const QString& text, const QByteArray& prefix) | 95 | void TermGenerator::indexFileNameText(const QString& text, const QByteArray& prefix) | ||
98 | { | 96 | { | ||
99 | QStringList terms = termList(text); | 97 | const QByteArrayList terms = termList(text); | ||
100 | for (const QString& term : terms) { | 98 | for (const QByteArray& term : terms) { | ||
101 | QByteArray arr = term.toUtf8(); | 99 | QByteArray finalArr = prefix + term; | ||
102 | | ||||
103 | QByteArray finalArr = prefix + arr; | | |||
104 | finalArr = finalArr.mid(0, maxTermSize); | 100 | finalArr = finalArr.mid(0, maxTermSize); | ||
105 | 101 | | |||
106 | m_doc.addFileNamePositionTerm(finalArr, m_position); | 102 | m_doc.addFileNamePositionTerm(finalArr, m_position); | ||
107 | m_position++; | 103 | m_position++; | ||
108 | } | 104 | } | ||
109 | } | 105 | } | ||
110 | 106 | | |||
111 | void TermGenerator::indexFileNameText(const QString& text) | 107 | void TermGenerator::indexFileNameText(const QString& text) | ||
112 | { | 108 | { | ||
113 | indexFileNameText(text, QByteArray()); | 109 | indexFileNameText(text, QByteArray()); | ||
114 | } | 110 | } | ||
115 | 111 | | |||
116 | void TermGenerator::indexXattrText(const QString& text, const QByteArray& prefix) | 112 | void TermGenerator::indexXattrText(const QString& text, const QByteArray& prefix) | ||
117 | { | 113 | { | ||
118 | QStringList terms = termList(text); | 114 | const QByteArrayList terms = termList(text); | ||
119 | for (const QString& term : terms) { | 115 | for (const QByteArray& term : terms) { | ||
120 | QByteArray arr = term.toUtf8(); | 116 | QByteArray finalArr = prefix + term; | ||
121 | | ||||
122 | QByteArray finalArr = prefix + arr; | | |||
123 | finalArr = finalArr.mid(0, maxTermSize); | 117 | finalArr = finalArr.mid(0, maxTermSize); | ||
124 | 118 | | |||
125 | m_doc.addXattrPositionTerm(finalArr, m_position); | 119 | m_doc.addXattrPositionTerm(finalArr, m_position); | ||
126 | m_position++; | 120 | m_position++; | ||
127 | } | 121 | } | ||
128 | | ||||
129 | } | 122 | } | ||
130 | 123 | | |||
131 | int TermGenerator::position() const | 124 | int TermGenerator::position() const | ||
132 | { | 125 | { | ||
133 | return m_position; | 126 | return m_position; | ||
134 | } | 127 | } | ||
135 | 128 | | |||
136 | void TermGenerator::setPosition(int position) | 129 | void TermGenerator::setPosition(int position) | ||
137 | { | 130 | { | ||
138 | m_position = position; | 131 | m_position = position; | ||
139 | } | 132 | } | ||
140 | 133 | |