Lolly 1.4.27
Loading...
Searching...
No Matches
string_u16.hpp
Go to the documentation of this file.
1
2/** \file string_u16.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 "string_view.hpp"
15
16namespace lolly {
17namespace data {
18
20
21class string_u16;
22
24 int n;
25 int a_N;
26 char16_t* a;
27
28public:
29 inline string_u16_rep () : n (0), a_N (0), a (NULL) {}
30 string_u16_rep (int n);
31 inline ~string_u16_rep () {
32 if (n != 0) tm_delete_array (a);
33 }
34 /**
35 * @brief expand (or shrink) string by delta, but do not release memory when
36 * string is shrinked.
37 *
38 * @return string length before expansion
39 */
40 int expand_or_shrink_by (int delta);
41
42 /**
43 * @brief expand (or shrink) string to given length n, and try to release
44 * memory when string is shrinked.
45 *
46 * @note expand_or_shrink_by may be faster if memory space is reserved
47 */
48 void resize (int n);
49
50 /**
51 * @brief reserve memory space to contain at least n word in the whole string.
52 * Do not affect length of string, and do not release memory when n is smaller
53 * than current space.
54 */
55 void reserve (int n);
56
57 friend class string_u16;
58 friend int N (string_u16 a);
59};
60
63
64 inline string_u16 () : rep (tm_new<string_u16_rep> ()) {}
65 inline explicit string_u16 (int n) : rep (tm_new<string_u16_rep> (n)) {}
66
67 template <size_t N_>
68 string_u16 (const char16_t (&s)[N_]) : rep (tm_new<string_u16_rep> (N_ - 1)) {
69 constexpr int n= N_ - 1;
70 for (int i= 0; i < n; i++)
71 rep->a[i]= s[i];
72 };
73
74 string_u16 (char16_t c);
75 string_u16 (char16_t c, int n);
77
78 inline char16_t* buffer () { return rep->a; }
79 inline char16_t* buffer (int size) {
80 rep->resize (size);
81 return rep->a;
82 }
83 char16_t* begin () { return rep->a; }
84 char16_t* end () { return rep->a + rep->n; }
85
86 inline operator string_u16_view () {
87 return string_u16_view (rep->a, rep->n);
88 }
90 return ((string_u16_view) * this) (start, end);
91 }
92
93 inline char16_t& operator[] (int i) { return rep->a[i]; }
94};
96
97inline int
99 return a->n;
100}
101
102inline int
104 int i, h= 0, n= N (s);
105 for (i= 0; i < n; i++) {
106 h= (h << 9) + (h >> 23);
107 h= h + ((int) s[i]);
108 }
109 return h;
110};
111
112inline bool
116
117inline bool
119 return ((string_u16_view) a) == b;
120}
121
122inline bool
124 return a == ((string_u16_view) b);
125}
126
127template <size_t Nb>
128bool
129operator== (string_u16 a, const char16_t (&b)[Nb]) {
130 return ((string_u16_view) a) == string_u16_view (b);
131}
132
133template <size_t Na>
134bool
135operator== (const char16_t (&a)[Na], string_u16 b) {
136 return string_u16_view (a) == ((string_u16_view) b);
137}
138
139inline bool
143
144inline bool
146 return ((string_u16_view) a) != b;
147}
148
149inline bool
151 return a != ((string_u16_view) b);
152}
153
154template <size_t Nb>
155bool
156operator!= (string_u16 a, const char16_t (&b)[Nb]) {
157 return ((string_u16_view) a) != string_u16_view (b);
158}
159
160template <size_t Na>
161bool
162operator!= (const char16_t (&a)[Na], string_u16 b) {
163 return string_u16_view (a) != ((string_u16_view) b);
164}
165
166inline bool
168 return ((string_u16_view) a) < ((string_u16_view) b);
169}
170
171inline bool
173 return ((string_u16_view) a) <= ((string_u16_view) b);
174}
175
176string_u16& operator<< (string_u16& a, char16_t c);
177string_u16& operator<< (string_u16& a, string_u16 b);
178
179string_u16 operator* (string_u16 a, const string_u16_view& b);
180
181string_u16 copy (const string_u16_view& a);
182string_u16 copy (string_u16 a);
183
184} // namespace data
185} // namespace lolly
int hash(pointer ptr)
Computes a hash value for a pointer.
Definition basic.cpp:17
blackbox b[13]
#define CONCRETE_CODE(PTR)
Macro used to define the implementation of a concrete smart pointer.
Definition classdef.hpp:159
The list class represents a linked list.
Definition list.hpp:48
void resize(int n)
expand (or shrink) string to given length n, and try to release memory when string is shrinked.
void reserve(int n)
reserve memory space to contain at least n word in the whole string. Do not affect length of string,...
int expand_or_shrink_by(int delta)
expand (or shrink) string by delta, but do not release memory when string is shrinked.
friend int N(string_u16 a)
string_u16_view operator()(int start, int end)
string_u16(const char16_t(&s)[N_])
char16_t * buffer(int size)
char16_t & operator[](int i)
C * tm_new()
void tm_delete_array(C *Ptr)
bool operator!=(lolly_tree< T > t, int lab)
bool operator<(string_u16 a, string_u16 b)
int N(lolly_tree< T > t)
lolly_tree< T > operator*(lolly_tree< T > t1, lolly_tree< T > t2)
lolly_tree< T > copy(lolly_tree< T > t)
lolly_tree< T > & operator<<(lolly_tree< T > &t, lolly_tree< T > t2)
bool operator==(lolly_tree< T > t, int lab)
bool operator<=(string_u16 a, string_u16 b)
lolly::data::string_view< char16_t > string_u16_view
Structure representing a concrete object with a reference count.
Definition classdef.hpp:24
base class of resources
Definition resource.hpp:23
url delta(url base, url u)
Definition url.cpp:469