source: libcfa/src/collections/string_res.hfa @ e8b3717

Last change on this file since e8b3717 was e8b3717, checked in by Michael Brooks <mlbrooks@…>, 5 months ago

Modify substring interface from start-end to start-len, and add a missing test.

  • Property mode set to 100644
File size: 8.8 KB
Line 
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 : Peter A. Buhr
12// Last Modified On : Thu Jan  4 11:28:06 2024
13// Update Count     : 27
14//
15
16#pragma once
17
18#include <fstream.hfa>
19#include <string.h>    // e.g. strlen
20
21   
22//######################### HandleNode #########################
23//private
24
25struct VbyteHeap;
26
27struct 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
36VbyteHeap * DEBUG_string_heap();
37size_t DEBUG_string_bytes_in_heap( VbyteHeap * heap );
38size_t DEBUG_string_bytes_avail_until_gc( VbyteHeap * heap );
39const char * DEBUG_string_heap_start( VbyteHeap * heap );
40
41void TUNING_set_string_heap_liveness_threshold( double val );
42
43//######################### String #########################
44
45// A dynamically-sized string
46struct string_res {
47    HandleNode Handle; // chars, start, end, global neighbours
48    bool shareEditSet_owns_ulink;
49    string_res * shareEditSet_prev;
50    string_res * shareEditSet_next;
51};
52
53
54//######################### charclass_res #########################
55
56struct charclass_res {
57    string_res chars;
58};
59
60void ?{}( charclass_res & ) = void;
61void ?{}( charclass_res &, charclass_res) = void;
62charclass_res ?=?( charclass_res &, charclass_res) = void;
63void ?{}( charclass_res &, const string_res & chars);
64void ?{}( charclass_res &, const char * chars );
65void ?{}( charclass_res &, const char * chars, size_t charssize );
66void ^?{}( charclass_res & );
67
68
69//######################### String #########################
70
71// Getters
72size_t size(const string_res & s);
73
74// Constructors, Assignment Operators, Destructor
75void ?{}(string_res & s); // empty string
76void ?{}(string_res & s, const char * buffer, size_t bsize); // copy specific length from buffer
77static inline void ?{}(string_res & s, const char * rhs) { // copy from string literal (NULL-terminated)
78    (s){ rhs, strlen(rhs) };
79}
80static inline void ?{}(string_res & s, char c ) {
81    ?{}( s, &c, 1);
82}
83
84// Deleting the copy constructors makes the compiler reject an attempt to call/return by value
85void ?{}(string_res & s, const string_res & s2) = void;
86void ?{}(string_res & s, string_res & s2) = void;
87
88enum StrResInitMode { COPY_VALUE, SHARE_EDITS };
89void ?{}(string_res & s, const string_res & src, StrResInitMode, size_t start, size_t len );
90static inline void ?{}(string_res & s, const string_res & src, StrResInitMode mode ) {
91    ?{}( s, src, mode, 0, size(src));
92}
93static inline void ?{}(string_res & s, const string_res & src, StrResInitMode mode, size_t maxlen ) {
94    ?{}( s, src, mode, 0, (size(src) > maxlen)?maxlen:size(src) );
95}
96
97string_res & assign(string_res & s, const string_res & src, size_t maxlen); // copy specific length from other string
98string_res & assign(string_res & s, const char * buffer, size_t bsize); // copy specific length from buffer
99static inline string_res & ?=?(string_res & s, const char * c) {  // copy from string literal (NULL-terminated)
100    return assign(s, c, strlen(c));
101}
102string_res & ?=?(string_res & s, const string_res & c);
103string_res & ?=?(string_res & s, string_res & c);
104string_res & ?=?(string_res & s, char c);
105
106void ^?{}(string_res & s);
107
108// IO Operator
109ofstream & ?|?(ofstream & out, const string_res & s);
110void ?|?(ofstream & out, const string_res & s);
111ifstream & ?|?(ifstream & in, string_res & s);
112void ?|?( ifstream & in, string_res & s );
113
114struct _Istream_Rstr {
115        string_res * s;
116        inline _Istream_str_base;
117}; // _Istream_Rstr
118
119static inline {
120        // read width does not include null terminator
121        _Istream_Rstr wdi( unsigned int rwd, string_res & s ) { return (_Istream_Rstr)@{ &s, {{0p}, rwd, {.flags.rwd : true}} }; }
122        _Istream_Rstr getline( string_res & s, const char delimiter = '\n' ) {
123                return (_Istream_Rstr)@{ &s, {{.delimiters : { delimiter, '\0' } }, -1, {.flags.delimiter : true, .flags.inex : true}} };
124        }
125        _Istream_Rstr & getline( _Istream_Rstr & fmt, const char delimiter = '\n' ) {
126                fmt.delimiters[0] = delimiter; fmt.delimiters[1] = '\0'; fmt.flags.delimiter = true; fmt.flags.inex = true; return fmt;
127        }
128        _Istream_Rstr incl( const char scanset[], string_res & s ) { return (_Istream_Rstr)@{ &s, {{scanset}, -1, {.flags.inex : false}} }; }
129        _Istream_Rstr & incl( const char scanset[], _Istream_Rstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; }
130        _Istream_Rstr excl( const char scanset[], string_res & s ) { return (_Istream_Rstr)@{ &s, {{scanset}, -1, {.flags.inex : true}} }; }
131        _Istream_Rstr & excl( const char scanset[], _Istream_Rstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; }
132        _Istream_Rstr ignore( string_res & s ) { return (_Istream_Rstr)@{ &s, {{0p}, -1, {.flags.ignore : true}} }; }
133        _Istream_Rstr & ignore( _Istream_Rstr & fmt ) { fmt.flags.ignore = true; return fmt; }
134} // distribution
135ifstream & ?|?( ifstream & is, _Istream_Rstr f );
136void ?|?( ifstream & is, _Istream_Rstr t );
137
138// Concatenation
139void ?+=?(string_res & s, const string_res & s2);
140void ?+=?(string_res & s, char c);
141void append(string_res & s, const string_res & s2, size_t maxlen);
142void ?+=?(string_res & s, const char * c);
143void append(string_res & s, const char * buffer, size_t bsize);
144
145static inline string_res & strcat(string_res & s, const string_res & s2) { s += s2; return s; }
146static inline string_res & strcat(string_res & s, const char * c) { s += c; return s; }
147static inline string_res & strncat(string_res & s, const string_res & s2, size_t maxlen) { append(s, s2, maxlen); return s; }
148static inline string_res & strncat(string_res & s, const char * buffer, size_t bsize) { append(s, buffer, bsize); return s; }
149
150// Repetition
151void ?*=?(string_res & s, size_t factor);
152
153// Character access
154void assignAt(const string_res & s, size_t index, char val);
155char ?[?](const string_res & s, size_t index); // Mike changed to ret by val from Sunjay's ref, to match Peter's
156//char codePointAt(const string_res & s, size_t index); // revisit under Unicode
157
158// Comparisons
159int  strcmp (const string_res &, const string_res &);
160bool ?==?(const string_res &, const string_res &);
161bool ?!=?(const string_res &, const string_res &);
162bool ?>? (const string_res &, const string_res &);
163bool ?>=?(const string_res &, const string_res &);
164bool ?<=?(const string_res &, const string_res &);
165bool ?<? (const string_res &, const string_res &);
166
167int  strcmp(const string_res &, const char *);
168bool ?==?(const string_res &, const char *);
169bool ?!=?(const string_res &, const char *);
170bool ?>? (const string_res &, const char *);
171bool ?>=?(const string_res &, const char *);
172bool ?<=?(const string_res &, const char *);
173bool ?<? (const string_res &, const char *);
174
175int  strcmp(const char *, const string_res &);
176bool ?==?(const char *, const string_res &);
177bool ?!=?(const char *, const string_res &);
178bool ?>? (const char *, const string_res &);
179bool ?>=?(const char *, const string_res &);
180bool ?<=?(const char *, const string_res &);
181bool ?<? (const char *, const string_res &);
182
183// String search
184bool contains(const string_res & s, char ch); // single character
185
186int find(const string_res & s, char search);
187int find(const string_res & s, const string_res & search);
188int find(const string_res & s, const char * search);
189int find(const string_res & s, const char * search, size_t searchsize);
190
191int findFrom(const string_res & s, size_t fromPos, char search);
192int findFrom(const string_res & s, size_t fromPos, const string_res & search);
193int findFrom(const string_res & s, size_t fromPos, const char * search);
194int findFrom(const string_res & s, size_t fromPos, const char * search, size_t searchsize);
195
196bool includes(const string_res & s, const string_res & search);
197bool includes(const string_res & s, const char * search);
198bool includes(const string_res & s, const char * search, size_t searchsize);
199
200bool startsWith(const string_res & s, const string_res & prefix);
201bool startsWith(const string_res & s, const char * prefix);
202bool startsWith(const string_res & s, const char * prefix, size_t prefixsize);
203
204bool endsWith(const string_res & s, const string_res & suffix);
205bool endsWith(const string_res & s, const char * suffix);
206bool endsWith(const string_res & s, const char * suffix, size_t suffixsize);
207
208int include(const string_res & s, const charclass_res & mask);
209int exclude(const string_res & s, const charclass_res & mask);
210
211// Modifiers
212void padStart(string_res & s, size_t n);
213void padStart(string_res & s, size_t n, char padding);
214void padEnd(string_res & s, size_t n);
215void padEnd(string_res &s, size_t n, char padding);
216
Note: See TracBrowser for help on using the repository browser.