source: libcfa/src/collections/string_res.hfa @ 3ee8853

Last change on this file since 3ee8853 was 416b443, checked in by Michael Brooks <mlbrooks@…>, 14 months ago

Implement full set of relational operators for strings

  • Property mode set to 100644
File size: 7.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 : Sat Aug 12 15:45:47 2023
13// Update Count     : 2
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}
80
81void ?{}(string_res &s, const string_res & s2) = void;
82void ?{}(string_res &s, string_res & s2) = void;
83
84enum StrResInitMode { COPY_VALUE, SHARE_EDITS };
85void ?{}(string_res &s, const string_res & src, StrResInitMode, size_t start, size_t end );
86static inline void ?{}(string_res &s, const string_res & src, StrResInitMode mode ) {
87    ?{}( s, src, mode, 0, size(src));
88}
89
90string_res & assign(string_res &s, const char* buffer, size_t bsize); // copy specific length from buffer
91static inline string_res & ?=?(string_res &s, const char* other) {  // copy from string literal (NULL-terminated)
92    return assign(s, other, strlen(other));
93}
94string_res & ?=?(string_res &s, const string_res &other);
95string_res & ?=?(string_res &s, string_res &other);
96string_res & ?=?(string_res &s, char other);
97
98void ^?{}(string_res &s);
99
100// IO Operator
101ofstream & ?|?(ofstream &out, const string_res &s);
102void ?|?(ofstream &out, const string_res &s);
103ifstream & ?|?(ifstream &in, string_res &s);
104void ?|?( ifstream & in, string_res & this );
105
106struct _Istream_Rstr {
107        string_res * s;
108        inline _Istream_str_base;
109}; // _Istream_Rstr
110
111static inline {
112        // read width does not include null terminator
113        _Istream_Rstr wdi( unsigned int rwd, string_res & s ) { return (_Istream_Rstr)@{ &s, {{0p}, rwd, {.flags.rwd : true}} }; }
114        _Istream_Rstr getline( string_res & s, const char delimiter = '\n' ) {
115                return (_Istream_Rstr)@{ &s, {{.delimiter : { delimiter, '\0' } }, -1, {.flags.delimiter : true, .flags.inex : true}} };
116        }
117        _Istream_Rstr & getline( _Istream_Rstr & fmt, const char delimiter = '\n' ) {
118                fmt.delimiter[0] = delimiter; fmt.delimiter[1] = '\0'; fmt.flags.delimiter = true; fmt.flags.inex = true; return fmt;
119        }
120        _Istream_Rstr incl( const char scanset[], string_res & s ) { return (_Istream_Rstr)@{ &s, {{scanset}, -1, {.flags.inex : false}} }; }
121        _Istream_Rstr & incl( const char scanset[], _Istream_Rstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; }
122        _Istream_Rstr excl( const char scanset[], string_res & s ) { return (_Istream_Rstr)@{ &s, {{scanset}, -1, {.flags.inex : true}} }; }
123        _Istream_Rstr & excl( const char scanset[], _Istream_Rstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; }
124        _Istream_Rstr ignore( string_res & s ) { return (_Istream_Rstr)@{ &s, {{0p}, -1, {.flags.ignore : true}} }; }
125        _Istream_Rstr & ignore( _Istream_Rstr & fmt ) { fmt.flags.ignore = true; return fmt; }
126} // distribution
127ifstream & ?|?( ifstream & is, _Istream_Rstr f );
128void ?|?( ifstream & is, _Istream_Rstr t );
129
130// Concatenation
131void append(string_res &s, const char* buffer, size_t bsize);
132void ?+=?(string_res &s, char other); // append a character
133void ?+=?(string_res &s, const string_res &s2); // append-concatenate to first string
134static inline void ?+=?(string_res &s, const char* other) {
135    append( s, other, strlen(other) );
136}
137
138// Character access
139void assignAt(const string_res &s, size_t index, char val);
140char ?[?](const string_res &s, size_t index); // Mike changed to ret by val from Sunjay's ref, to match Peter's
141//char codePointAt(const string_res &s, size_t index); // revisit under Unicode
142
143// Comparisons
144int  cmp (const string_res &, const string_res &);
145bool ?==?(const string_res &, const string_res &);
146bool ?!=?(const string_res &, const string_res &);
147bool ?>? (const string_res &, const string_res &);
148bool ?>=?(const string_res &, const string_res &);
149bool ?<=?(const string_res &, const string_res &);
150bool ?<? (const string_res &, const string_res &);
151
152int  cmp (const string_res &, const char*);
153bool ?==?(const string_res &, const char*);
154bool ?!=?(const string_res &, const char*);
155bool ?>? (const string_res &, const char*);
156bool ?>=?(const string_res &, const char*);
157bool ?<=?(const string_res &, const char*);
158bool ?<? (const string_res &, const char*);
159
160int  cmp (const char*, const string_res &);
161bool ?==?(const char*, const string_res &);
162bool ?!=?(const char*, const string_res &);
163bool ?>? (const char*, const string_res &);
164bool ?>=?(const char*, const string_res &);
165bool ?<=?(const char*, const string_res &);
166bool ?<? (const char*, const string_res &);
167
168// String search
169bool contains(const string_res &s, char ch); // single character
170
171int find(const string_res &s, char search);
172int find(const string_res &s, const string_res &search);
173int find(const string_res &s, const char* search);
174int find(const string_res &s, const char* search, size_t searchsize);
175
176int findFrom(const string_res &s, size_t fromPos, char search);
177int findFrom(const string_res &s, size_t fromPos, const string_res &search);
178int findFrom(const string_res &s, size_t fromPos, const char* search);
179int findFrom(const string_res &s, size_t fromPos, const char* search, size_t searchsize);
180
181bool includes(const string_res &s, const string_res &search);
182bool includes(const string_res &s, const char* search);
183bool includes(const string_res &s, const char* search, size_t searchsize);
184
185bool startsWith(const string_res &s, const string_res &prefix);
186bool startsWith(const string_res &s, const char* prefix);
187bool startsWith(const string_res &s, const char* prefix, size_t prefixsize);
188
189bool endsWith(const string_res &s, const string_res &suffix);
190bool endsWith(const string_res &s, const char* suffix);
191bool endsWith(const string_res &s, const char* suffix, size_t suffixsize);
192
193int include(const string_res &s, const charclass_res &mask);
194int exclude(const string_res &s, const charclass_res &mask);
195
196// Modifiers
197void padStart(string_res &s, size_t n);
198void padStart(string_res &s, size_t n, char padding);
199void padEnd(string_res &s, size_t n);
200void padEnd(string_res &s, size_t n, char padding);
201
Note: See TracBrowser for help on using the repository browser.