source: libcfa/src/collections/string.hfa@ 9018dcf

Last change on this file since 9018dcf was 9018dcf, checked in by Peter A. Buhr <pabuhr@…>, 6 months ago

updates to string type

  • Property mode set to 100644
File size: 14.6 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 -- variable-length, mutable run of text, with value 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 : Wed Apr 9 22:27:41 2025
13// Update Count : 259
14//
15
16#pragma once
17
18#include <fstream.hfa>
19#include <string_res.hfa>
20
21struct string {
22 string_res * inner;
23};
24
25// Getters
26static inline size_t len( const string & s ) { return len( *s.inner ); }
27static inline size_t len( const char * cs ) { return strlen( cs ); };
28static inline size_t strlen( const string & s ) { return len( s ); }
29
30// RAII, assignment
31void ?{}( string & s ); // empty string
32void ?{}( string & s, const string & s2 );
33void ?{}( string & s, const string & s2, size_t maxlen );
34void ?{}( string & s, string & s2 );
35
36void ?{}( string & s, char );
37void ?{}( string & s, const char * c ); // copy from string literal (NULL-terminated)
38void ?{}( string & s, const char * c, size_t size ); // copy specific length from buffer
39
40void ?{}( string & s, ssize_t rhs );
41void ?{}( string & s, size_t rhs );
42void ?{}( string & s, double rhs );
43void ?{}( string & s, long double rhs );
44void ?{}( string & s, double _Complex rhs );
45void ?{}( string & s, long double _Complex rhs );
46
47// string str( ssize_t rhs );
48// string str( size_t rhs );
49// string str( double rhs );
50// string str( long double rhs );
51// string str( double _Complex rhs );
52// string str( long double _Complex rhs );
53
54string & ?=?( string & s, const string & c );
55string & ?=?( string & s, string & c );
56string & ?=?( string & s, const char * c ); // copy from "literal"
57string & ?=?( string & s, char c ); // copy from 'l'
58string & assign( string & s, const string & c, size_t n );
59string & assign( string & s, const char * c, size_t n );
60
61static inline string & strcpy( string & s, const char * c ) { s = c; return s; }
62static inline string & strncpy( string & s, const char * c, size_t n ) { assign( s, c, n ); return s; }
63static inline string & strcpy( string & s, const string & c ) { s = c; return s; }
64static inline string & strncpy( string & s, const string & c, size_t n ) { assign( s, c, n ); return s; }
65
66string & ?=?( string & s, ssize_t rhs );
67string & ?=?( string & s, size_t rhs );
68string & ?=?( string & s, double rhs );
69string & ?=?( string & s, long double rhs );
70string & ?=?( string & s, double _Complex rhs );
71string & ?=?( string & s, long double _Complex rhs );
72
73void ^?{}( string & s );
74
75// Alternate construction: request shared edits
76struct string_Share {
77 string * s;
78};
79string_Share ?`share( string & s );
80void ?{}( string & s, string_Share src );
81
82// IO Operator
83ofstream & ?|?( ofstream & out, const string & s );
84void ?|?( ofstream & out, const string & s );
85ifstream & ?|?( ifstream & in, string & s );
86
87static inline {
88 _Ostream_Manip(string) bin( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'b', { .all = 0 } }; }
89 _Ostream_Manip(string) oct( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'o', { .all = 0 } }; }
90 _Ostream_Manip(string) hex( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'x', { .all = 0 } }; }
91 _Ostream_Manip(string) wd( unsigned int w, string s ) { return (_Ostream_Manip(string))@{ s, w, 0, 's', { .all = 0 } }; }
92 _Ostream_Manip(string) wd( unsigned int w, unsigned int pc, string s ) { return (_Ostream_Manip(string))@{ s, w, pc, 's', { .flags.pc = true } }; }
93 _Ostream_Manip(string) & wd( unsigned int w, _Ostream_Manip(string) & fmt ) { fmt.wd = w; return fmt; }
94 _Ostream_Manip(string) & wd( unsigned int w, unsigned int pc, _Ostream_Manip(string) & fmt ) { fmt.wd = w; fmt.pc = pc; fmt.flags.pc = true; return fmt; }
95 _Ostream_Manip(string) & left( _Ostream_Manip(string) & fmt ) { fmt.flags.left = true; return fmt; }
96 _Ostream_Manip(string) & nobase( _Ostream_Manip(string) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
97} // distribution
98ofstream & ?|?( ofstream & os, _Ostream_Manip(string) f );
99void ?|?( ofstream & os, _Ostream_Manip(string) );
100
101struct _Istream_Swidth {
102 string & s;
103 inline _Istream_str_base;
104}; // _Istream_Swidth
105
106struct _Istream_Squoted {
107 _Istream_Swidth sstr;
108}; // _Istream_Squoted
109
110struct _Istream_Sstr {
111 string & s;
112 inline _Istream_str_base;
113// _Istream_Swidth sstr;
114}; // _Istream_Sstr
115
116static inline {
117 // read width does not include null terminator
118 _Istream_Swidth wdi( unsigned int rwd, string & s ) { return (_Istream_Swidth)@{ .s = s, { {.scanset = 0p}, .wd = rwd, {.flags.rwd = true} } }; }
119 _Istream_Sstr getline( string & s, const char delimiter = '\n' ) {
120// return (_Istream_Sstr)@{ { .s = s, { {.delimiters = { delimiter, '\0' } }, .wd = -1, {.flags.delimiter = true} } } };
121 return (_Istream_Sstr)@{ .s = s, { {.delimiters = { delimiter, '\0' } }, .wd = -1, {.flags.delimiter = true} } };
122 }
123 _Istream_Sstr & getline( _Istream_Swidth & f, const char delimiter = '\n' ) {
124 f.delimiters[0] = delimiter; f.delimiters[1] = '\0'; f.flags.delimiter = true; return (_Istream_Sstr &)f;
125 }
126 _Istream_Squoted quoted( string & s, const char Ldelimiter = '\"', const char Rdelimiter = '\0' ) {
127 return (_Istream_Squoted)@{ { .s = s, { {.delimiters = { Ldelimiter, Rdelimiter, '\0' }}, .wd = -1, {.flags.rwd = true} } } };
128 }
129 _Istream_Squoted & quoted( _Istream_Swidth & f, const char Ldelimiter = '"', const char Rdelimiter = '\0' ) {
130 f.delimiters[0] = Ldelimiter; f.delimiters[1] = Rdelimiter; f.delimiters[2] = '\0';
131 return (_Istream_Squoted &)f;
132 }
133// _Istream_Sstr incl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ { .s = s, { {.scanset = scanset}, .wd = -1, {.flags.inex = false} } } }; }
134 _Istream_Sstr incl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ .s = s, { {.scanset = scanset}, .wd = -1, {.flags.inex = false} } }; }
135 _Istream_Sstr & incl( const char scanset[], _Istream_Swidth & f ) { f.scanset = scanset; f.flags.inex = false; return (_Istream_Sstr &)f; }
136// _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ { .s = s, { {.scanset = scanset}, .wd = -1, {.flags.inex = true} } } }; }
137 _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ .s = s, { {.scanset = scanset}, .wd = -1, {.flags.inex = true} } }; }
138 _Istream_Sstr & excl( const char scanset[], _Istream_Swidth & f ) { f.scanset = scanset; f.flags.inex = true; return (_Istream_Sstr &)f; }
139// _Istream_Sstr ignore( string & s ) { return (_Istream_Sstr)@{ { .s = s, { {.scanset = 0p}, .wd = -1, {.flags.ignore = true} } } }; }
140 _Istream_Sstr ignore( string & s ) { return (_Istream_Sstr)@{ .s = s, { {.scanset = 0p}, .wd = -1, {.flags.ignore = true} } }; }
141 _Istream_Sstr & ignore( _Istream_Swidth & f ) { f.flags.ignore = true; return (_Istream_Sstr &)f; }
142 _Istream_Squoted & ignore( _Istream_Squoted & f ) { f.sstr.flags.ignore = true; return (_Istream_Squoted &)f; }
143// _Istream_Sstr & ignore( _Istream_Sstr & f ) { f.sstr.flags.ignore = true; return (_Istream_Sstr &)f; }
144 _Istream_Sstr & ignore( _Istream_Sstr & f ) { f.flags.ignore = true; return (_Istream_Sstr &)f; }
145} // distribution
146ifstream & ?|?( ifstream & is, _Istream_Squoted f );
147ifstream & ?|?( ifstream & is, _Istream_Sstr f );
148static inline ifstream & ?|?( ifstream & is, _Istream_Swidth f ) { return is | *(_Istream_Sstr *)&f; }
149
150// Concatenation
151void ?+=?( string & s, char c );
152void ?+=?( string & s, const string & s2 );
153void append( string & s, const string & s2, size_t maxlen );
154void ?+=?( string & s, const char * s2 );
155void append( string & s, const char * buffer, size_t bsize );
156
157string ?+?( const string & s, char c );
158string ?+?( char c, const string & s );
159string ?+?( const string & s, const string & s2 );
160string ?+?( const char * s, char c ); // not backwards compatible
161string ?+?( char c, const char * s );
162string ?+?( const char * c, const char * s );
163string ?+?( const char * c, const string & s );
164string ?+?( const string & s, const char * c );
165string ?+?( char, char ); // not being called 8-(
166
167static inline string & strcat( string & s, const string & s2 ) { s += s2; return s; }
168static inline string & strcat( string & s, const char * c ) { s += c; return s; }
169static inline string & strncat( string & s, const string & s2, size_t maxlen ) { append( s, s2, maxlen ); return s; }
170static inline string & strncat( string & s, const char * buffer, size_t bsize ) { append( s, buffer, bsize ); return s; }
171
172// Repetition
173void ?*=?( string & s, size_t factor );
174string ?*?( char c, size_t factor ); // not backwards compatible
175string ?*?( const string & s, size_t factor );
176static inline string ?*?( size_t factor, const string & s ) { return s * factor; }
177string ?*?( const char * s, size_t factor );
178static inline string ?*?( size_t factor, const char * s ) { return s * factor; }
179
180// Character access
181char ?[?]( const string & s, size_t index );
182string ?[?]( string & s, size_t index ); // mutable length-1 slice of original
183//char codePointAt(const string & s, size_t index ); // to revisit under Unicode
184
185// Comparisons
186int strcmp ( const string &, const string & );
187bool ?==?( const string &, const string & );
188bool ?!=?( const string &, const string & );
189bool ?>? ( const string &, const string & );
190bool ?>=?( const string &, const string & );
191bool ?<=?( const string &, const string & );
192bool ?<? ( const string &, const string & );
193
194int strcmp( const string &, const char * );
195bool ?==?( const string &, const char * );
196bool ?!=?( const string &, const char * );
197bool ?>? ( const string &, const char * );
198bool ?>=?( const string &, const char * );
199bool ?<=?( const string &, const char * );
200bool ?<? ( const string &, const char * );
201
202int strcmp( const char *, const string & );
203bool ?==?( const char *, const string & );
204bool ?!=?( const char *, const string & );
205bool ?>? ( const char *, const string & );
206bool ?>=?( const char *, const string & );
207bool ?<=?( const char *, const string & );
208bool ?<? ( const char *, const string & );
209
210// String search
211bool contains( const string & s, char ch ); // single character
212
213//int find( const string & s, size_t start, size_t len, const string & key, size_t kstart, size_t klen );
214size_t find$( const string_res & s, size_t start, size_t len, const string & key_res, size_t kstart, size_t klen );
215
216size_t find( const string & s, char key );
217size_t find( const string & s, const char * key );
218size_t find( const string & s, const string & key );
219size_t find( const string & s, const char * key, size_t keysize );
220
221size_t find( const string & s, size_t start, char key );
222size_t find( const string & s, size_t start, const string & key );
223size_t find( const string & s, size_t start, const char * key );
224size_t find( const string & s, size_t start, const char * key, size_t keysize );
225static inline ?^?( const string & key, const string & s ) { return find( s, key ); }
226static inline ?^?( const char * key, const string & s ) { return find( s, key ); }
227
228bool includes( const string & s, const string & mask );
229bool includes( const string & s, const char * mask );
230bool includes( const string & s, const char * mask, size_t masksize );
231
232bool startsWith( const string & s, const string & prefix );
233bool startsWith( const string & s, const char * prefix );
234bool startsWith( const string & s, const char * prefix, size_t prefixsize );
235
236bool endsWith( const string & s, const string & suffix );
237bool endsWith( const string & s, const char * suffix );
238bool endsWith( const string & s, const char * suffix, size_t suffixsize );
239
240// Slicing
241string ?()( string & s, ssize_t start, ssize_t len );
242static inline string ?()( const string & s, ssize_t start, ssize_t len ) { string & w = (string &)s; return w( start, len ); } // FIX ME
243string ?()( string & s, ssize_t start );
244static inline string ?()( const string & s, ssize_t start ) { string & w = (string &)s; return w( start ); } // FIX ME
245static inline string ?()( string & s, char m ) { return s( find( s, m ), 1 )`share; }
246static inline string ?()( const string & s, char m ) { string & w = (string &)s; return w( find( s, m ), 1 )`share; } // FIX ME
247static inline string ?()( string & s, const char * m ) { return s( find( s, m ), len( m ) )`share; }
248static inline string ?()( const string & s, const char * m ) { string & w = (string &)s; return w( find( s, m ), len( m ) )`share; } // FIX ME
249static inline string ?()( string & s, const string & m ) { return s( find( s, m ), len( m ) )`share; }
250static inline string ?()( const string & s, const string & m ) { string & w = (string &)s; return w( find( s, m ), len( m ) )`share; } // FIX ME
251
252struct charclass {
253 charclass_res * inner;
254};
255
256void ?{}( charclass & ) = void;
257void ?{}( charclass &, charclass ) = void;
258charclass ?=?( charclass &, charclass ) = void;
259
260void ?{}( charclass &, const string & chars );
261void ?{}( charclass &, const char * chars );
262void ?{}( charclass &, const char * chars, size_t charssize );
263void ^?{}( charclass & );
264
265size_t include( const string & s, const charclass & mask );
266static inline size_t include( const char * s, const charclass & mask ) { string temp = s; return include( temp, mask ); }
267static inline string include( const string & s, const charclass & mask ) { ssize_t i = include( s, mask ); return s( 0, i )`share; }
268static inline string include( const char * s, const charclass & mask ) { string temp = s; ssize_t i = include( temp, mask ); return temp( 0, i ); }
269
270size_t exclude( const string & s, const charclass & mask );
271static inline size_t exclude( const char * s, const charclass & mask ) { string temp = s; return exclude( temp, mask ); }
272static inline string exclude( const string & s, const charclass & mask ) { ssize_t i = exclude( s, mask ); return s( 0, i )`share; }
273static inline string exclude( const char * s, const charclass & mask ) { string temp = s; ssize_t i = exclude( temp, mask ); return temp( 0, i ); }
274
275size_t test( const string & s, int (*f)( int ) );
276static inline size_t test( const char * c, int (*f)( int ) ) {
277 const string S = c;
278 return test( S, f );
279}
280
281string replace( string & s, const string & from, const string & to );
282static inline string replace( const char * s, const char * from, const char * to ) {
283 string S = s, From = from, To = to;
284 return replace( S, From, To );
285}
286static inline string replace( string & s, const char * from, const char * to ) {
287 string From = from, To = to;
288 return replace( s, From, To );
289}
290static inline string replace( string & s, const char * from, const string & to ) {
291 string From = from;
292 return replace( s, From, to );
293}
294static inline string replace( string & s, string & from, const char * to ) {
295 string To = to;
296 return replace( s, from, To );
297}
298
299string translate( const string & s, int (*f)( int ) );
300static inline string translate( const char * c, int (*f)( int ) ) {
301 const string S = c;
302 return translate( S, f );
303}
Note: See TracBrowser for help on using the repository browser.