Lolly 1.4.27
Loading...
Searching...
No Matches
string.hpp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : string.hpp
4 * DESCRIPTION: Fixed size strings with reference counting and
5 * pointer copying. Zero-characters are allowed in strings.
6 * COPYRIGHT : (C) 1999 Joris van der Hoeven
7 *******************************************************************************
8 * This software falls under the GNU general public license version 3 or later.
9 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
10 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ******************************************************************************/
12
13#ifndef STRING_H
14#define STRING_H
15#include "classdef.hpp"
16#include "fast_alloc.hpp"
18#include "minmax.hpp"
19#include <stdint.h>
20
22
23class string;
25 int n;
26 char* a;
27 int a_N;
28
29public:
30 inline string_rep () : n (0), a_N (0), a (NULL) {}
31 string_rep (int n);
32 inline ~string_rep () {
33 if (a_N != 0) tm_delete_array (a);
34 }
35 /**
36 * @brief expand (or shrink) string by delta, but do not release memory when
37 * string is shrinked.
38 *
39 * @return string length before expansion
40 */
41 int expand_or_shrink_by (int delta);
42
43 /**
44 * @brief expand (or shrink) string to given length n, and try to release
45 * memory when string is shrinked.
46 *
47 * @note expand_or_shrink_by may be faster if memory space is reserved
48 */
49 void resize (int n);
50
51 /**
52 * @brief reserve memory space to contain at least n word in the whole string.
53 * Do not affect length of string, and do not release memory when n is smaller
54 * than current space.
55 */
56 void reserve (int n);
57
58 friend class string;
59 friend inline int N (string a);
60};
61
62class string {
63 CONCRETE (string);
64 inline string () : rep (tm_new<string_rep> ()) {}
65 inline string (int n) : rep (tm_new<string_rep> (n)) {}
66 string (char c);
67 string (char c, int n);
68 string (const char* s);
69 string (const char* s, int n);
70 inline char& operator[] (int i) { return rep->a[i]; }
71 bool operator== (const char* s);
72 bool operator!= (const char* s);
73 bool operator== (string s);
74 bool operator!= (string s);
75 string operator() (int start, int end);
76 char* begin () { return rep->a; }
77 char* end () { return rep->a + rep->n; }
78
79 inline operator string_view<char> () {
80 return string_view<char> (rep->a, rep->n);
81 }
82};
84
85extern inline int
86N (string a) {
87 return a->n;
88}
89string copy (string a);
90string& operator<< (string& a, char);
91string& operator<< (string& a, string b);
92string operator* (const char* a, string b);
93string operator* (string a, string b);
94string operator* (string a, const char* b);
95bool operator< (string a, string b);
96bool operator<= (string a, string b);
97int hash (string s);
98
99bool as_bool (string s);
100int as_int (string s);
101long int as_long_int (string s);
102double as_double (string s);
103char* as_charp (string s);
104string as_string_bool (bool f);
105string as_string (int16_t i);
106string as_string (int32_t i);
107string as_string (int64_t i);
108string as_string (unsigned int i);
109string as_string (unsigned long int i);
110string as_string (double x);
111string as_string (const char* s);
112bool is_empty (string s);
113bool is_bool (string s);
114bool is_int (string s);
115bool is_double (string s);
116bool is_charp (string s);
117
118bool is_quoted (string s);
119bool is_id (string s);
120
121/******************************************************************************
122 * C-style strings with automatic memory management
123 ******************************************************************************/
124
125class c_string;
127 char* value;
128
129private:
131 // disable copy constructor
132 inline c_string_rep& operator= (c_string_rep&) { return *this; }
133 // disable assignment
134
135public:
136 inline c_string_rep (char* v= NULL) : value (v) {}
137 inline ~c_string_rep () {
139 }
140 friend class c_string;
141};
142
143class c_string {
145
146public:
147 inline c_string () : rep (tm_new<c_string_rep> ()) {}
148 inline c_string (int len)
150 inline c_string (string s) : rep (tm_new<c_string_rep> (as_charp (s))) {}
151 inline operator char* () const { return rep->value; }
152};
154
155typedef string string_u8;
156
157#endif // defined STRING_H
blackbox b[13]
c_string_rep(char *v=NULL)
Definition string.hpp:136
char * value
Definition string.hpp:127
c_string_rep & operator=(c_string_rep &)
Definition string.hpp:132
c_string_rep(c_string_rep &)
Definition string.hpp:130
CONCRETE(c_string)
c_string(int len)
Definition string.hpp:148
c_string(string s)
Definition string.hpp:150
#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
char * a
Definition string.hpp:26
void reserve(int n)
reserve memory space to contain at least n word in the whole string. Do not affect length of string,...
Definition string.cpp:73
int expand_or_shrink_by(int delta)
expand (or shrink) string by delta, but do not release memory when string is shrinked.
Definition string.cpp:65
void resize(int n)
expand (or shrink) string to given length n, and try to release memory when string is shrinked.
Definition string.cpp:41
~string_rep()
Definition string.hpp:32
friend int N(string a)
Definition string.hpp:86
string()
Definition string.hpp:64
bool operator!=(const char *s)
Definition string.cpp:135
CONCRETE(string)
char & operator[](int i)
Definition string.hpp:70
string(int n)
Definition string.hpp:65
char * end()
Definition string.hpp:77
char * begin()
Definition string.hpp:76
bool operator==(const char *s)
Definition string.cpp:124
string operator()(int start, int end)
Definition string.cpp:156
C * tm_new_array(int n)
C * tm_new()
void tm_delete_array(C *Ptr)
int as_int(string s)
Definition string.cpp:244
string string_u8
Definition string.hpp:155
bool is_charp(string s)
Definition string.cpp:402
string copy(string a)
Definition string.cpp:169
bool operator<(string a, string b)
Definition string.cpp:215
string as_string_bool(bool f)
Definition string.cpp:304
char * as_charp(string s)
Definition string.cpp:294
bool is_double(string s)
Definition string.cpp:376
bool as_bool(string s)
Definition string.cpp:239
string as_string(int16_t i)
Definition string.cpp:310
string operator*(const char *a, string b)
Definition string.cpp:205
bool is_int(string s)
Definition string.cpp:364
long int as_long_int(string s)
Definition string.cpp:261
double as_double(string s)
Definition string.cpp:279
bool is_bool(string s)
Definition string.cpp:359
string & operator<<(string &a, char)
Definition string.cpp:178
bool operator<=(string a, string b)
Definition string.cpp:220
bool is_id(string s)
Definition string.cpp:414
bool is_quoted(string s)
Definition string.cpp:408
int N(string a)
Definition string.hpp:86
bool is_empty(string s)
Definition string.cpp:354
int hash(string s)
Definition string.cpp:225
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