2011-08-10 01:46:13 +04:00
|
|
|
/*
|
2013-02-07 06:05:00 +04:00
|
|
|
* Copyright 2011 Haiku, Inc. All rights reserved.
|
2011-08-10 01:46:13 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Axel Dörfler, axeld@pinc-software.de
|
|
|
|
* Adrien Destugues <pulkomandy@pulkomandy.ath.cx>
|
|
|
|
* John Scipione, jscipione@gmail.com
|
|
|
|
*
|
|
|
|
* Corresponds to:
|
2013-02-07 06:05:00 +04:00
|
|
|
* headers/os/locale/Collator.h rev 42274
|
|
|
|
* src/kits/locale/Collator.cpp rev 42274
|
2011-08-10 01:46:13 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-08-10 16:39:48 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\file Collator.h
|
2013-02-07 06:05:00 +04:00
|
|
|
\ingroup locale
|
|
|
|
\ingroup libbe
|
2011-08-10 01:46:13 +04:00
|
|
|
\brief Provides the BCollator class.
|
|
|
|
*/
|
2010-08-10 16:39:48 +04:00
|
|
|
|
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
/*!
|
|
|
|
\class BCollator
|
|
|
|
\ingroup locale
|
2013-02-07 06:05:00 +04:00
|
|
|
\ingroup libbe
|
2011-11-11 07:55:09 +04:00
|
|
|
\brief Class for handling locale-aware collation (sorting) of strings.
|
2011-08-10 01:46:13 +04:00
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
BCollator is designed to handle collation (sorting) of strings. Unlike
|
|
|
|
string sorting using strcmp() or similar functions that compare raw bytes
|
|
|
|
the collation is done using a set of rules that changes from one locale
|
|
|
|
to another. For example, in Spanish, 'ch' is considered to be a letter
|
|
|
|
and is sorted between 'c' and 'd'. This class is also able to perform
|
|
|
|
natural number sorting so that 2 is sorted before 10 unlike byte-based
|
|
|
|
sorting.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2017-05-28 23:31:52 +03:00
|
|
|
\warning This class is not multithread-safe. So if you want to use a
|
|
|
|
BCollator from more than one thread you need to protect it with
|
|
|
|
a lock.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-10 16:39:48 +04:00
|
|
|
*/
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
|
2010-08-10 16:39:48 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\fn BCollator::BCollator()
|
2011-11-11 07:55:09 +04:00
|
|
|
\brief Construct a collator with the default locale and strength.
|
|
|
|
|
|
|
|
\attention The default collator should be constructed by the BLocale
|
2014-06-12 00:48:17 +04:00
|
|
|
instead since it is aware of the currently defined locale.
|
2011-08-10 01:46:13 +04:00
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
This constructor uses \c B_COLLATE_PRIMARY strength.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-10 16:39:48 +04:00
|
|
|
*/
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
|
2010-08-10 16:39:48 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\fn BCollator::BCollator(const char* locale,
|
|
|
|
int8 strength = B_COLLATE_PRIMARY, bool ignorePunctuation = false)
|
2011-11-11 07:55:09 +04:00
|
|
|
\brief Construct a collator for the given \a locale and \a strength.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
This constructor loads the data for the given locale. You can also
|
2011-11-11 07:55:09 +04:00
|
|
|
set the \a strength and choose if the collator should take
|
|
|
|
punctuation into account or not.
|
2011-08-10 01:46:13 +04:00
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
\param locale The \a locale to build the constructor for.
|
|
|
|
\param strength The collator class provide four level of \a strength.
|
2014-06-12 00:48:17 +04:00
|
|
|
\li \c B_COLLATE_PRIMARY doesn't differentiate e from é,
|
|
|
|
\li \c B_COLLATE_SECONDARY takes letter accents into account,
|
|
|
|
\li \c B_COLLATE_TERTIARY is case sensitive,
|
|
|
|
\li \c B_COLLATE_QUATERNARY is very strict. Most of the time you
|
|
|
|
shouldn't need to go this far.
|
2011-11-11 07:55:09 +04:00
|
|
|
\param ignorePunctuation Ignore punctuation during sorting.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-10 16:39:48 +04:00
|
|
|
*/
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
|
2010-08-10 16:39:48 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\fn BCollator::BCollator(BMessage* archive)
|
|
|
|
\brief Unarchive a collator from a message.
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
\param archive The message to unarchive the BCollator object from.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-10 16:39:48 +04:00
|
|
|
*/
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
|
2010-08-10 16:39:48 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\fn BCollator::BCollator(const BCollator& other)
|
|
|
|
\brief Copy constructor.
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
Copies a BCollator object from another BCollator object.
|
2011-08-10 01:46:13 +04:00
|
|
|
|
|
|
|
\param other The BCollator to copy from.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-10 16:39:48 +04:00
|
|
|
*/
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
|
2010-08-10 16:39:48 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\fn BCollator::~BCollator()
|
2011-11-11 07:55:09 +04:00
|
|
|
\brief Destructor method.
|
2011-08-10 01:46:13 +04:00
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
Deletes the BCollator object freeing the resources it consumes.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-10 16:39:48 +04:00
|
|
|
*/
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
|
2010-08-10 16:39:48 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\fn Bcollator& BCollator::operator=(const BCollator& other)
|
|
|
|
\brief Assignment operator.
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
\param other the BCollator object to assign from.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-10 16:39:48 +04:00
|
|
|
*/
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
|
2010-08-10 16:39:48 +04:00
|
|
|
/*!
|
2017-05-28 23:31:52 +03:00
|
|
|
\fn void BCollator::SetStrength(int8 strength)
|
2011-11-11 07:55:09 +04:00
|
|
|
\brief Set the \a strength of the collator.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
\param strength The collator class provide four level of \a strength.
|
2014-06-12 00:48:17 +04:00
|
|
|
\li \c B_COLLATE_PRIMARY doesn't differentiate e from é,
|
|
|
|
\li \c B_COLLATE_SECONDARY takes letter accents into account,
|
|
|
|
\li \c B_COLLATE_TERTIARY is case sensitive,
|
|
|
|
\li \c B_COLLATE_QUATERNARY is very strict. Most of the time you
|
|
|
|
shouldn't need to go this far.
|
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-10 16:39:48 +04:00
|
|
|
*/
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
|
2010-08-10 16:39:48 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\fn void BCollator::SetIgnorePunctuation(bool ignore)
|
2011-11-11 07:55:09 +04:00
|
|
|
\brief Enable or disable punctuation handling.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
This function enables or disables the handling of punctuation.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2014-06-12 00:48:17 +04:00
|
|
|
\param ignore Whether or not punctuation should be ignored.
|
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-10 16:39:48 +04:00
|
|
|
*/
|
|
|
|
|
2014-06-12 00:48:17 +04:00
|
|
|
|
2010-08-10 16:39:48 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\fn bool BCollator::IgnorePunctuation() const
|
2011-11-11 07:55:09 +04:00
|
|
|
\brief Gets the behavior of the collator with regards to punctuation.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
\returns \c true if the collator will take punctuation into account
|
2014-06-12 00:48:17 +04:00
|
|
|
when sorting, \c false otherwise.
|
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-10 16:39:48 +04:00
|
|
|
*/
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
|
2010-08-10 16:39:48 +04:00
|
|
|
/*!
|
2017-05-28 23:31:52 +03:00
|
|
|
\fn void BCollator::SetNumericSorting(bool ignore)
|
|
|
|
\brief Enable or disable numeric order sorting.
|
|
|
|
|
|
|
|
Numeric sorting enables the collator to identify strings of digits as
|
|
|
|
numbers, and sort them in ascending number. For example, the string "123"
|
|
|
|
is sorted after "234". Numbers and other characters can be mixed in the
|
|
|
|
same string.
|
|
|
|
|
|
|
|
\since Haiku R1
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn status_t BCollator::GetSortKey(const char* string, BString* key)
|
|
|
|
const
|
2011-11-11 07:55:09 +04:00
|
|
|
\brief Compute the sortkey of a \a string.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
The sortkey is a modified version of the input \a string that you can use
|
|
|
|
to perform faster comparisons with other sortkeys using strcmp() or a
|
|
|
|
similar comparison function. If you need to compare one string with other
|
|
|
|
many times, storing the sortkey will allow you to perform the comparisons
|
|
|
|
faster.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
\param string String from which to compute the sortkey.
|
|
|
|
\param key The resulting sortkey.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
\retval B_OK if everything went well.
|
|
|
|
\retval B_ERROR if an error occurred generating the sortkey.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-10 16:39:48 +04:00
|
|
|
*/
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
|
2010-08-10 16:39:48 +04:00
|
|
|
/*!
|
2017-05-28 23:31:52 +03:00
|
|
|
\fn int BCollator::Compare(const char* s1, const char* s2)
|
|
|
|
const
|
|
|
|
\brief Returns the difference betweens the two strings.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
This method should be used in place of the strcmp() function to perform
|
|
|
|
locale-aware comparisons.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
\param s1 The first string to compare.
|
|
|
|
\param s2 The second string to compare.
|
|
|
|
|
2014-06-12 00:48:17 +04:00
|
|
|
\returns An integer value representing how the strings compare to each
|
|
|
|
other.
|
2011-08-10 01:46:13 +04:00
|
|
|
\retval 0 if the strings are equal.
|
|
|
|
\retval <0 if s1 is less than s2.
|
|
|
|
\retval >0 if s1 is greater than s2.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-10 16:39:48 +04:00
|
|
|
*/
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
|
2010-08-10 16:39:48 +04:00
|
|
|
/*!
|
2017-05-28 23:31:52 +03:00
|
|
|
\fn bool BCollator::Equal(const char* s1, const char* s2)
|
|
|
|
const
|
2011-11-11 07:55:09 +04:00
|
|
|
\brief Compares two strings for equality.
|
2011-08-10 01:46:13 +04:00
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
Note that strings that are not byte-by-byte identical may end up being
|
|
|
|
treated as equal by this method. For example two strings may be
|
|
|
|
considered equal if the only differences between them are in case and
|
|
|
|
punctuation, depending on the \a strength used. Using
|
|
|
|
\c B_QUANTERNARY_STRENGTH will force this method return \c true only
|
|
|
|
if the strings are byte-for-byte identical.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
\param s1 The first string to compare.
|
|
|
|
\param s2 The second string to compare.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
\returns \c true if the strings are identical, \c false otherwise.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-10 16:39:48 +04:00
|
|
|
*/
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
|
2010-08-10 16:39:48 +04:00
|
|
|
/*!
|
2017-05-28 23:31:52 +03:00
|
|
|
\fn bool BCollator::Greater(const char* s1, const char* s2)
|
|
|
|
const
|
2011-08-10 01:46:13 +04:00
|
|
|
\brief Determine if a string is greater than another.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2011-11-11 13:19:19 +04:00
|
|
|
\note !Greater(s1, s2) is the same as GreaterOrEqual(s2, s1). This means
|
|
|
|
there is no need for Lesser(s1, s2) and LesserOrEqual(s1, s2) methods.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
\param s1 The first string to compare.
|
|
|
|
\param s2 The second string to compare.
|
|
|
|
|
|
|
|
\returns \c true if s1 is greater than, but not equal to, s2.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-10 16:39:48 +04:00
|
|
|
*/
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
|
2010-08-10 16:39:48 +04:00
|
|
|
/*!
|
2017-05-28 23:31:52 +03:00
|
|
|
\fn bool BCollator::GreaterOrEqual(const char* s1, const char* s2)
|
|
|
|
const
|
2011-11-11 07:55:09 +04:00
|
|
|
\brief Determines if one string is greater than another.
|
|
|
|
|
2011-11-11 13:19:19 +04:00
|
|
|
\note !GreaterOrEqual(s1, s2) is the same as Greater(s2, s1).
|
2011-08-10 01:46:13 +04:00
|
|
|
|
|
|
|
\param s1 The first string to compare.
|
|
|
|
\param s2 The second string to compare.
|
2010-08-10 16:39:48 +04:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
\returns \c true if s1 is greater or equal than s2.
|
2014-06-12 00:48:17 +04:00
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-10 16:39:48 +04:00
|
|
|
*/
|
2010-08-11 12:09:12 +04:00
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
|
2010-08-11 12:09:12 +04:00
|
|
|
/*!
|
2011-08-10 01:46:13 +04:00
|
|
|
\fn static BArchivable* BCollator::Instantiate(BMessage* archive)
|
|
|
|
\brief Unarchive the collator
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
This method allows you to restore a collator that you previously
|
2017-05-28 23:31:52 +03:00
|
|
|
archived.
|
2010-08-11 12:09:12 +04:00
|
|
|
|
2011-08-10 01:46:13 +04:00
|
|
|
\param archive The message to restore the collator from.
|
|
|
|
|
2011-11-11 07:55:09 +04:00
|
|
|
\returns A pointer to a BArchivable object containing the BCollator or
|
2014-06-12 00:48:17 +04:00
|
|
|
\c NULL if an error occurred restoring the \a archive.
|
|
|
|
|
|
|
|
\since Haiku R1
|
2010-08-11 12:09:12 +04:00
|
|
|
*/
|