QChar: reduce code duplication by inlining some methods

using templates for toLower()/toUpper()/toTitleCase()/toCaseFolded()
makes the upcoming patches smaller and thus easier to review

Change-Id: Ideb23e8669dbc2fe9ea3f129bf0137e1805ece11
Reviewed-by: Konstantin Ritt <ritt.ks@gmail.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
Konstantin Ritt 2012-04-09 17:56:44 +03:00 committed by Qt by Nokia
parent fd87a4c7f6
commit 4ca3d4ebab
2 changed files with 80 additions and 96 deletions

View File

@ -707,12 +707,10 @@ bool QChar::isSymbol() const
*/ */
/*! /*!
\fn int QChar::digitValue() const
Returns the numeric value of the digit, or -1 if the character is not a digit. Returns the numeric value of the digit, or -1 if the character is not a digit.
*/ */
int QChar::digitValue() const
{
return qGetProp(ucs)->digitValue;
}
/*! /*!
\overload \overload
@ -737,12 +735,10 @@ int QChar::digitValue(uint ucs4)
} }
/*! /*!
\fn QChar::Category QChar::category() const
Returns the character's category. Returns the character's category.
*/ */
QChar::Category QChar::category() const
{
return (QChar::Category) qGetProp(ucs)->category;
}
/*! /*!
\overload \overload
@ -766,12 +762,10 @@ QChar::Category QChar::category(ushort ucs2)
/*! /*!
\fn QChar::Direction QChar::direction() const
Returns the character's direction. Returns the character's direction.
*/ */
QChar::Direction QChar::direction() const
{
return (QChar::Direction) qGetProp(ucs)->direction;
}
/*! /*!
\overload \overload
@ -794,13 +788,11 @@ QChar::Direction QChar::direction(ushort ucs2)
} }
/*! /*!
\fn QChar::Joining QChar::joining() const
Returns information about the joining properties of the character Returns information about the joining properties of the character
(needed for certain languages such as Arabic). (needed for certain languages such as Arabic).
*/ */
QChar::Joining QChar::joining() const
{
return (QChar::Joining) qGetProp(ucs)->joining;
}
/*! /*!
\overload \overload
@ -865,15 +857,13 @@ bool QChar::hasMirrored() const
*/ */
/*! /*!
\fn QChar QChar::mirroredChar() const
Returns the mirrored character if this character is a mirrored Returns the mirrored character if this character is a mirrored
character; otherwise returns the character itself. character; otherwise returns the character itself.
\sa hasMirrored() \sa hasMirrored()
*/ */
QChar QChar::mirroredChar() const
{
return ucs + qGetProp(ucs)->mirrorDiff;
}
/*! /*!
\overload \overload
@ -990,6 +980,8 @@ QChar::Decomposition QChar::decompositionTag(uint ucs4)
} }
/*! /*!
\fn unsigned char QChar::combiningClass() const
Returns the combining class for the character as defined in the Returns the combining class for the character as defined in the
Unicode standard. This is mainly useful as a positioning hint for Unicode standard. This is mainly useful as a positioning hint for
marks attached to a base character. marks attached to a base character.
@ -997,10 +989,6 @@ QChar::Decomposition QChar::decompositionTag(uint ucs4)
The Qt text rendering engine uses this information to correctly The Qt text rendering engine uses this information to correctly
position non-spacing marks around a base character. position non-spacing marks around a base character.
*/ */
unsigned char QChar::combiningClass() const
{
return (unsigned char) qGetProp(ucs)->combiningClass;
}
/*! /*!
\overload \overload
@ -1025,12 +1013,10 @@ unsigned char QChar::combiningClass(ushort ucs2)
} }
/*! /*!
\fn QChar::UnicodeVersion QChar::unicodeVersion() const
Returns the Unicode version that introduced this character. Returns the Unicode version that introduced this character.
*/ */
QChar::UnicodeVersion QChar::unicodeVersion() const
{
return (QChar::UnicodeVersion) qGetProp(ucs)->unicodeVersion;
}
/*! /*!
\overload \overload
@ -1062,17 +1048,46 @@ QChar::UnicodeVersion QChar::currentUnicodeVersion()
return UNICODE_DATA_VERSION; return UNICODE_DATA_VERSION;
} }
template <typename T>
static inline T toLowerCase_helper(T uc)
{
const QUnicodeTables::Properties *p = qGetProp(uc);
if (!p->lowerCaseSpecial)
return uc + p->lowerCaseDiff;
return uc;
}
template <typename T>
static inline T toUpperCase_helper(T uc)
{
const QUnicodeTables::Properties *p = qGetProp(uc);
if (!p->upperCaseSpecial)
return uc + p->upperCaseDiff;
return uc;
}
template <typename T>
static inline T toTitleCase_helper(T uc)
{
const QUnicodeTables::Properties *p = qGetProp(uc);
if (!p->titleCaseSpecial)
return uc + p->titleCaseDiff;
return uc;
}
template <typename T>
static inline T toCaseFolded_helper(T uc)
{
return uc + qGetProp(uc)->caseFoldDiff;
}
/*! /*!
\fn QChar QChar::toLower() const
Returns the lowercase equivalent if the character is uppercase or titlecase; Returns the lowercase equivalent if the character is uppercase or titlecase;
otherwise returns the character itself. otherwise returns the character itself.
*/ */
QChar QChar::toLower() const
{
const QUnicodeTables::Properties *p = qGetProp(ucs);
if (!p->lowerCaseSpecial)
return ucs + p->lowerCaseDiff;
return ucs;
}
/*! /*!
\overload \overload
@ -1084,10 +1099,7 @@ uint QChar::toLower(uint ucs4)
{ {
if (ucs4 > UNICODE_LAST_CODEPOINT) if (ucs4 > UNICODE_LAST_CODEPOINT)
return ucs4; return ucs4;
const QUnicodeTables::Properties *p = qGetProp(ucs4); return toLowerCase_helper<uint>(ucs4);
if (!p->lowerCaseSpecial)
return ucs4 + p->lowerCaseDiff;
return ucs4;
} }
/*! /*!
@ -1098,23 +1110,15 @@ uint QChar::toLower(uint ucs4)
*/ */
ushort QChar::toLower(ushort ucs2) ushort QChar::toLower(ushort ucs2)
{ {
const QUnicodeTables::Properties *p = qGetProp(ucs2); return toLowerCase_helper<ushort>(ucs2);
if (!p->lowerCaseSpecial)
return ucs2 + p->lowerCaseDiff;
return ucs2;
} }
/*! /*!
\fn QChar QChar::toUpper() const
Returns the uppercase equivalent if the character is lowercase or titlecase; Returns the uppercase equivalent if the character is lowercase or titlecase;
otherwise returns the character itself. otherwise returns the character itself.
*/ */
QChar QChar::toUpper() const
{
const QUnicodeTables::Properties *p = qGetProp(ucs);
if (!p->upperCaseSpecial)
return ucs + p->upperCaseDiff;
return ucs;
}
/*! /*!
\overload \overload
@ -1126,10 +1130,7 @@ uint QChar::toUpper(uint ucs4)
{ {
if (ucs4 > UNICODE_LAST_CODEPOINT) if (ucs4 > UNICODE_LAST_CODEPOINT)
return ucs4; return ucs4;
const QUnicodeTables::Properties *p = qGetProp(ucs4); return toUpperCase_helper<uint>(ucs4);
if (!p->upperCaseSpecial)
return ucs4 + p->upperCaseDiff;
return ucs4;
} }
/*! /*!
@ -1140,23 +1141,15 @@ uint QChar::toUpper(uint ucs4)
*/ */
ushort QChar::toUpper(ushort ucs2) ushort QChar::toUpper(ushort ucs2)
{ {
const QUnicodeTables::Properties *p = qGetProp(ucs2); return toUpperCase_helper<ushort>(ucs2);
if (!p->upperCaseSpecial)
return ucs2 + p->upperCaseDiff;
return ucs2;
} }
/*! /*!
\fn QChar QChar::toTitleCase() const
Returns the title case equivalent if the character is lowercase or uppercase; Returns the title case equivalent if the character is lowercase or uppercase;
otherwise returns the character itself. otherwise returns the character itself.
*/ */
QChar QChar::toTitleCase() const
{
const QUnicodeTables::Properties *p = qGetProp(ucs);
if (!p->titleCaseSpecial)
return ucs + p->titleCaseDiff;
return ucs;
}
/*! /*!
\overload \overload
@ -1168,10 +1161,7 @@ uint QChar::toTitleCase(uint ucs4)
{ {
if (ucs4 > UNICODE_LAST_CODEPOINT) if (ucs4 > UNICODE_LAST_CODEPOINT)
return ucs4; return ucs4;
const QUnicodeTables::Properties *p = qGetProp(ucs4); return toTitleCase_helper<uint>(ucs4);
if (!p->titleCaseSpecial)
return ucs4 + p->titleCaseDiff;
return ucs4;
} }
/*! /*!
@ -1182,19 +1172,15 @@ uint QChar::toTitleCase(uint ucs4)
*/ */
ushort QChar::toTitleCase(ushort ucs2) ushort QChar::toTitleCase(ushort ucs2)
{ {
const QUnicodeTables::Properties *p = qGetProp(ucs2); return toTitleCase_helper<ushort>(ucs2);
if (!p->titleCaseSpecial)
return ucs2 + p->titleCaseDiff;
return ucs2;
} }
static inline uint foldCase(const ushort *ch, const ushort *start) static inline uint foldCase(const ushort *ch, const ushort *start)
{ {
uint c = *ch; uint c = *ch;
if (QChar(c).isLowSurrogate() && ch > start && QChar(*(ch - 1)).isHighSurrogate()) if (QChar(c).isLowSurrogate() && ch > start && QChar(*(ch - 1)).isHighSurrogate())
c = QChar::surrogateToUcs4(*(ch - 1), c); c = QChar::surrogateToUcs4(*(ch - 1), c);
return *ch + qGetProp(c)->caseFoldDiff; return toCaseFolded_helper<uint>(c);
} }
static inline uint foldCase(uint ch, uint &last) static inline uint foldCase(uint ch, uint &last)
@ -1203,22 +1189,20 @@ static inline uint foldCase(uint ch, uint &last)
if (QChar(c).isLowSurrogate() && QChar(last).isHighSurrogate()) if (QChar(c).isLowSurrogate() && QChar(last).isHighSurrogate())
c = QChar::surrogateToUcs4(last, c); c = QChar::surrogateToUcs4(last, c);
last = ch; last = ch;
return ch + qGetProp(c)->caseFoldDiff; return toCaseFolded_helper<uint>(c);
} }
static inline ushort foldCase(ushort ch) static inline ushort foldCase(ushort ch)
{ {
return ch + qGetProp(ch)->caseFoldDiff; return toCaseFolded_helper<ushort>(ch);
} }
/*! /*!
Returns the case folded equivalent of the character. For most Unicode characters this \fn QChar QChar::toCaseFolded() const
is the same as toLowerCase().
Returns the case folded equivalent of the character.
For most Unicode characters this is the same as toLowerCase().
*/ */
QChar QChar::toCaseFolded() const
{
return ucs + qGetProp(ucs)->caseFoldDiff;
}
/*! /*!
\overload \overload
@ -1229,7 +1213,7 @@ uint QChar::toCaseFolded(uint ucs4)
{ {
if (ucs4 > UNICODE_LAST_CODEPOINT) if (ucs4 > UNICODE_LAST_CODEPOINT)
return ucs4; return ucs4;
return ucs4 + qGetProp(ucs4)->caseFoldDiff; return toCaseFolded_helper<uint>(ucs4);
} }
/*! /*!
@ -1239,7 +1223,7 @@ uint QChar::toCaseFolded(uint ucs4)
*/ */
ushort QChar::toCaseFolded(ushort ucs2) ushort QChar::toCaseFolded(ushort ucs2)
{ {
return ucs2 + qGetProp(ucs2)->caseFoldDiff; return toCaseFolded_helper<ushort>(ucs2);
} }
/*! /*!

View File

@ -203,23 +203,23 @@ public:
}; };
// ****** WHEN ADDING FUNCTIONS, CONSIDER ADDING TO QCharRef TOO // ****** WHEN ADDING FUNCTIONS, CONSIDER ADDING TO QCharRef TOO
Category category() const; inline Category category() const { return QChar::category(ucs); }
Direction direction() const; inline Direction direction() const { return QChar::direction(ucs); }
Joining joining() const; inline Joining joining() const { return QChar::joining(ucs); }
bool hasMirrored() const; bool hasMirrored() const;
unsigned char combiningClass() const; inline unsigned char combiningClass() const { return QChar::combiningClass(ucs); }
QChar mirroredChar() const; inline QChar mirroredChar() const { return QChar::mirroredChar(ucs); }
QString decomposition() const; QString decomposition() const;
Decomposition decompositionTag() const; Decomposition decompositionTag() const;
int digitValue() const; inline int digitValue() const { return QChar::digitValue(ucs); }
QChar toLower() const; inline QChar toLower() const { return QChar::toLower(ucs); }
QChar toUpper() const; inline QChar toUpper() const { return QChar::toUpper(ucs); }
QChar toTitleCase() const; inline QChar toTitleCase() const { return QChar::toTitleCase(ucs); }
QChar toCaseFolded() const; inline QChar toCaseFolded() const { return QChar::toCaseFolded(ucs); }
UnicodeVersion unicodeVersion() const; inline UnicodeVersion unicodeVersion() const { return QChar::unicodeVersion(ucs); }
inline char toAscii() const; inline char toAscii() const;
inline char toLatin1() const; inline char toLatin1() const;