source: libcfa/src/collections/string.hfa@ 570e7ad

Last change on this file since 570e7ad was 570e7ad, checked in by Michael Brooks <mlbrooks@…>, 6 months ago

Make string operator-overload costs match their intuitively equivalent arithmetics.

Replace many by-reference string args with by-value args to work around noise from the reference-cost column.

Use a special arithmetic type for the factor argument of ?*? to match conversion cost of (char*int=int).

Removes cost-function noise of char-arithmetic operators being preferred over their string-concatenation equivalents in the reference-cost column.

Notably, all former Spanish-A and numeric outputs have become ambiguous or been associated with a reproducible bug.

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