2011-08-09 21:46:13 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011, Haiku, Inc. All Rights Reserved.
|
|
|
|
* 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:
|
|
|
|
* /trunk/headers/os/locale/Collator.h rev 42274
|
|
|
|
* /trunk/src/kits/locale/Collator.cpp rev 42274
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-08-10 12:39:48 +00:00
|
|
|
/*!
|
2011-08-09 21:46:13 +00:00
|
|
|
\file Collator.h
|
|
|
|
\brief Provides the BCollator class.
|
|
|
|
*/
|
2010-08-10 12:39:48 +00:00
|
|
|
|
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
/*!
|
|
|
|
\class BCollator
|
|
|
|
\ingroup locale
|
|
|
|
\brief Class for handling collation of string
|
|
|
|
|
|
|
|
BCatalog is designed to handle collations (sorting) of strings.
|
|
|
|
The collation is done using a set of rules that changes from a country
|
|
|
|
to another. For example, in spanish, 'ch' is consiidered as a letter
|
|
|
|
and is sorted between 'c' and 'd'. This class is alsoable to perform
|
|
|
|
natural sorting, so that '2' is sorted before '10', which is not the
|
|
|
|
case when you do a simple ASCII sort.
|
2010-08-10 12:39:48 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
\warning This class is not multithread-safe, as Compare() and GetKey()
|
|
|
|
change the ICUCollator (the strength). So if you want to use a
|
|
|
|
BCollator from more than one thread, you need to protect it with a lock.
|
2010-08-10 12:39:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2011-08-09 21:46:13 +00:00
|
|
|
\fn BCollator::BCollator()
|
|
|
|
\brief Construct a collator for the default locale.
|
|
|
|
|
|
|
|
Empty contructor.
|
2010-08-10 12:39:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2011-08-09 21:46:13 +00:00
|
|
|
\fn BCollator::BCollator(const char* locale,
|
|
|
|
int8 strength = B_COLLATE_PRIMARY, bool ignorePunctuation = false)
|
|
|
|
\brief Construct a collator for the given locale.
|
2010-08-10 12:39:48 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
This constructor loads the data for the given locale. You can also
|
|
|
|
adjust the strength and tell if the collator should take punctuation
|
|
|
|
into account when sorting.
|
|
|
|
|
|
|
|
\param locale The \a locale.
|
|
|
|
\param strength The collator class provide four level of strength. These
|
|
|
|
define the handling of various things.
|
|
|
|
\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 that far.
|
|
|
|
\param ignorePunctuation Ignore punctuation in the Collator when sorting.
|
2010-08-10 12:39:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2011-08-09 21:46:13 +00:00
|
|
|
\fn BCollator::BCollator(BMessage* archive)
|
|
|
|
\brief Unarchive a collator from a message.
|
|
|
|
|
|
|
|
\param archive The message to unarchive the BCollator from.
|
2010-08-10 12:39:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2011-08-09 21:46:13 +00:00
|
|
|
\fn BCollator::BCollator(const BCollator& other)
|
|
|
|
\brief Copy constructor.
|
|
|
|
|
|
|
|
Constructs a BCollator by making a copy of another BCollator.
|
|
|
|
|
|
|
|
\param other The BCollator to copy from.
|
2010-08-10 12:39:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2011-08-09 21:46:13 +00:00
|
|
|
\fn BCollator::~BCollator()
|
|
|
|
\brief Destructor.
|
|
|
|
|
|
|
|
Standard destructor method.
|
2010-08-10 12:39:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2011-08-09 21:46:13 +00:00
|
|
|
\fn Bcollator& BCollator::operator=(const BCollator& other)
|
|
|
|
\brief Assignment operator.
|
|
|
|
|
|
|
|
\param other the BCollator to assign from.
|
2010-08-10 12:39:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2011-08-09 21:46:13 +00:00
|
|
|
\fn void BCollator::SetDefaultStrength(int8 strength)
|
|
|
|
\brief Set the strength of the collator.
|
2010-08-10 12:39:48 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
Note that the \a strength can also be given on a case-by-case basis
|
|
|
|
when calling other methods.
|
2010-08-10 12:39:48 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
\param strength The collator class provide four level of strength.
|
|
|
|
These define the handling of various things.
|
|
|
|
\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 that far.
|
2010-08-10 12:39:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2011-08-09 21:46:13 +00:00
|
|
|
\fn int8 BCollator::DefaultStrength() const
|
|
|
|
\brief Get the current strength of this catalog.
|
|
|
|
|
|
|
|
\returns the current strength of this catalog.
|
2010-08-10 12:39:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2011-08-09 21:46:13 +00:00
|
|
|
\fn void BCollator::SetIgnorePunctuation(bool ignore)
|
|
|
|
\brief Enable or disable punctuation handling
|
2010-08-10 12:39:48 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
This function enables or disables the handling of punctuations.
|
2010-08-10 12:39:48 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
\param ignore Boolean telling if the punctuation should be ignored.
|
2010-08-10 12:39:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2011-08-09 21:46:13 +00:00
|
|
|
\fn bool BCollator::IgnorePunctuation() const
|
|
|
|
\brief Gets the behavior of the collator regarding punctuation.
|
2010-08-10 12:39:48 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
This function returns \c true if the collator will take punctuation into
|
|
|
|
account when sorting.
|
2010-08-10 12:39:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2011-08-09 21:46:13 +00:00
|
|
|
\fn satus_t BCollator::GetSortKey(const char* string, BString* key,
|
|
|
|
int8 strength) const
|
|
|
|
\brief Compute the sortkey of a string.
|
2010-08-10 12:39:48 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
A sortkey is a modified version of the string that you can use for faster
|
|
|
|
comparison with other sortkeys, using strcmp or a similar ASCII comparison.
|
|
|
|
If you need to compare a string with other ones a lot of times, storing
|
|
|
|
the sortkey will allow you to do the comparisons faster.
|
2010-08-10 12:39:48 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
\param string String from which to compute the sortkey.
|
|
|
|
\param key The resulting sortkey.
|
|
|
|
\param strength The \a strength to use for computing the sortkey.
|
2010-08-10 12:39:48 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
\returns B_OK if everything went well.
|
2010-08-10 12:39:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2011-08-09 21:46:13 +00:00
|
|
|
\fn int BCollator::Compare(const char* s1, const char* s2,
|
|
|
|
int8 strength) const
|
|
|
|
\brief Compare two strings.
|
2010-08-10 12:39:48 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
Returns the difference betweens the two strings similar to strcmp().
|
2010-08-10 12:39:48 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
\param s1 The first string to compare.
|
|
|
|
\param s2 The second string to compare.
|
|
|
|
\param strength The \a strength to use for comparing the strings.
|
|
|
|
|
|
|
|
\retval 0 if the strings are equal.
|
|
|
|
\retval <0 if s1 is less than s2.
|
|
|
|
\retval >0 if s1 is greater than s2.
|
2010-08-10 12:39:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2011-08-09 21:46:13 +00:00
|
|
|
\fn bool BCollator::Equal(const char* s1, const char* s2,
|
|
|
|
int8 strength) const
|
|
|
|
\brief Checks two strings for equality.
|
|
|
|
|
|
|
|
Compares two strings for equality. Note that different strings may end
|
|
|
|
up being equal, for example if the differences are only in case and
|
|
|
|
punctuation, depending on the strength used. Quaterary strength will
|
|
|
|
make this function return true only if the strings are byte-for-byte
|
|
|
|
identical.
|
2010-08-10 12:39:48 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
\param s1 The first string to compare.
|
|
|
|
\param s2 The second string to compare.
|
|
|
|
\param strength The \a strength to use for comparing the strings.
|
2010-08-10 12:39:48 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
\returns \c true if the strings are identical, otherwise \c false.
|
2010-08-10 12:39:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2011-08-09 21:46:13 +00:00
|
|
|
\fn bool BCollator::Greater(cosnt char* s1, const char* s2,
|
|
|
|
int8 strength) const
|
|
|
|
\brief Determine if a string is greater than another.
|
2010-08-10 12:39:48 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
\note !Greater(s1, s2) does the same thing as Greater(s2, s1)
|
2010-08-10 12:39:48 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
\param s1 The first string to compare.
|
|
|
|
\param s2 The second string to compare.
|
|
|
|
\param strength The \a strength to use for comparing the strings.
|
|
|
|
|
|
|
|
\returns \c true if s1 is greater than, but not equal to, s2.
|
2010-08-10 12:39:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2011-08-09 21:46:13 +00:00
|
|
|
\fn bool BCollator::GreaterOrEqual(cosnt char* s1, const char* s2,
|
|
|
|
int8 strength) const
|
|
|
|
\brief Tell if a string is greater than another.
|
|
|
|
|
|
|
|
\param s1 The first string to compare.
|
|
|
|
\param s2 The second string to compare.
|
|
|
|
\param strength The \a strength to use for comparing the strings.
|
2010-08-10 12:39:48 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
\returns \c true if s1 is greater or equal than s2.
|
2010-08-10 12:39:48 +00:00
|
|
|
*/
|
2010-08-11 08:09:12 +00:00
|
|
|
|
|
|
|
/*!
|
2011-08-09 21:46:13 +00:00
|
|
|
\fn static BArchivable* BCollator::Instantiate(BMessage* archive)
|
|
|
|
\brief Unarchive the collator
|
|
|
|
|
|
|
|
This function allows you to restore a collator that you previously
|
|
|
|
archived. It is faster to do that than to buid a collator and set
|
|
|
|
it up by hand every time you need it with the same settings.
|
2010-08-11 08:09:12 +00:00
|
|
|
|
2011-08-09 21:46:13 +00:00
|
|
|
\param archive The message to restore the collator from.
|
|
|
|
|
|
|
|
\returns A BArchivable object containing the BCollator or \c NULL.
|
2010-08-11 08:09:12 +00:00
|
|
|
*/
|
2011-08-09 21:46:13 +00:00
|
|
|
|