-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollator.cpp
More file actions
55 lines (52 loc) · 1.93 KB
/
collator.cpp
File metadata and controls
55 lines (52 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright © 2014 CCP ehf.
#include "stdafx.h"
#include "collator.h"
// -------------------------------------------------------------
// Description:
// Constructs a collator instance for the given locale.
// Arguments:
// languageCode - string representation of the locale as used in Python
// SeeAlso:
// LanguageID
// -------------------------------------------------------------
Collator::Collator( const std::string& languageCode, IRoot* lockObj /* = 0 */ )
{
SetLocale( languageCode );
}
#if !__APPLE__
// -------------------------------------------------------------
// Description:
// The comparison function compares the character data stored in two different strings.
// Returns information about whether a string is less than, greater than or equal to another string.
// Arguments:
// source - the string to be compared with.
// target - the string that is to be compared with the source string.
// Return Value:
// -1 if source is lesser than target, 0 if equal and 1 if source is greater than target.
// -------------------------------------------------------------
int Collator::Compare( const std::wstring& source, const std::wstring& target ) const
{
auto& col = std::use_facet<std::collate<wchar_t>>( m_locale );
return col.compare(
source.c_str(), source.c_str() + source.length(),
target.c_str(), target.c_str() + target.length() );
}
// -------------------------------------------------------------
// Description:
// Sets the locale for this collator.
// Arguments:
// languageCode - string representation of the locale as used in Python
// -------------------------------------------------------------
void Collator::SetLocale( const std::string& languageCode )
{
m_localeID = CodeToLanguageID( languageCode.c_str() );
try
{
m_locale = std::locale( LanguageIDToLocaleName( m_localeID ) );
}
catch ( std::runtime_error )
{
m_locale = std::locale();
}
}
#endif