Lolly 1.4.28
Loading...
Searching...
No Matches
string_view.hpp
Go to the documentation of this file.
1
2/** \file string_view.hpp
3 * \copyright GPLv3
4 * \details Strings with different type of char, and corresponding readonly
5 * view of string. Zero-characters are allowed in strings.
6 * \author jingkaimori
7 * \date 2024
8 */
9
10#pragma once
11
12#include "classdef.hpp"
13#include "fast_alloc.hpp"
14#include "minmax.hpp"
15#include <string>
16
17namespace lolly {
18namespace data {
19
20/**
21 * Readonly view of string, in which char is store in a continuous space. NUL is
22 * allowed in string. this view can be construct from lolly_string et al.
23 */
24template <typename T> class string_view {
25public:
26 const int N;
27 const T* const a;
28 /**
29 * construct from arbitary const pointer with length. NUL can occurs in given
30 * array
31 */
32 string_view (const T* a_, int N_) : N (N_), a (a_){};
33 /**
34 * construct from string literal
35 */
36 template <size_t N_>
37 constexpr string_view (const T (&null_end_str)[N_])
38 : N (N_ - 1), a (null_end_str){};
39
41
42 constexpr string_view (const std::basic_string<T>& str)
43 : N (str.size ()), a (str.data ()){};
44 operator std::basic_string<T> () { return std::basic_string<T> (a, N); };
45
46 string_view<T> operator() (int start, int end) const;
47
48 /**
49 * empty view is not allowed.
50 */
51 string_view () = delete;
52 string_view (const string_view<T>&) = delete;
55};
56
57template <class T>
60 if (end <= start) {
61 return string_view<T> (nullptr, 0);
62 }
63 start= max (min (N, start), 0);
64 end = max (min (N, end), 0);
65 return string_view<T> (a + start, end - start);
66}
67
68template <typename T>
69bool
71 if (a.N != b.N) return false;
72 const T *Sa= a.a, *Sb= b.a;
73 int n= a.N;
74 for (int i= 0; i < n; i++)
75 if (Sa[i] != Sb[i]) return false;
76 return true;
77};
78
79template <typename T, size_t Na>
80bool
81operator== (const T (&a)[Na], const string_view<T>& b) {
82 return string_view<T> (a) == b;
83}
84
85template <typename T, size_t Nb>
86bool
87operator== (const string_view<T>& a, const T (&b)[Nb]) {
88 return a == string_view<T> (b);
89};
90
91template <typename T>
92bool
94 if (a.N != b.N) return true;
95 const T *Sa= a.a, *Sb= b.a;
96 int n= a.N;
97 for (int i= 0; i < n; i++)
98 if (Sa[i] != Sb[i]) return true;
99 return false;
100};
101
102template <typename T, size_t Nb>
103bool
104operator!= (const string_view<T>& a, const T (&b)[Nb]) {
105 return a != string_view<T> (b);
106};
107
108template <typename T, size_t Na>
109bool
110operator!= (const T (&a)[Na], const string_view<T>& b) {
111 return string_view<T> (a) != b;
112};
113
114template <typename T>
115bool
116operator< (const string_view<T>& a, const string_view<T>& b) {
117 int i, na= a.N, nb= b.N, nmin= min (na, nb);
118 const T *Sa= a.a, *Sb= b.a;
119 for (i= 0; i < nmin; i++) {
120 if (Sa[i] < Sb[i]) return true;
121 if (Sb[i] < Sa[i]) return false;
122 }
123 return na < nb;
124};
125
126template <typename T, size_t Nb>
127bool
128operator< (const string_view<T>& a, const T (&b)[Nb]) {
129 return a < string_view<T> (b);
130};
131
132template <typename T, size_t Na>
133bool
134operator< (const T (&a)[Na], const string_view<T>& b) {
135 return string_view<T> (a);
136};
137
138template <typename T>
139bool
140operator<= (const string_view<T>& a, const string_view<T>& b) {
141 int i, na= a.N, nb= b.N, nmin= min (na, nb);
142 const T *Sa= a.a, *Sb= b.a;
143 for (i= 0; i < nmin; i++) {
144 if (Sa[i] < Sb[i]) return true;
145 if (Sb[i] < Sa[i]) return false;
146 }
147 return na <= nb;
148};
149
150template <typename T, size_t Nb>
151bool
152operator<= (const string_view<T>& a, const T (&b)[Nb]) {
153 return a <= string_view<T> (b);
154};
155
156template <typename T, size_t Na>
157bool
158operator<= (const T (&a)[Na], const string_view<T>& b) {
159 return string_view<T> (a) <= b;
160};
161
162} // namespace data
163} // namespace lolly
blackbox b[13]
The list class represents a linked list.
Definition list.hpp:48
string_view(string_view< T > &&)=default
string_view(const T *a_, int N_)
string_view(const string_view< T > &)=delete
constexpr string_view(const std::basic_string< T > &str)
string_view< T > operator()(int start, int end) const
constexpr string_view(const T(&null_end_str)[N_])
string_view & operator=(const string_view< T > &)=delete
SI min(SI i, SI j)
Returns the minimum of two signed integers.
Definition minmax.hpp:27
SI max(SI i, SI j)
Returns the maximum of two signed integers.
Definition minmax.hpp:40
bool operator!=(lolly_tree< T > t, int lab)
bool operator<(string_u16 a, string_u16 b)
int N(lolly_tree< T > t)
bool operator==(lolly_tree< T > t, int lab)
bool operator<=(string_u16 a, string_u16 b)