LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
string.h
Go to the documentation of this file.
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"
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
38namespace frozen {
39
40template <typename _CharT>
42 using chr_t = _CharT;
43
44 chr_t const *data_;
45 std::size_t size_;
46
47public:
48 template <std::size_t N>
49 constexpr basic_string(chr_t const (&data)[N])
50 : data_(data), size_(N - 1) {}
51 constexpr basic_string(chr_t const *data, std::size_t size)
52 : 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 constexpr std::size_t size() const { return size_; }
68
69 constexpr chr_t operator[](std::size_t i) const { return data_[i]; }
70
71 constexpr bool operator==(basic_string other) const {
72 if (size_ != other.size_)
73 return false;
74 for (std::size_t i = 0; i < size_; ++i)
75 if (data_[i] != other.data_[i])
76 return false;
77 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 constexpr const chr_t *data() const { return data_; }
105 constexpr const chr_t *begin() const { return data(); }
106 constexpr const chr_t *end() const { return data() + size(); }
107};
108
109template <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 constexpr std::size_t operator()(basic_string<_CharT> value, std::size_t seed) const {
114 return hash_string(value, seed);
115 }
116};
117
122
123#ifdef FROZEN_LETITGO_HAS_CHAR8T
124using u8string = basic_string<char8_t>;
125#endif
126
128
129constexpr string operator""_s(const char *data, std::size_t size) {
130 return {data, size};
131}
132
133constexpr wstring operator""_s(const wchar_t *data, std::size_t size) {
134 return {data, size};
135}
136
137constexpr u16string operator""_s(const char16_t *data, std::size_t size) {
138 return {data, size};
139}
140
141constexpr u32string operator""_s(const char32_t *data, std::size_t size) {
142 return {data, size};
143}
144
145#ifdef FROZEN_LETITGO_HAS_CHAR8T
146constexpr 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
155namespace std {
156template <typename _CharT> struct hash<frozen::basic_string<_CharT>> {
160};
161} // namespace std
162
163#endif
Definition string.h:41
friend constexpr bool operator<=(const basic_string &lhs, const basic_string &rhs)
Definition string.h:100
constexpr bool operator<(const basic_string &other) const
Definition string.h:80
friend constexpr bool operator>(const basic_string &lhs, const basic_string &rhs)
Definition string.h:94
constexpr const chr_t * begin() const
Definition string.h:105
constexpr std::size_t size() const
Definition string.h:67
constexpr const chr_t * end() const
Definition string.h:106
constexpr chr_t operator[](std::size_t i) const
Definition string.h:69
constexpr bool operator==(basic_string other) const
Definition string.h:71
constexpr const chr_t * data() const
Definition string.h:104
constexpr basic_string(chr_t const (&data)[N])
Definition string.h:49
constexpr std::size_t length() const
Definition string.h:66
constexpr basic_string(const basic_string &) noexcept=default
constexpr basic_string & operator=(const basic_string &) noexcept=default
friend constexpr bool operator>=(const basic_string &lhs, const basic_string &rhs)
Definition string.h:97
constexpr basic_string(chr_t const *data, std::size_t size)
Definition string.h:51
Definition string.h:127
Definition algorithm.h:30
basic_string< char > string
Definition string.h:118
basic_string< char16_t > u16string
Definition string.h:120
basic_string< char32_t > u32string
Definition string.h:121
constexpr std::size_t hash_string(const String &value)
Definition hash_string.h:9
basic_string< wchar_t > wstring
Definition string.h:119
Definition string.h:155
constexpr std::size_t operator()(basic_string< _CharT > value, std::size_t seed) const
Definition string.h:113
constexpr std::size_t operator()(basic_string< _CharT > value) const
Definition string.h:110
Definition elsa.h:30
std::size_t operator()(frozen::basic_string< _CharT > s) const
Definition string.h:157