Lolly 1.4.28
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
28public:
29 inline string_rep () : n (0), a (NULL) {}
30 string_rep (int n);
31 inline ~string_rep () {
32 if (n != 0) tm_delete_array (a);
33 }
34 void resize (int n);
35
36 friend class string;
37 friend inline int N (string a);
38};
39
40class string {
41 CONCRETE (string);
42 inline string () : rep (tm_new<string_rep> ()) {}
43 inline string (int n) : rep (tm_new<string_rep> (n)) {}
44 string (char c);
45 string (char c, int n);
46 string (const char* s);
47 string (const char* s, int n);
48 inline char& operator[] (int i) { return rep->a[i]; }
49 bool operator== (const char* s);
50 bool operator!= (const char* s);
51 bool operator== (string s);
52 bool operator!= (string s);
53 string operator() (int start, int end);
54 char* begin () { return rep->a; }
55 char* end () { return rep->a + rep->n; }
56
57 inline operator string_view<char> () {
58 return string_view<char> (rep->a, rep->n);
59 }
60};
62
63extern inline int
64N (string a) {
65 return a->n;
66}
67string copy (string a);
68string& operator<< (string& a, char);
69string& operator<< (string& a, string b);
70string operator* (const char* a, string b);
71string operator* (string a, string b);
72string operator* (string a, const char* b);
73bool operator< (string a, string b);
74bool operator<= (string a, string b);
75int hash (string s);
76
77bool as_bool (string s);
78int as_int (string s);
79long int as_long_int (string s);
80double as_double (string s);
81char* as_charp (string s);
82string as_string_bool (bool f);
83string as_string (int16_t i);
84string as_string (int32_t i);
85string as_string (int64_t i);
86string as_string (unsigned int i);
87string as_string (unsigned long int i);
88string as_string (double x);
89string as_string (const char* s);
90bool is_empty (string s);
91bool is_bool (string s);
92bool is_int (string s);
93bool is_double (string s);
94bool is_charp (string s);
95
96bool is_quoted (string s);
97bool is_id (string s);
98
99/******************************************************************************
100 * C-style strings with automatic memory management
101 ******************************************************************************/
102
103class c_string;
105 char* value;
106
107private:
109 // disable copy constructor
110 inline c_string_rep& operator= (c_string_rep&) { return *this; }
111 // disable assignment
112
113public:
114 inline c_string_rep (char* v= NULL) : value (v) {}
115 inline ~c_string_rep () {
117 }
118 friend class c_string;
119};
120
121class c_string {
123
124public:
125 inline c_string () : rep (tm_new<c_string_rep> ()) {}
126 inline c_string (int len)
128 inline c_string (string s) : rep (tm_new<c_string_rep> (as_charp (s))) {}
129 inline operator char* () const { return rep->value; }
130};
132
133typedef string string_u8;
134
135#endif // defined STRING_H
blackbox b[13]
c_string_rep(char *v=NULL)
Definition string.hpp:114
char * value
Definition string.hpp:105
c_string_rep & operator=(c_string_rep &)
Definition string.hpp:110
c_string_rep(c_string_rep &)
Definition string.hpp:108
CONCRETE(c_string)
c_string(int len)
Definition string.hpp:126
c_string(string s)
Definition string.hpp:128
#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 resize(int n)
Definition string.cpp:39
~string_rep()
Definition string.hpp:31
friend int N(string a)
Definition string.hpp:64
string()
Definition string.hpp:42
bool operator!=(const char *s)
Definition string.cpp:97
CONCRETE(string)
char & operator[](int i)
Definition string.hpp:48
string(int n)
Definition string.hpp:43
char * end()
Definition string.hpp:55
char * begin()
Definition string.hpp:54
bool operator==(const char *s)
Definition string.cpp:86
string operator()(int start, int end)
Definition string.cpp:118
C * tm_new_array(int n)
C * tm_new()
void tm_delete_array(C *Ptr)
int as_int(string s)
Definition string.cpp:206
string string_u8
Definition string.hpp:133
bool is_charp(string s)
Definition string.cpp:364
string copy(string a)
Definition string.cpp:131
bool operator<(string a, string b)
Definition string.cpp:177
string as_string_bool(bool f)
Definition string.cpp:266
char * as_charp(string s)
Definition string.cpp:256
bool is_double(string s)
Definition string.cpp:338
bool as_bool(string s)
Definition string.cpp:201
string as_string(int16_t i)
Definition string.cpp:272
string operator*(const char *a, string b)
Definition string.cpp:167
bool is_int(string s)
Definition string.cpp:326
long int as_long_int(string s)
Definition string.cpp:223
double as_double(string s)
Definition string.cpp:241
bool is_bool(string s)
Definition string.cpp:321
string & operator<<(string &a, char)
Definition string.cpp:140
bool operator<=(string a, string b)
Definition string.cpp:182
bool is_id(string s)
Definition string.cpp:376
bool is_quoted(string s)
Definition string.cpp:370
int N(string a)
Definition string.hpp:64
bool is_empty(string s)
Definition string.cpp:316
int hash(string s)
Definition string.cpp:187
Structure representing a concrete object with a reference count.
Definition classdef.hpp:24
base class of resources
Definition resource.hpp:23