source: libcfa/src/collections/string_res.hfa@ 01fb70a

stuck-waitfor-destruct
Last change on this file since 01fb70a was f2898df, checked in by Michael Brooks <mlbrooks@…>, 2 years ago

Implement string initialization and assignment from various numeric types

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