source: libcfa/src/collections/string.hfa@ 234c432

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

Reorder string quasi-lifecycle functions to ensure string dtor called by all static-inline implementations.

This situation is not exercised currently, but will be, upon upcoming string-overload reorganizations. Incumbent tests are adequate to show if a static-inline change misses the custom dtor: have observed "allocated but not freed" failures from test.py.

Doing this code-move ahead of the string-overload reorganizations keeps them more standalone.

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