1a33f8fbdSAdrien Destugues/* 2820dca4dSJohn Scipione * Copyright 2011 Haiku, Inc. All rights reserved. 3a33f8fbdSAdrien Destugues * Distributed under the terms of the MIT License. 4a33f8fbdSAdrien Destugues * 5a33f8fbdSAdrien Destugues * Authors: 6a33f8fbdSAdrien Destugues * Axel Dörfler, axeld@pinc-software.de 7a33f8fbdSAdrien Destugues * Adrien Destugues <pulkomandy@pulkomandy.ath.cx> 8a33f8fbdSAdrien Destugues * John Scipione, jscipione@gmail.com 9a33f8fbdSAdrien Destugues * 10a33f8fbdSAdrien Destugues * Corresponds to: 11820dca4dSJohn Scipione * headers/os/locale/Collator.h rev 42274 12820dca4dSJohn Scipione * src/kits/locale/Collator.cpp rev 42274 13a33f8fbdSAdrien Destugues */ 14a33f8fbdSAdrien Destugues 15a33f8fbdSAdrien Destugues 16a33f8fbdSAdrien Destugues/*! 17a33f8fbdSAdrien Destugues \file Collator.h 18820dca4dSJohn Scipione \ingroup locale 19820dca4dSJohn Scipione \ingroup libbe 20a33f8fbdSAdrien Destugues \brief Provides the BCollator class. 21a33f8fbdSAdrien Destugues*/ 22a33f8fbdSAdrien Destugues 23a33f8fbdSAdrien Destugues 24e82e8f36SAdrien Destugues/*! 25e82e8f36SAdrien Destugues \class BCollator 26e82e8f36SAdrien Destugues \ingroup locale 27820dca4dSJohn Scipione \ingroup libbe 28f69cadd0SJohn Scipione \brief Class for handling locale-aware collation (sorting) of strings. 29e82e8f36SAdrien Destugues 30f69cadd0SJohn Scipione BCollator is designed to handle collation (sorting) of strings. Unlike 31f69cadd0SJohn Scipione string sorting using strcmp() or similar functions that compare raw bytes 32f69cadd0SJohn Scipione the collation is done using a set of rules that changes from one locale 33f69cadd0SJohn Scipione to another. For example, in Spanish, 'ch' is considered to be a letter 34a33f8fbdSAdrien Destugues and is sorted between 'c' and 'd'. This class is also able to perform 35f69cadd0SJohn Scipione natural number sorting so that 2 is sorted before 10 unlike byte-based 36f69cadd0SJohn Scipione sorting. 37e82e8f36SAdrien Destugues 38*a19a18f5SAdrien Destugues \warning This class is not multithread-safe. So if you want to use a 39*a19a18f5SAdrien Destugues BCollator from more than one thread you need to protect it with 40*a19a18f5SAdrien Destugues a lock. 41edc845a3SJohn Scipione 42edc845a3SJohn Scipione \since Haiku R1 43e82e8f36SAdrien Destugues*/ 44e82e8f36SAdrien Destugues 45f69cadd0SJohn Scipione 46e82e8f36SAdrien Destugues/*! 47e82e8f36SAdrien Destugues \fn BCollator::BCollator() 48f69cadd0SJohn Scipione \brief Construct a collator with the default locale and strength. 49a33f8fbdSAdrien Destugues 50f69cadd0SJohn Scipione \attention The default collator should be constructed by the BLocale 51f69cadd0SJohn Scipione instead since it is aware of the currently defined locale. 52f69cadd0SJohn Scipione 53f69cadd0SJohn Scipione This constructor uses \c B_COLLATE_PRIMARY strength. 54edc845a3SJohn Scipione 55edc845a3SJohn Scipione \since Haiku R1 56e82e8f36SAdrien Destugues*/ 57e82e8f36SAdrien Destugues 58f69cadd0SJohn Scipione 59e82e8f36SAdrien Destugues/*! 60a33f8fbdSAdrien Destugues \fn BCollator::BCollator(const char* locale, 61a33f8fbdSAdrien Destugues int8 strength = B_COLLATE_PRIMARY, bool ignorePunctuation = false) 62f69cadd0SJohn Scipione \brief Construct a collator for the given \a locale and \a strength. 63e82e8f36SAdrien Destugues 64a33f8fbdSAdrien Destugues This constructor loads the data for the given locale. You can also 65f69cadd0SJohn Scipione set the \a strength and choose if the collator should take 66f69cadd0SJohn Scipione punctuation into account or not. 67a33f8fbdSAdrien Destugues 68f69cadd0SJohn Scipione \param locale The \a locale to build the constructor for. 69f69cadd0SJohn Scipione \param strength The collator class provide four level of \a strength. 70a33f8fbdSAdrien Destugues \li \c B_COLLATE_PRIMARY doesn't differentiate e from é, 71a33f8fbdSAdrien Destugues \li \c B_COLLATE_SECONDARY takes letter accents into account, 72a33f8fbdSAdrien Destugues \li \c B_COLLATE_TERTIARY is case sensitive, 73a33f8fbdSAdrien Destugues \li \c B_COLLATE_QUATERNARY is very strict. Most of the time you 74f69cadd0SJohn Scipione shouldn't need to go this far. 75f69cadd0SJohn Scipione \param ignorePunctuation Ignore punctuation during sorting. 76edc845a3SJohn Scipione 77edc845a3SJohn Scipione \since Haiku R1 78e82e8f36SAdrien Destugues*/ 79e82e8f36SAdrien Destugues 80f69cadd0SJohn Scipione 81e82e8f36SAdrien Destugues/*! 82e82e8f36SAdrien Destugues \fn BCollator::BCollator(BMessage* archive) 83a33f8fbdSAdrien Destugues \brief Unarchive a collator from a message. 84a33f8fbdSAdrien Destugues 85f69cadd0SJohn Scipione \param archive The message to unarchive the BCollator object from. 86edc845a3SJohn Scipione 87edc845a3SJohn Scipione \since Haiku R1 88e82e8f36SAdrien Destugues*/ 89e82e8f36SAdrien Destugues 90f69cadd0SJohn Scipione 91e82e8f36SAdrien Destugues/*! 92e82e8f36SAdrien Destugues \fn BCollator::BCollator(const BCollator& other) 93e82e8f36SAdrien Destugues \brief Copy constructor. 94a33f8fbdSAdrien Destugues 95f69cadd0SJohn Scipione Copies a BCollator object from another BCollator object. 96a33f8fbdSAdrien Destugues 97a33f8fbdSAdrien Destugues \param other The BCollator to copy from. 98edc845a3SJohn Scipione 99edc845a3SJohn Scipione \since Haiku R1 100e82e8f36SAdrien Destugues*/ 101e82e8f36SAdrien Destugues 102f69cadd0SJohn Scipione 103e82e8f36SAdrien Destugues/*! 104a33f8fbdSAdrien Destugues \fn BCollator::~BCollator() 105f69cadd0SJohn Scipione \brief Destructor method. 106a33f8fbdSAdrien Destugues 107f69cadd0SJohn Scipione Deletes the BCollator object freeing the resources it consumes. 108edc845a3SJohn Scipione 109edc845a3SJohn Scipione \since Haiku R1 110e82e8f36SAdrien Destugues*/ 111e82e8f36SAdrien Destugues 112f69cadd0SJohn Scipione 113e82e8f36SAdrien Destugues/*! 114a33f8fbdSAdrien Destugues \fn Bcollator& BCollator::operator=(const BCollator& other) 115e82e8f36SAdrien Destugues \brief Assignment operator. 116a33f8fbdSAdrien Destugues 117f69cadd0SJohn Scipione \param other the BCollator object to assign from. 118edc845a3SJohn Scipione 119edc845a3SJohn Scipione \since Haiku R1 120e82e8f36SAdrien Destugues*/ 121e82e8f36SAdrien Destugues 122f69cadd0SJohn Scipione 123e82e8f36SAdrien Destugues/*! 124*a19a18f5SAdrien Destugues \fn void BCollator::SetStrength(int8 strength) 125f69cadd0SJohn Scipione \brief Set the \a strength of the collator. 126e82e8f36SAdrien Destugues 127f69cadd0SJohn Scipione \param strength The collator class provide four level of \a strength. 128a33f8fbdSAdrien Destugues \li \c B_COLLATE_PRIMARY doesn't differentiate e from é, 129a33f8fbdSAdrien Destugues \li \c B_COLLATE_SECONDARY takes letter accents into account, 130a33f8fbdSAdrien Destugues \li \c B_COLLATE_TERTIARY is case sensitive, 131a33f8fbdSAdrien Destugues \li \c B_COLLATE_QUATERNARY is very strict. Most of the time you 132f69cadd0SJohn Scipione shouldn't need to go this far. 133edc845a3SJohn Scipione 134edc845a3SJohn Scipione \since Haiku R1 135e82e8f36SAdrien Destugues*/ 136e82e8f36SAdrien Destugues 137f69cadd0SJohn Scipione 138e82e8f36SAdrien Destugues/*! 139e82e8f36SAdrien Destugues \fn void BCollator::SetIgnorePunctuation(bool ignore) 140f69cadd0SJohn Scipione \brief Enable or disable punctuation handling. 141e82e8f36SAdrien Destugues 142f69cadd0SJohn Scipione This function enables or disables the handling of punctuation. 143e82e8f36SAdrien Destugues 144edc845a3SJohn Scipione \param ignore Whether or not punctuation should be ignored. 145edc845a3SJohn Scipione 146edc845a3SJohn Scipione \since Haiku R1 147e82e8f36SAdrien Destugues*/ 148e82e8f36SAdrien Destugues 149edc845a3SJohn Scipione 150e82e8f36SAdrien Destugues/*! 151e82e8f36SAdrien Destugues \fn bool BCollator::IgnorePunctuation() const 152f69cadd0SJohn Scipione \brief Gets the behavior of the collator with regards to punctuation. 153e82e8f36SAdrien Destugues 154f69cadd0SJohn Scipione \returns \c true if the collator will take punctuation into account 155f69cadd0SJohn Scipione when sorting, \c false otherwise. 156edc845a3SJohn Scipione 157edc845a3SJohn Scipione \since Haiku R1 158e82e8f36SAdrien Destugues*/ 159e82e8f36SAdrien Destugues 160e82e8f36SAdrien Destugues 161f69cadd0SJohn Scipione/*! 162*a19a18f5SAdrien Destugues \fn void BCollator::SetNumericSorting(bool ignore) 163*a19a18f5SAdrien Destugues \brief Enable or disable numeric order sorting. 164*a19a18f5SAdrien Destugues 165*a19a18f5SAdrien Destugues Numeric sorting enables the collator to identify strings of digits as 166*a19a18f5SAdrien Destugues numbers, and sort them in ascending number. For example, the string "123" 167*a19a18f5SAdrien Destugues is sorted after "234". Numbers and other characters can be mixed in the 168*a19a18f5SAdrien Destugues same string. 169*a19a18f5SAdrien Destugues 170*a19a18f5SAdrien Destugues \since Haiku R1 171*a19a18f5SAdrien Destugues*/ 172*a19a18f5SAdrien Destugues 173*a19a18f5SAdrien Destugues 174*a19a18f5SAdrien Destugues/*! 175*a19a18f5SAdrien Destugues \fn status_t BCollator::GetSortKey(const char* string, BString* key) 176*a19a18f5SAdrien Destugues const 177f69cadd0SJohn Scipione \brief Compute the sortkey of a \a string. 178f69cadd0SJohn Scipione 179f69cadd0SJohn Scipione The sortkey is a modified version of the input \a string that you can use 180f69cadd0SJohn Scipione to perform faster comparisons with other sortkeys using strcmp() or a 181f69cadd0SJohn Scipione similar comparison function. If you need to compare one string with other 182f69cadd0SJohn Scipione many times, storing the sortkey will allow you to perform the comparisons 183f69cadd0SJohn Scipione faster. 184e82e8f36SAdrien Destugues 185e82e8f36SAdrien Destugues \param string String from which to compute the sortkey. 186e82e8f36SAdrien Destugues \param key The resulting sortkey. 187e82e8f36SAdrien Destugues 188f69cadd0SJohn Scipione \retval B_OK if everything went well. 189f69cadd0SJohn Scipione \retval B_ERROR if an error occurred generating the sortkey. 190edc845a3SJohn Scipione 191edc845a3SJohn Scipione \since Haiku R1 192e82e8f36SAdrien Destugues*/ 193e82e8f36SAdrien Destugues 194f69cadd0SJohn Scipione 195e82e8f36SAdrien Destugues/*! 196*a19a18f5SAdrien Destugues \fn int BCollator::Compare(const char* s1, const char* s2) 197*a19a18f5SAdrien Destugues const 198*a19a18f5SAdrien Destugues \brief Returns the difference betweens the two strings. 199e82e8f36SAdrien Destugues 200f69cadd0SJohn Scipione This method should be used in place of the strcmp() function to perform 201f69cadd0SJohn Scipione locale-aware comparisons. 202e82e8f36SAdrien Destugues 203a33f8fbdSAdrien Destugues \param s1 The first string to compare. 204a33f8fbdSAdrien Destugues \param s2 The second string to compare. 205a33f8fbdSAdrien Destugues 206edc845a3SJohn Scipione \returns An integer value representing how the strings compare to each 207edc845a3SJohn Scipione other. 208a33f8fbdSAdrien Destugues \retval 0 if the strings are equal. 209a33f8fbdSAdrien Destugues \retval <0 if s1 is less than s2. 210a33f8fbdSAdrien Destugues \retval >0 if s1 is greater than s2. 211edc845a3SJohn Scipione 212edc845a3SJohn Scipione \since Haiku R1 213edc845a3SJohn Scipione 214edc845a3SJohn Scipione \since Haiku R1 215e82e8f36SAdrien Destugues*/ 216e82e8f36SAdrien Destugues 217f69cadd0SJohn Scipione 218e82e8f36SAdrien Destugues/*! 219*a19a18f5SAdrien Destugues \fn bool BCollator::Equal(const char* s1, const char* s2) 220*a19a18f5SAdrien Destugues const 221f69cadd0SJohn Scipione \brief Compares two strings for equality. 222e82e8f36SAdrien Destugues 223f69cadd0SJohn Scipione Note that strings that are not byte-by-byte identical may end up being 224f69cadd0SJohn Scipione treated as equal by this method. For example two strings may be 225f69cadd0SJohn Scipione considered equal if the only differences between them are in case and 226f69cadd0SJohn Scipione punctuation, depending on the \a strength used. Using 227f69cadd0SJohn Scipione \c B_QUANTERNARY_STRENGTH will force this method return \c true only 228f69cadd0SJohn Scipione if the strings are byte-for-byte identical. 229e82e8f36SAdrien Destugues 230a33f8fbdSAdrien Destugues \param s1 The first string to compare. 231a33f8fbdSAdrien Destugues \param s2 The second string to compare. 232a33f8fbdSAdrien Destugues 233f69cadd0SJohn Scipione \returns \c true if the strings are identical, \c false otherwise. 234edc845a3SJohn Scipione 235edc845a3SJohn Scipione \since Haiku R1 236e82e8f36SAdrien Destugues*/ 237e82e8f36SAdrien Destugues 238f69cadd0SJohn Scipione 239e82e8f36SAdrien Destugues/*! 240*a19a18f5SAdrien Destugues \fn bool BCollator::Greater(const char* s1, const char* s2) 241*a19a18f5SAdrien Destugues const 242a33f8fbdSAdrien Destugues \brief Determine if a string is greater than another. 243e82e8f36SAdrien Destugues 2445caae4d4SAdrien Destugues \note !Greater(s1, s2) is the same as GreaterOrEqual(s2, s1). This means 2455caae4d4SAdrien Destugues there is no need for Lesser(s1, s2) and LesserOrEqual(s1, s2) methods. 246a33f8fbdSAdrien Destugues 247a33f8fbdSAdrien Destugues \param s1 The first string to compare. 248a33f8fbdSAdrien Destugues \param s2 The second string to compare. 249a33f8fbdSAdrien Destugues 250a33f8fbdSAdrien Destugues \returns \c true if s1 is greater than, but not equal to, s2. 251edc845a3SJohn Scipione 252edc845a3SJohn Scipione \since Haiku R1 253e82e8f36SAdrien Destugues*/ 254e82e8f36SAdrien Destugues 255f69cadd0SJohn Scipione 256e82e8f36SAdrien Destugues/*! 257*a19a18f5SAdrien Destugues \fn bool BCollator::GreaterOrEqual(const char* s1, const char* s2) 258*a19a18f5SAdrien Destugues const 259f69cadd0SJohn Scipione \brief Determines if one string is greater than another. 260f69cadd0SJohn Scipione 2615caae4d4SAdrien Destugues \note !GreaterOrEqual(s1, s2) is the same as Greater(s2, s1). 262e82e8f36SAdrien Destugues 263a33f8fbdSAdrien Destugues \param s1 The first string to compare. 264a33f8fbdSAdrien Destugues \param s2 The second string to compare. 265a33f8fbdSAdrien Destugues 266a33f8fbdSAdrien Destugues \returns \c true if s1 is greater or equal than s2. 267edc845a3SJohn Scipione 268edc845a3SJohn Scipione \since Haiku R1 269e82e8f36SAdrien Destugues*/ 270cae874d3SAdrien Destugues 271f69cadd0SJohn Scipione 272cae874d3SAdrien Destugues/*! 273a33f8fbdSAdrien Destugues \fn static BArchivable* BCollator::Instantiate(BMessage* archive) 274cae874d3SAdrien Destugues \brief Unarchive the collator 275cae874d3SAdrien Destugues 276f69cadd0SJohn Scipione This method allows you to restore a collator that you previously 277*a19a18f5SAdrien Destugues archived. 278a33f8fbdSAdrien Destugues 279a33f8fbdSAdrien Destugues \param archive The message to restore the collator from. 280a33f8fbdSAdrien Destugues 281f69cadd0SJohn Scipione \returns A pointer to a BArchivable object containing the BCollator or 282f69cadd0SJohn Scipione \c NULL if an error occurred restoring the \a archive. 283edc845a3SJohn Scipione 284edc845a3SJohn Scipione \since Haiku R1 285cae874d3SAdrien Destugues*/ 286