1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___LOCALE_DIR_COLLATE_H
10#define _LIBCPP___LOCALE_DIR_COLLATE_H
11
12#include <__config>
13
14#if _LIBCPP_HAS_LOCALIZATION
15
16# include <__cstddef/size_t.h>
17# include <__locale_dir/locale.h>
18# include <__locale_dir/locale_base_api.h>
19# include <string>
20
21# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22# pragma GCC system_header
23# endif
24
25_LIBCPP_BEGIN_NAMESPACE_STD
26_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
27
28// template <class _CharT> class collate;
29
30template <class _CharT>
31class collate : public locale::facet {
32public:
33 typedef _CharT char_type;
34 typedef basic_string<char_type> string_type;
35
36 _LIBCPP_HIDE_FROM_ABI explicit collate(size_t __refs = 0) : locale::facet(__refs) {}
37
38 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI int
39 compare(const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const {
40 return do_compare(__lo1, __hi1, __lo2, __hi2);
41 }
42
43 // FIXME(EricWF): The _LIBCPP_ALWAYS_INLINE is needed on Windows to work
44 // around a dllimport bug that expects an external instantiation.
45 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE string_type
46 transform(const char_type* __lo, const char_type* __hi) const {
47 return do_transform(__lo, __hi);
48 }
49
50 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI long hash(const char_type* __lo, const char_type* __hi) const {
51 return do_hash(__lo, __hi);
52 }
53
54 static locale::id id;
55
56protected:
57 ~collate() override;
58 virtual int
59 do_compare(const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const;
60 virtual string_type do_transform(const char_type* __lo, const char_type* __hi) const {
61 return string_type(__lo, __hi);
62 }
63 virtual long do_hash(const char_type* __lo, const char_type* __hi) const;
64};
65
66template <class _CharT>
67locale::id collate<_CharT>::id;
68
69template <class _CharT>
70collate<_CharT>::~collate() {}
71
72template <class _CharT>
73int collate<_CharT>::do_compare(
74 const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const {
75 for (; __lo2 != __hi2; ++__lo1, ++__lo2) {
76 if (__lo1 == __hi1 || *__lo1 < *__lo2)
77 return -1;
78 if (*__lo2 < *__lo1)
79 return 1;
80 }
81 return __lo1 != __hi1;
82}
83
84template <class _CharT>
85long collate<_CharT>::do_hash(const char_type* __lo, const char_type* __hi) const {
86 size_t __h = 0;
87 const size_t __sr = __CHAR_BIT__ * sizeof(size_t) - 8;
88 const size_t __mask = size_t(0xF) << (__sr + 4);
89 for (const char_type* __p = __lo; __p != __hi; ++__p) {
90 __h = (__h << 4) + static_cast<size_t>(*__p);
91 size_t __g = __h & __mask;
92 __h ^= __g | (__g >> __sr);
93 }
94 return static_cast<long>(__h);
95}
96
97extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<char>;
98# if _LIBCPP_HAS_WIDE_CHARACTERS
99extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS collate<wchar_t>;
100# endif
101
102// template <class CharT> class collate_byname;
103
104template <class _CharT>
105class collate_byname;
106
107template <>
108class _LIBCPP_EXPORTED_FROM_ABI collate_byname<char> : public collate<char> {
109 __locale::__locale_t __l_;
110
111public:
112 typedef char char_type;
113 typedef basic_string<char_type> string_type;
114
115 explicit collate_byname(const char* __n, size_t __refs = 0);
116 explicit collate_byname(const string& __n, size_t __refs = 0);
117
118protected:
119 ~collate_byname() override;
120 int do_compare(
121 const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const override;
122 string_type do_transform(const char_type* __lo, const char_type* __hi) const override;
123};
124
125# if _LIBCPP_HAS_WIDE_CHARACTERS
126template <>
127class _LIBCPP_EXPORTED_FROM_ABI collate_byname<wchar_t> : public collate<wchar_t> {
128 __locale::__locale_t __l_;
129
130public:
131 typedef wchar_t char_type;
132 typedef basic_string<char_type> string_type;
133
134 explicit collate_byname(const char* __n, size_t __refs = 0);
135 explicit collate_byname(const string& __n, size_t __refs = 0);
136
137protected:
138 ~collate_byname() override;
139
140 int do_compare(
141 const char_type* __lo1, const char_type* __hi1, const char_type* __lo2, const char_type* __hi2) const override;
142 string_type do_transform(const char_type* __lo, const char_type* __hi) const override;
143};
144# endif
145
146_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
147_LIBCPP_END_NAMESPACE_STD
148
149#endif // _LIBCPP_HAS_LOCALIZATION
150
151#endif // _LIBCPP___LOCALE_DIR_COLLATE_H
152