ZRCola::translation::CompareString() >> ZRCola::CompareString()

This commit is contained in:
Simon Rozman 2017-03-14 13:03:03 +01:00
parent bd0fdba435
commit a224454b3c
3 changed files with 35 additions and 35 deletions

View File

@ -552,6 +552,34 @@ namespace ZRCola {
i->invert();
}
};
///
/// Binary compares two strings
///
/// \param[in] str_a First string
/// \param[in] str_a_end First string end
/// \param[in] str_b Second string
/// \param[in] str_b_end Second string end
///
/// \returns
/// - <0 when str_a < str_b
/// - =0 when str_a == str_b
/// - >0 when str_a > str_b
///
/// \note
/// The function does not treat \\0 characters as terminators for performance reasons.
/// Therefore \p str_a_end and \p str_b_end must represent exact string ends.
///
inline int CompareString(const wchar_t *str_a, const wchar_t *str_a_end, const wchar_t *str_b, const wchar_t *str_b_end)
{
for (; ; str_a++, str_b++) {
if (str_a >= str_a_end && str_b >= str_b_end) return 0;
else if (str_a >= str_a_end && str_b < str_b_end) return -1;
else if (str_a < str_a_end && str_b >= str_b_end) return +1;
else if (*str_a < *str_b) return -1;
else if (*str_a > *str_b) return +1;
}
}
};

View File

@ -234,7 +234,7 @@ namespace ZRCola {
///
static inline int CompareName(LCID locale, const wchar_t *str_a, unsigned __int16 count_a, const wchar_t *str_b, unsigned __int16 count_b)
{
switch (CompareString(locale, SORT_STRINGSORT | NORM_IGNORECASE, str_a, count_a, str_b, count_b)) {
switch (::CompareString(locale, SORT_STRINGSORT | NORM_IGNORECASE, str_a, count_a, str_b, count_b)) {
case CSTR_LESS_THAN : return -1;
case CSTR_EQUAL : return 0;
case CSTR_GREATER_THAN: return 1;

View File

@ -54,34 +54,6 @@ namespace ZRCola {
};
unsigned __int16 dec_end; ///< Decomposed string end in \c data
wchar_t data[]; ///< Decomposed string and composed character
///
/// Binary compares two strings
///
/// \param[in] str_a First string
/// \param[in] str_a_end First string end
/// \param[in] str_b Second string
/// \param[in] str_b_end Second string end
///
/// \returns
/// - <0 when str_a < str_b
/// - =0 when str_a == str_b
/// - >0 when str_a > str_b
///
/// \note
/// The function does not treat \\0 characters as terminators for performance reasons.
/// Therefore \p str_a_end and \p str_b_end must represent exact string ends.
///
static inline int CompareString(const wchar_t *str_a, const wchar_t *str_a_end, const wchar_t *str_b, const wchar_t *str_b_end)
{
for (; ; str_a++, str_b++) {
if (str_a >= str_a_end && str_b >= str_b_end) return 0;
else if (str_a >= str_a_end && str_b < str_b_end) return -1;
else if (str_a < str_a_end && str_b >= str_b_end) return +1;
else if (*str_a < *str_b) return -1;
else if (*str_a > *str_b) return +1;
}
}
};
#pragma pack(pop)
@ -111,7 +83,7 @@ namespace ZRCola {
///
virtual int compare(_In_ const translation &a, _In_ const translation &b) const
{
int r = translation::CompareString(a.data + a.dec_start, a.data + a.dec_end, b.data + b.dec_start, b.data + b.dec_end);
int r = ZRCola::CompareString(a.data + a.dec_start, a.data + a.dec_end, b.data + b.dec_start, b.data + b.dec_end);
if (r != 0) return r;
return 0;
@ -130,10 +102,10 @@ namespace ZRCola {
///
virtual int compare_sort(_In_ const translation &a, _In_ const translation &b) const
{
int r = translation::CompareString(a.data + a.dec_start, a.data + a.dec_end, b.data + b.dec_start, b.data + b.dec_end);
int r = ZRCola::CompareString(a.data + a.dec_start, a.data + a.dec_end, b.data + b.dec_start, b.data + b.dec_end);
if (r != 0) return r;
r = translation::CompareString(a.data + a.com_start, a.data + a.com_end, b.data + b.com_start, b.data + b.com_end);
r = ZRCola::CompareString(a.data + a.com_start, a.data + a.com_end, b.data + b.com_start, b.data + b.com_end);
if (r != 0) return r;
return 0;
@ -167,7 +139,7 @@ namespace ZRCola {
///
virtual int compare(_In_ const translation &a, _In_ const translation &b) const
{
int r = translation::CompareString(a.data + a.com_start, a.data + a.com_end, b.data + b.com_start, b.data + b.com_end);
int r = ZRCola::CompareString(a.data + a.com_start, a.data + a.com_end, b.data + b.com_start, b.data + b.com_end);
if (r != 0) return r;
return 0;
@ -186,13 +158,13 @@ namespace ZRCola {
///
virtual int compare_sort(_In_ const translation &a, _In_ const translation &b) const
{
int r = translation::CompareString(a.data + a.com_start, a.data + a.com_end, b.data + b.com_start, b.data + b.com_end);
int r = ZRCola::CompareString(a.data + a.com_start, a.data + a.com_end, b.data + b.com_start, b.data + b.com_end);
if (r != 0) return r;
if (a.rank < b.rank) return -1;
else if (a.rank > b.rank) return +1;
r = translation::CompareString(a.data + a.dec_start, a.data + a.dec_end, b.data + b.dec_start, b.data + b.dec_end);
r = ZRCola::CompareString(a.data + a.dec_start, a.data + a.dec_end, b.data + b.dec_start, b.data + b.dec_end);
if (r != 0) return r;
return 0;