From 59debcd0e94a71498d76061966ec12d5d54c83de Mon Sep 17 00:00:00 2001 From: hjk Date: Mon, 16 Apr 2012 11:33:36 +0200 Subject: [PATCH] preprocessor: avoid branches in tight code Change-Id: I8dc0910f1cf11e506eb415a6f313b47ad2d41eae Reviewed-by: Thomas Hartmann --- src/libs/cplusplus/PPToken.cpp | 9 +-------- src/libs/cplusplus/PPToken.h | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/libs/cplusplus/PPToken.cpp b/src/libs/cplusplus/PPToken.cpp index 2e10bc34e4d..670b80bad84 100644 --- a/src/libs/cplusplus/PPToken.cpp +++ b/src/libs/cplusplus/PPToken.cpp @@ -4,11 +4,7 @@ using namespace CPlusPlus::Internal; -ByteArrayRef::ByteArrayRef() - : m_ref(0) - , m_offset(0) - , m_length(0) -{} +const QByteArray ByteArrayRef::m_emptyByteArray; bool ByteArrayRef::startsWith(const char *s) const { @@ -20,9 +16,6 @@ bool ByteArrayRef::startsWith(const char *s) const int ByteArrayRef::count(char ch) const { - if (!m_ref) - return 0; - int num = 0; const char *b = start(); const char *i = b + m_length; diff --git a/src/libs/cplusplus/PPToken.h b/src/libs/cplusplus/PPToken.h index 237ed088d67..1d63ebe59b8 100644 --- a/src/libs/cplusplus/PPToken.h +++ b/src/libs/cplusplus/PPToken.h @@ -12,7 +12,11 @@ namespace Internal { class CPLUSPLUS_EXPORT ByteArrayRef { public: - ByteArrayRef(); + ByteArrayRef() + : m_ref(&m_emptyByteArray) + , m_offset(0) + , m_length(0) + {} ByteArrayRef(const QByteArray *ref) : m_ref(ref) @@ -32,7 +36,7 @@ public: } inline const char *start() const - { return m_ref ? m_ref->constData() + m_offset : 0; } + { return m_ref->constData() + m_offset; } inline int length() const { return m_length; } @@ -41,16 +45,16 @@ public: { return length(); } inline char at(int pos) const - { return m_ref && pos >= 0 && pos < m_length ? m_ref->at(m_offset + pos) : '\0'; } + { return pos >= 0 && pos < m_length ? m_ref->at(m_offset + pos) : '\0'; } inline char operator[](int pos) const { return at(pos); } QByteArray toByteArray() const - { return m_ref ? QByteArray(m_ref->constData() + m_offset, m_length) : QByteArray(); } + { return QByteArray(m_ref->constData() + m_offset, m_length); } bool operator==(const QByteArray &other) const - { return m_ref ? (m_length == other.length() && !qstrncmp(m_ref->constData() + m_offset, other.constData(), m_length)) : false; } + { return m_length == other.length() && !qstrncmp(m_ref->constData() + m_offset, other.constData(), m_length); } bool operator!=(const QByteArray &other) const { return !this->operator==(other); } @@ -59,9 +63,10 @@ public: int count(char c) const; private: - const QByteArray *m_ref; - int m_offset; - int m_length; + const static QByteArray m_emptyByteArray; + const QByteArray * const m_ref; + const int m_offset; + const int m_length; }; inline bool operator==(const QByteArray &other, const ByteArrayRef &ref)