Translator::Translator() :
m_codec(QTextCodec::codecForName("ISO-8859-1")),
- m_locationsType(AbsoluteLocations)
+ m_locationsType(AbsoluteLocations),
+ m_indexOk(true)
{
}
return theFormats;
}
+void Translator::addIndex(int idx, const TranslatorMessage &msg) const
+{
+ if (msg.sourceText().isEmpty() && msg.id().isEmpty()) {
+ m_ctxCmtIdx[msg.context()] = idx;
+ } else {
+ m_msgIdx[TMMKey(msg)] = idx;
+ if (!msg.id().isEmpty())
+ m_idMsgIdx[msg.id()] = idx;
+ }
+}
+
+void Translator::delIndex(int idx) const
+{
+ const TranslatorMessage &msg = m_messages.at(idx);
+ if (msg.sourceText().isEmpty() && msg.id().isEmpty()) {
+ m_ctxCmtIdx.remove(msg.context());
+ } else {
+ m_msgIdx.remove(TMMKey(msg));
+ if (!msg.id().isEmpty())
+ m_idMsgIdx.remove(msg.id());
+ }
+}
+
+void Translator::ensureIndexed() const
+{
+ if (!m_indexOk) {
+ m_indexOk = true;
+ m_ctxCmtIdx.clear();
+ m_idMsgIdx.clear();
+ m_msgIdx.clear();
+ for (int i = 0; i < m_messages.count(); i++)
+ addIndex(i, m_messages.at(i));
+ }
+}
+
void Translator::replaceSorted(const TranslatorMessage &msg)
{
int index = find(msg);
- if (index == -1)
+ if (index == -1) {
appendSorted(msg);
- else
+ } else {
+ delIndex(index);
m_messages[index] = msg;
+ addIndex(index, msg);
+ }
}
void Translator::extend(const TranslatorMessage &msg)
{
int index = find(msg);
if (index == -1) {
- m_messages.append(msg);
+ append(msg);
} else {
TranslatorMessage &emsg = m_messages[index];
emsg.addReferenceUniq(msg.fileName(), msg.lineNumber());
}
}
+void Translator::insert(int idx, const TranslatorMessage &msg)
+{
+ addIndex(idx, msg);
+ m_messages.insert(idx, msg);
+}
+
void Translator::append(const TranslatorMessage &msg)
{
- m_messages.append(msg);
+ insert(m_messages.count(), msg);
}
void Translator::appendSorted(const TranslatorMessage &msg)
{
int msgLine = msg.lineNumber();
if (msgLine < 0) {
- m_messages.append(msg);
+ append(msg);
return;
}
thisScore = 1;
}
if (thisScore > bestScore || (thisScore == bestScore && thisSize > bestSize))
- m_messages.insert(thisIdx, msg);
+ insert(thisIdx, msg);
else if (bestScore)
- m_messages.insert(bestIdx, msg);
+ insert(bestIdx, msg);
else
- m_messages.append(msg);
+ append(msg);
}
static QString guessFormat(const QString &filename, const QString &format)
int Translator::find(const TranslatorMessage &msg) const
{
- for (int i = 0; i < m_messages.count(); ++i) {
- const TranslatorMessage &tmsg = m_messages.at(i);
- if (msg.id().isEmpty() || tmsg.id().isEmpty()) {
- if (msg.context() == tmsg.context()
- && msg.sourceText() == tmsg.sourceText()
- && msg.comment() == tmsg.comment())
- return i;
- } else {
- if (msg.id() == tmsg.id())
- return i;
- }
- }
- return -1;
+ ensureIndexed();
+ if (msg.id().isEmpty())
+ return m_msgIdx.value(TMMKey(msg), -1);
+ int i = m_idMsgIdx.value(msg.id(), -1);
+ if (i >= 0)
+ return i;
+ i = m_msgIdx.value(TMMKey(msg), -1);
+ // If both have an id, then find only by id.
+ return i >= 0 && m_messages.at(i).id().isEmpty() ? i : -1;
}
int Translator::find(const QString &context,
int Translator::find(const QString &context) const
{
- for (TMM::ConstIterator it = m_messages.constBegin(); it != m_messages.constEnd(); ++it)
- if (it->context() == context && it->sourceText().isEmpty() && it->id().isEmpty())
- return it - m_messages.constBegin();
- return -1;
+ ensureIndexed();
+ return m_ctxCmtIdx.value(context, -1);
}
void Translator::stripObsoleteMessages()
if (it->type() != TranslatorMessage::Obsolete)
newmm.append(*it);
m_messages = newmm;
+ m_indexOk = false;
}
void Translator::stripFinishedMessages()
if (it->type() != TranslatorMessage::Finished)
newmm.append(*it);
m_messages = newmm;
+ m_indexOk = false;
}
void Translator::stripEmptyContexts()
if (it->sourceText() != QLatin1String(ContextComment))
newmm.append(*it);
m_messages = newmm;
+ m_indexOk = false;
}
void Translator::stripNonPluralForms()
if (it->isPlural())
newmm.append(*it);
m_messages = newmm;
+ m_indexOk = false;
}
void Translator::stripIdenticalSourceTranslations()
else if (it->translation() != it->sourceText())
newmm.append(*it);
}
+ m_indexOk = false;
m_messages = newmm;
}
}
if (!omsg->isTranslated() && msg.isTranslated())
omsg->setTranslations(msg.translations());
+ m_indexOk = false;
m_messages.removeAt(i);
}
return dups;
TranslatorSaveMode m_saveMode;
};
+class TMMKey {
+public:
+ TMMKey(const TranslatorMessage &msg)
+ { context = msg.context(); source = msg.sourceText(); comment = msg.comment(); }
+ bool operator==(const TMMKey &o) const
+ { return context == o.context && source == o.source && comment == o.comment; }
+ QString context, source, comment;
+};
+Q_DECLARE_TYPEINFO(TMMKey, Q_MOVABLE_TYPE);
+inline uint qHash(const TMMKey &key) { return qHash(key.context) ^ qHash(key.source) ^ qHash(key.comment); }
+
class Translator
{
public:
};
private:
+ void insert(int idx, const TranslatorMessage &msg);
+ void addIndex(int idx, const TranslatorMessage &msg) const;
+ void delIndex(int idx) const;
+ void ensureIndexed() const;
+
typedef QList<TranslatorMessage> TMM; // int stores the sequence position.
TMM m_messages;
QString m_language;
QString m_sourceLanguage;
ExtraData m_extra;
+
+ mutable bool m_indexOk;
+ mutable QHash<QString, int> m_ctxCmtIdx;
+ mutable QHash<QString, int> m_idMsgIdx;
+ mutable QHash<TMMKey, int> m_msgIdx;
};
bool getNumerusInfo(QLocale::Language language, QLocale::Country country,