Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* Frozen |
3 |
|
|
* Copyright 2016 QuarksLab |
4 |
|
|
* |
5 |
|
|
* Licensed to the Apache Software Foundation (ASF) under one |
6 |
|
|
* or more contributor license agreements. See the NOTICE file |
7 |
|
|
* distributed with this work for additional information |
8 |
|
|
* regarding copyright ownership. The ASF licenses this file |
9 |
|
|
* to you under the Apache License, Version 2.0 (the |
10 |
|
|
* "License"); you may not use this file except in compliance |
11 |
|
|
* with the License. You may obtain a copy of the License at |
12 |
|
|
* |
13 |
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
14 |
|
|
* |
15 |
|
|
* Unless required by applicable law or agreed to in writing, |
16 |
|
|
* software distributed under the License is distributed on an |
17 |
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
18 |
|
|
* KIND, either express or implied. See the License for the |
19 |
|
|
* specific language governing permissions and limitations |
20 |
|
|
* under the License. |
21 |
|
|
*/ |
22 |
|
|
|
23 |
|
|
#ifndef FROZEN_LETITGO_STRING_H |
24 |
|
|
#define FROZEN_LETITGO_STRING_H |
25 |
|
|
|
26 |
|
|
#include "frozen/bits/elsa.h" |
27 |
|
|
#include "frozen/bits/hash_string.h" |
28 |
|
|
#include "frozen/bits/version.h" |
29 |
|
|
#include "frozen/bits/defines.h" |
30 |
|
|
|
31 |
|
|
#include <cstddef> |
32 |
|
|
#include <functional> |
33 |
|
|
|
34 |
|
|
#ifdef FROZEN_LETITGO_HAS_STRING_VIEW |
35 |
|
|
#include <string_view> |
36 |
|
|
#endif |
37 |
|
|
|
38 |
|
|
namespace frozen { |
39 |
|
|
|
40 |
|
|
template <typename _CharT> |
41 |
|
|
class basic_string { |
42 |
|
|
using chr_t = _CharT; |
43 |
|
|
|
44 |
|
|
chr_t const *data_; |
45 |
|
|
std::size_t size_; |
46 |
|
|
|
47 |
|
|
public: |
48 |
|
|
template <std::size_t N> |
49 |
|
|
constexpr basic_string(chr_t const (&data)[N]) |
50 |
|
|
: data_(data), size_(N - 1) {} |
51 |
|
88071 |
constexpr basic_string(chr_t const *data, std::size_t size) |
52 |
|
88071 |
: data_(data), size_(size) {} |
53 |
|
|
|
54 |
|
|
#ifdef FROZEN_LETITGO_HAS_STRING_VIEW |
55 |
|
|
constexpr basic_string(std::basic_string_view<chr_t> data) |
56 |
|
|
: data_(data.data()), size_(data.size()) {} |
57 |
|
|
#endif |
58 |
|
|
|
59 |
|
|
constexpr basic_string(const basic_string &) noexcept = default; |
60 |
|
|
constexpr basic_string &operator=(const basic_string &) noexcept = default; |
61 |
|
|
|
62 |
|
97871 |
constexpr std::size_t size() const { return size_; } |
63 |
|
|
|
64 |
|
|
constexpr chr_t operator[](std::size_t i) const { return data_[i]; } |
65 |
|
|
|
66 |
|
42303 |
constexpr bool operator==(basic_string other) const { |
67 |
4/4
✓ Branch 0 taken 5228 times.
✓ Branch 1 taken 15748 times.
✓ Branch 2 taken 5312 times.
✓ Branch 3 taken 16015 times.
|
42303 |
if (size_ != other.size_) |
68 |
|
10540 |
return false; |
69 |
4/4
✓ Branch 0 taken 75621 times.
✓ Branch 1 taken 15397 times.
✓ Branch 2 taken 76551 times.
✓ Branch 3 taken 15658 times.
|
183227 |
for (std::size_t i = 0; i < size_; ++i) |
70 |
4/4
✓ Branch 0 taken 351 times.
✓ Branch 1 taken 75270 times.
✓ Branch 2 taken 357 times.
✓ Branch 3 taken 76194 times.
|
152172 |
if (data_[i] != other.data_[i]) |
71 |
|
708 |
return false; |
72 |
|
31055 |
return true; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
constexpr bool operator<(const basic_string &other) const { |
76 |
|
|
unsigned i = 0; |
77 |
|
|
while (i < size() && i < other.size()) { |
78 |
|
|
if ((*this)[i] < other[i]) { |
79 |
|
|
return true; |
80 |
|
|
} |
81 |
|
|
if ((*this)[i] > other[i]) { |
82 |
|
|
return false; |
83 |
|
|
} |
84 |
|
|
++i; |
85 |
|
|
} |
86 |
|
|
return size() < other.size(); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
friend constexpr bool operator>(const basic_string& lhs, const basic_string& rhs) { |
90 |
|
|
return rhs < lhs; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
195742 |
constexpr const chr_t *data() const { return data_; } |
94 |
|
97871 |
constexpr const chr_t *begin() const { return data(); } |
95 |
|
97871 |
constexpr const chr_t *end() const { return data() + size(); } |
96 |
|
|
}; |
97 |
|
|
|
98 |
|
|
template <typename _CharT> struct elsa<basic_string<_CharT>> { |
99 |
|
|
constexpr std::size_t operator()(basic_string<_CharT> value) const { |
100 |
|
|
return hash_string(value); |
101 |
|
|
} |
102 |
|
97871 |
constexpr std::size_t operator()(basic_string<_CharT> value, std::size_t seed) const { |
103 |
|
97871 |
return hash_string(value, seed); |
104 |
|
|
} |
105 |
|
|
}; |
106 |
|
|
|
107 |
|
|
using string = basic_string<char>; |
108 |
|
|
using wstring = basic_string<wchar_t>; |
109 |
|
|
using u16string = basic_string<char16_t>; |
110 |
|
|
using u32string = basic_string<char32_t>; |
111 |
|
|
|
112 |
|
|
#ifdef FROZEN_LETITGO_HAS_CHAR8T |
113 |
|
|
using u8string = basic_string<char8_t>; |
114 |
|
|
#endif |
115 |
|
|
|
116 |
|
|
namespace string_literals { |
117 |
|
|
|
118 |
|
|
constexpr string operator"" _s(const char *data, std::size_t size) { |
119 |
|
|
return {data, size}; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
constexpr wstring operator"" _s(const wchar_t *data, std::size_t size) { |
123 |
|
|
return {data, size}; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
constexpr u16string operator"" _s(const char16_t *data, std::size_t size) { |
127 |
|
|
return {data, size}; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
constexpr u32string operator"" _s(const char32_t *data, std::size_t size) { |
131 |
|
|
return {data, size}; |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
#ifdef FROZEN_LETITGO_HAS_CHAR8T |
135 |
|
|
constexpr u8string operator"" _s(const char8_t *data, std::size_t size) { |
136 |
|
|
return {data, size}; |
137 |
|
|
} |
138 |
|
|
#endif |
139 |
|
|
|
140 |
|
|
} // namespace string_literals |
141 |
|
|
|
142 |
|
|
} // namespace frozen |
143 |
|
|
|
144 |
|
|
namespace std { |
145 |
|
|
template <typename _CharT> struct hash<frozen::basic_string<_CharT>> { |
146 |
|
|
std::size_t operator()(frozen::basic_string<_CharT> s) const { |
147 |
|
|
return frozen::elsa<frozen::basic_string<_CharT>>{}(s); |
148 |
|
|
} |
149 |
|
|
}; |
150 |
|
|
} // namespace std |
151 |
|
|
|
152 |
|
|
#endif |
153 |
|
|
|