summaryrefslogtreecommitdiffstats
path: root/src/shared
diff options
context:
space:
mode:
authorJan Arne Petersen <jan.petersen@kdab.com>2016-04-18 21:44:57 +0200
committerJan Arne Petersen <jan.petersen@kdab.com>2016-04-19 08:44:50 +0000
commitc5ab939aa4f2683c7be61e9b788d50526b811ab8 (patch)
tree168241f2366ce9c74510a91e984a3c219c2f8b47 /src/shared
parentbc9eac9800811417697bebf35c5f11b0e852d123 (diff)
Use function to convert indices for text protocol
Use a proper function instead of left/midRef(index).toUtf8().size(). Makes it more clear what is happening and makes it easier when wayland text protocol switches from byte-based to unicode character-based indices. Also rename parameters of existing indexFromWayland() function to be the same as in the new function. Change-Id: Ie90abdcb264b74a024e96e041d5daf651920e9e4 Reviewed-by: Giulio Camuffo <giulio.camuffo@kdab.com>
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/qwaylandinputmethodeventbuilder.cpp21
-rw-r--r--src/shared/qwaylandinputmethodeventbuilder.h3
2 files changed, 15 insertions, 9 deletions
diff --git a/src/shared/qwaylandinputmethodeventbuilder.cpp b/src/shared/qwaylandinputmethodeventbuilder.cpp
index eb527c15a..fe93f5146 100644
--- a/src/shared/qwaylandinputmethodeventbuilder.cpp
+++ b/src/shared/qwaylandinputmethodeventbuilder.cpp
@@ -268,19 +268,24 @@ QWaylandInputMethodContentType QWaylandInputMethodContentType::convert(Qt::Input
return QWaylandInputMethodContentType{hint, purpose};
}
-int QWaylandInputMethodEventBuilder::indexFromWayland(const QString &str, int utf8Index, int baseIndex)
+int QWaylandInputMethodEventBuilder::indexFromWayland(const QString &text, int length, int base)
{
- if (utf8Index == 0)
- return baseIndex;
+ if (length == 0)
+ return base;
- if (utf8Index < 0) {
- const QByteArray &utf8 = str.leftRef(baseIndex).toUtf8();
- return QString::fromUtf8(utf8.left(qMax(utf8.length() + utf8Index, 0))).length();
+ if (length < 0) {
+ const QByteArray &utf8 = text.leftRef(base).toUtf8();
+ return QString::fromUtf8(utf8.left(qMax(utf8.length() + length, 0))).length();
} else {
- const QByteArray &utf8 = str.midRef(baseIndex).toUtf8();
- return QString::fromUtf8(utf8.left(utf8Index)).length() + baseIndex;
+ const QByteArray &utf8 = text.midRef(base).toUtf8();
+ return QString::fromUtf8(utf8.left(length)).length() + base;
}
}
+int QWaylandInputMethodEventBuilder::indexToWayland(const QString &text, int length, int base)
+{
+ return text.midRef(base, length).toUtf8().size();
+}
+
QT_END_NAMESPACE
diff --git a/src/shared/qwaylandinputmethodeventbuilder.h b/src/shared/qwaylandinputmethodeventbuilder.h
index 188a6a94b..3912afc04 100644
--- a/src/shared/qwaylandinputmethodeventbuilder.h
+++ b/src/shared/qwaylandinputmethodeventbuilder.h
@@ -61,7 +61,8 @@ public:
QInputMethodEvent buildCommit(const QString &text);
QInputMethodEvent buildPreedit(const QString &text);
- static int indexFromWayland(const QString &str, int utf8Index, int baseIndex = 0);
+ static int indexFromWayland(const QString &text, int length, int base = 0);
+ static int indexToWayland(const QString &text, int length, int base = 0);
private:
QPair<int, int> replacementForDeleteSurrounding();