source: libcfa/src/collections/string_res.hfa @ 1fc111c

Last change on this file since 1fc111c was ff56dd2e, checked in by Michael Brooks <mlbrooks@…>, 14 months ago

Duplicate manipulator read-to-string code for string_res, and test.

  • Property mode set to 100644
File size: 7.1 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
144bool ?==?(const string_res &s, const string_res &other);
145bool ?!=?(const string_res &s, const string_res &other);
146bool ?==?(const string_res &s, const char* other);
147bool ?!=?(const string_res &s, const char* other);
148
149// String search
150bool contains(const string_res &s, char ch); // single character
151
152int find(const string_res &s, char search);
153int find(const string_res &s, const string_res &search);
154int find(const string_res &s, const char* search);
155int find(const string_res &s, const char* search, size_t searchsize);
156
157int findFrom(const string_res &s, size_t fromPos, char search);
158int findFrom(const string_res &s, size_t fromPos, const string_res &search);
159int findFrom(const string_res &s, size_t fromPos, const char* search);
160int findFrom(const string_res &s, size_t fromPos, const char* search, size_t searchsize);
161
162bool includes(const string_res &s, const string_res &search);
163bool includes(const string_res &s, const char* search);
164bool includes(const string_res &s, const char* search, size_t searchsize);
165
166bool startsWith(const string_res &s, const string_res &prefix);
167bool startsWith(const string_res &s, const char* prefix);
168bool startsWith(const string_res &s, const char* prefix, size_t prefixsize);
169
170bool endsWith(const string_res &s, const string_res &suffix);
171bool endsWith(const string_res &s, const char* suffix);
172bool endsWith(const string_res &s, const char* suffix, size_t suffixsize);
173
174int include(const string_res &s, const charclass_res &mask);
175int exclude(const string_res &s, const charclass_res &mask);
176
177// Modifiers
178void padStart(string_res &s, size_t n);
179void padStart(string_res &s, size_t n, char padding);
180void padEnd(string_res &s, size_t n);
181void padEnd(string_res &s, size_t n, char padding);
182
Note: See TracBrowser for help on using the repository browser.