| 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 |
|
88359 |
constexpr basic_string(chr_t const *data, std::size_t size) |
| 52 |
|
88359 |
: 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 |
|
|
|
| 58 |
|
|
explicit constexpr operator std::basic_string_view<chr_t>() const { |
| 59 |
|
|
return std::basic_string_view<chr_t>(data_, size_); |
| 60 |
|
|
} |
| 61 |
|
|
#endif |
| 62 |
|
|
|
| 63 |
|
|
constexpr basic_string(const basic_string &) noexcept = default; |
| 64 |
|
|
constexpr basic_string &operator=(const basic_string &) noexcept = default; |
| 65 |
|
|
|
| 66 |
|
|
constexpr std::size_t length() const { return size_; } |
| 67 |
|
98195 |
constexpr std::size_t size() const { return size_; } |
| 68 |
|
|
|
| 69 |
|
|
constexpr chr_t operator[](std::size_t i) const { return data_[i]; } |
| 70 |
|
|
|
| 71 |
|
42537 |
constexpr bool operator==(basic_string other) const { |
| 72 |
4/4
✓ Branch 0 taken 5306 times.
✓ Branch 1 taken 15811 times.
✓ Branch 2 taken 5369 times.
✓ Branch 3 taken 16051 times.
|
42537 |
if (size_ != other.size_) |
| 73 |
|
10675 |
return false; |
| 74 |
4/4
✓ Branch 0 taken 75912 times.
✓ Branch 1 taken 15460 times.
✓ Branch 2 taken 76770 times.
✓ Branch 3 taken 15697 times.
|
183839 |
for (std::size_t i = 0; i < size_; ++i) |
| 75 |
4/4
✓ Branch 0 taken 351 times.
✓ Branch 1 taken 75561 times.
✓ Branch 2 taken 354 times.
✓ Branch 3 taken 76416 times.
|
152682 |
if (data_[i] != other.data_[i]) |
| 76 |
|
705 |
return false; |
| 77 |
|
31157 |
return true; |
| 78 |
|
|
} |
| 79 |
|
|
|
| 80 |
|
|
constexpr bool operator<(const basic_string &other) const { |
| 81 |
|
|
unsigned i = 0; |
| 82 |
|
|
while (i < size() && i < other.size()) { |
| 83 |
|
|
if ((*this)[i] < other[i]) { |
| 84 |
|
|
return true; |
| 85 |
|
|
} |
| 86 |
|
|
if ((*this)[i] > other[i]) { |
| 87 |
|
|
return false; |
| 88 |
|
|
} |
| 89 |
|
|
++i; |
| 90 |
|
|
} |
| 91 |
|
|
return size() < other.size(); |
| 92 |
|
|
} |
| 93 |
|
|
|
| 94 |
|
|
friend constexpr bool operator>(const basic_string& lhs, const basic_string& rhs) { |
| 95 |
|
|
return rhs < lhs; |
| 96 |
|
|
} |
| 97 |
|
|
friend constexpr bool operator>=(const basic_string& lhs, const basic_string& rhs) { |
| 98 |
|
|
return !(lhs < rhs); |
| 99 |
|
|
} |
| 100 |
|
|
friend constexpr bool operator<=(const basic_string& lhs, const basic_string& rhs) { |
| 101 |
|
|
return !(lhs > rhs); |
| 102 |
|
|
} |
| 103 |
|
|
|
| 104 |
|
196390 |
constexpr const chr_t *data() const { return data_; } |
| 105 |
|
98195 |
constexpr const chr_t *begin() const { return data(); } |
| 106 |
|
98195 |
constexpr const chr_t *end() const { return data() + size(); } |
| 107 |
|
|
}; |
| 108 |
|
|
|
| 109 |
|
|
template <typename _CharT> struct elsa<basic_string<_CharT>> { |
| 110 |
|
|
constexpr std::size_t operator()(basic_string<_CharT> value) const { |
| 111 |
|
|
return hash_string(value); |
| 112 |
|
|
} |
| 113 |
|
98195 |
constexpr std::size_t operator()(basic_string<_CharT> value, std::size_t seed) const { |
| 114 |
|
98195 |
return hash_string(value, seed); |
| 115 |
|
|
} |
| 116 |
|
|
}; |
| 117 |
|
|
|
| 118 |
|
|
using string = basic_string<char>; |
| 119 |
|
|
using wstring = basic_string<wchar_t>; |
| 120 |
|
|
using u16string = basic_string<char16_t>; |
| 121 |
|
|
using u32string = basic_string<char32_t>; |
| 122 |
|
|
|
| 123 |
|
|
#ifdef FROZEN_LETITGO_HAS_CHAR8T |
| 124 |
|
|
using u8string = basic_string<char8_t>; |
| 125 |
|
|
#endif |
| 126 |
|
|
|
| 127 |
|
|
namespace string_literals { |
| 128 |
|
|
|
| 129 |
|
|
constexpr string operator""_s(const char *data, std::size_t size) { |
| 130 |
|
|
return {data, size}; |
| 131 |
|
|
} |
| 132 |
|
|
|
| 133 |
|
|
constexpr wstring operator""_s(const wchar_t *data, std::size_t size) { |
| 134 |
|
|
return {data, size}; |
| 135 |
|
|
} |
| 136 |
|
|
|
| 137 |
|
|
constexpr u16string operator""_s(const char16_t *data, std::size_t size) { |
| 138 |
|
|
return {data, size}; |
| 139 |
|
|
} |
| 140 |
|
|
|
| 141 |
|
|
constexpr u32string operator""_s(const char32_t *data, std::size_t size) { |
| 142 |
|
|
return {data, size}; |
| 143 |
|
|
} |
| 144 |
|
|
|
| 145 |
|
|
#ifdef FROZEN_LETITGO_HAS_CHAR8T |
| 146 |
|
|
constexpr u8string operator""_s(const char8_t *data, std::size_t size) { |
| 147 |
|
|
return {data, size}; |
| 148 |
|
|
} |
| 149 |
|
|
#endif |
| 150 |
|
|
|
| 151 |
|
|
} // namespace string_literals |
| 152 |
|
|
|
| 153 |
|
|
} // namespace frozen |
| 154 |
|
|
|
| 155 |
|
|
namespace std { |
| 156 |
|
|
template <typename _CharT> struct hash<frozen::basic_string<_CharT>> { |
| 157 |
|
|
std::size_t operator()(frozen::basic_string<_CharT> s) const { |
| 158 |
|
|
return frozen::elsa<frozen::basic_string<_CharT>>{}(s); |
| 159 |
|
|
} |
| 160 |
|
|
}; |
| 161 |
|
|
} // namespace std |
| 162 |
|
|
|
| 163 |
|
|
#endif |
| 164 |
|
|
|