1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // string_res -- variable-length, mutable run of text, with resource semantics |
---|
8 | // |
---|
9 | // Author : Michael L. Brooks |
---|
10 | // Created On : Fri Sep 03 11:00:00 2021 |
---|
11 | // Last Modified By : Michael L. Brooks |
---|
12 | // Last Modified On : Fri Sep 03 11:00:00 2021 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <fstream.hfa> |
---|
19 | #include <string.h> // e.g. strlen |
---|
20 | |
---|
21 | |
---|
22 | //######################### HandleNode ######################### |
---|
23 | //private |
---|
24 | |
---|
25 | struct VbyteHeap; |
---|
26 | |
---|
27 | struct HandleNode { |
---|
28 | HandleNode *flink; // forward link |
---|
29 | HandleNode *blink; // backward link |
---|
30 | VbyteHeap *ulink; // upward link |
---|
31 | |
---|
32 | char *s; // pointer to byte string |
---|
33 | unsigned int lnth; // length of byte string |
---|
34 | }; // HandleNode |
---|
35 | |
---|
36 | VbyteHeap * DEBUG_string_heap(); |
---|
37 | size_t DEBUG_string_bytes_in_heap( VbyteHeap * heap ); |
---|
38 | size_t DEBUG_string_bytes_avail_until_gc( VbyteHeap * heap ); |
---|
39 | const char * DEBUG_string_heap_start( VbyteHeap * heap ); |
---|
40 | |
---|
41 | |
---|
42 | //######################### String ######################### |
---|
43 | |
---|
44 | // A dynamically-sized string |
---|
45 | struct string_res { |
---|
46 | HandleNode Handle; // chars, start, end, global neighbours |
---|
47 | bool shareEditSet_owns_ulink; |
---|
48 | string_res * shareEditSet_prev; |
---|
49 | string_res * shareEditSet_next; |
---|
50 | }; |
---|
51 | |
---|
52 | |
---|
53 | //######################### charclass_res ######################### |
---|
54 | |
---|
55 | struct charclass_res { |
---|
56 | string_res chars; |
---|
57 | }; |
---|
58 | |
---|
59 | void ?{}( charclass_res & ) = void; |
---|
60 | void ?{}( charclass_res &, charclass_res) = void; |
---|
61 | charclass_res ?=?( charclass_res &, charclass_res) = void; |
---|
62 | void ?{}( charclass_res &, const string_res & chars); |
---|
63 | void ?{}( charclass_res &, const char * chars ); |
---|
64 | void ?{}( charclass_res &, const char * chars, size_t charssize ); |
---|
65 | void ^?{}( charclass_res & ); |
---|
66 | |
---|
67 | |
---|
68 | //######################### String ######################### |
---|
69 | |
---|
70 | // Getters |
---|
71 | size_t size(const string_res &s); |
---|
72 | |
---|
73 | // Constructors, Assignment Operators, Destructor |
---|
74 | void ?{}(string_res &s); // empty string |
---|
75 | void ?{}(string_res &s, const char* buffer, size_t bsize); // copy specific length from buffer |
---|
76 | static inline void ?{}(string_res &s, const char* rhs) { // copy from string literal (NULL-terminated) |
---|
77 | (s){ rhs, strlen(rhs) }; |
---|
78 | } |
---|
79 | |
---|
80 | void ?{}(string_res &s, const string_res & s2) = void; |
---|
81 | void ?{}(string_res &s, string_res & s2) = void; |
---|
82 | |
---|
83 | enum StrResInitMode { COPY_VALUE, SHARE_EDITS }; |
---|
84 | void ?{}(string_res &s, const string_res & src, StrResInitMode, size_t start, size_t end ); |
---|
85 | static inline void ?{}(string_res &s, const string_res & src, StrResInitMode mode ) { |
---|
86 | ?{}( s, src, mode, 0, size(src)); |
---|
87 | } |
---|
88 | |
---|
89 | string_res & assign(string_res &s, const char* buffer, size_t bsize); // copy specific length from buffer |
---|
90 | static inline string_res & ?=?(string_res &s, const char* other) { // copy from string literal (NULL-terminated) |
---|
91 | return assign(s, other, strlen(other)); |
---|
92 | } |
---|
93 | string_res & ?=?(string_res &s, const string_res &other); |
---|
94 | string_res & ?=?(string_res &s, string_res &other); |
---|
95 | string_res & ?=?(string_res &s, char other); |
---|
96 | |
---|
97 | void ^?{}(string_res &s); |
---|
98 | |
---|
99 | // IO Operator |
---|
100 | ofstream & ?|?(ofstream &out, const string_res &s); |
---|
101 | void ?|?(ofstream &out, const string_res &s); |
---|
102 | |
---|
103 | // Concatenation |
---|
104 | void append(string_res &s, const char* buffer, size_t bsize); |
---|
105 | void ?+=?(string_res &s, char other); // append a character |
---|
106 | void ?+=?(string_res &s, const string_res &s2); // append-concatenate to first string |
---|
107 | static inline void ?+=?(string_res &s, const char* other) { |
---|
108 | append( s, other, strlen(other) ); |
---|
109 | } |
---|
110 | |
---|
111 | // Character access |
---|
112 | void assignAt(const string_res &s, size_t index, char val); |
---|
113 | char ?[?](const string_res &s, size_t index); // Mike changed to ret by val from Sunjay's ref, to match Peter's |
---|
114 | //char codePointAt(const string_res &s, size_t index); // revisit under Unicode |
---|
115 | |
---|
116 | // Comparisons |
---|
117 | bool ?==?(const string_res &s, const string_res &other); |
---|
118 | bool ?!=?(const string_res &s, const string_res &other); |
---|
119 | bool ?==?(const string_res &s, const char* other); |
---|
120 | bool ?!=?(const string_res &s, const char* other); |
---|
121 | |
---|
122 | // String search |
---|
123 | bool contains(const string_res &s, char ch); // single character |
---|
124 | |
---|
125 | int find(const string_res &s, char search); |
---|
126 | int find(const string_res &s, const string_res &search); |
---|
127 | int find(const string_res &s, const char* search); |
---|
128 | int find(const string_res &s, const char* search, size_t searchsize); |
---|
129 | |
---|
130 | bool includes(const string_res &s, const string_res &search); |
---|
131 | bool includes(const string_res &s, const char* search); |
---|
132 | bool includes(const string_res &s, const char* search, size_t searchsize); |
---|
133 | |
---|
134 | bool startsWith(const string_res &s, const string_res &prefix); |
---|
135 | bool startsWith(const string_res &s, const char* prefix); |
---|
136 | bool startsWith(const string_res &s, const char* prefix, size_t prefixsize); |
---|
137 | |
---|
138 | bool endsWith(const string_res &s, const string_res &suffix); |
---|
139 | bool endsWith(const string_res &s, const char* suffix); |
---|
140 | bool endsWith(const string_res &s, const char* suffix, size_t suffixsize); |
---|
141 | |
---|
142 | int include(const string_res &s, const charclass_res &mask); |
---|
143 | int exclude(const string_res &s, const charclass_res &mask); |
---|
144 | |
---|
145 | // Modifiers |
---|
146 | void padStart(string_res &s, size_t n); |
---|
147 | void padStart(string_res &s, size_t n, char padding); |
---|
148 | void padEnd(string_res &s, size_t n); |
---|
149 | void padEnd(string_res &s, size_t n, char padding); |
---|
150 | |
---|