source: libcfa/src/collections/string.hfa@ b14d0d97

Last change on this file since b14d0d97 was 829a955, checked in by Peter A. Buhr <pabuhr@…>, 2 months ago

update strings, update for-control and string documentation

  • Property mode set to 100644
File size: 19.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 : Sun Sep 14 10:58:28 2025
13// Update Count : 311
14//
15
16#pragma once
17
18#include <iostream.hfa>
19#include <string_res.hfa>
20
21static struct __cfa_string_preference_boost_t {} __cfa_string_preference_boost;
22#define PBOOST forall ( | { __cfa_string_preference_boost_t __cfa_string_preference_boost; } )
23
24struct string {
25 string_res * inner;
26};
27
28// RAII, assignment
29void ^?{}( string & s );
30
31void ?{}( string & s ); // empty string
32void ?{}( string & s, string s2, size_t maxlen );
33PBOOST void ?{}( string & s, string s2 );
34void ?{}( string & s, char );
35void ?{}( string & s, const char * c ); // copy from string literal (NULL-terminated)
36void ?{}( string & s, const char * c, size_t size ); // copy specific length from buffer
37
38void ?{}( string & s, signed long int rhs );
39void ?{}( string & s, size_t rhs );
40void ?{}( string & s, double rhs );
41void ?{}( string & s, long double rhs );
42void ?{}( string & s, double _Complex rhs );
43void ?{}( string & s, long double _Complex rhs );
44static inline void ?{}( string & s, int rhs ) { (s){(signed long int) rhs}; }
45
46PBOOST string & ?=?( string & s, string c );
47string & ?=?( string & s, const char * c ); // copy from "literal"
48string & ?=?( string & s, char c ); // copy from 'l'
49string & assign( string & s, const string & c, size_t n );
50string & assign( string & s, const char * c, size_t n );
51string & ?=?( string & s, signed long int rhs );
52string & ?=?( string & s, size_t rhs );
53string & ?=?( string & s, double rhs );
54string & ?=?( string & s, long double rhs );
55string & ?=?( string & s, double _Complex rhs );
56string & ?=?( string & s, long double _Complex rhs );
57static inline string & ?=?( string & s, int rhs ) { return s = ((signed long int) rhs); } // to match cost of (char * int): int
58
59static inline string & strcpy( string & s, const char * c ) { s = c; return s; }
60static inline string & strncpy( string & s, const char * c, size_t n ) { assign( s, c, n ); return s; }
61static inline string & strcpy( string & s, const string & c ) { s = c; return s; }
62static inline string & strncpy( string & s, const string & c, size_t n ) { assign( s, c, n ); return s; }
63char * strncpy( char * dst, string & src, size_t n );
64char * ?=?( char *& dst, string & src );
65void ?{}( char *& dst, string & src );
66
67// Alternate construction: request shared edits
68struct string_Share {
69 string * s;
70};
71string_Share ?`share( string & s );
72void ?{}( string & s, string_Share src );
73
74// Getters
75static inline size_t len( const string & s ) { return len( *s.inner ); }
76static inline size_t len( const char * cs ) { return strlen( cs ); };
77static inline size_t strlen( const string & s ) { return len( s ); }
78size_t strnlen( const string & s, size_t maxlen );
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
99
100forall( ostype & | basic_ostream( ostype ) ) {
101 ostype & ?|?( ostype & os, _Ostream_Manip(string) f );
102 void ?|?( ostype & os, _Ostream_Manip(string) );
103}
104
105struct _Istream_Swidth {
106 string & s;
107 inline _Istream_str_base;
108}; // _Istream_Swidth
109
110struct _Istream_Squote {
111 _Istream_Swidth sstr;
112}; // _Istream_Squote
113
114struct _Istream_Sstr {
115 string & s;
116 inline _Istream_str_base;
117// _Istream_Swidth sstr;
118}; // _Istream_Sstr
119
120static inline {
121 // read width does not include null terminator
122 _Istream_Swidth wdi( unsigned int rwd, string & s ) { return (_Istream_Swidth)@{ .s = s, { {.scanset = 0p}, .wd = rwd, {.flags.rwd = true} } }; }
123 _Istream_Sstr getline( string & s, const char delimiter = '\n' ) {
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_Squote quote( string & s, const char Ldelimiter = '\"', const char Rdelimiter = '\0' ) {
130 return (_Istream_Squote)@{ { .s = s, { {.delimiters = { Ldelimiter, Rdelimiter, '\0' }}, .wd = -1, {.flags.rwd = true} } } };
131 }
132 _Istream_Squote & quote( _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_Squote &)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[], _Istream_Swidth & f ) { f.scanset = scanset; f.flags.inex = false; return (_Istream_Sstr &)f; }
138 _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ .s = s, { {.scanset = scanset}, .wd = -1, {.flags.inex = true} } }; }
139 _Istream_Sstr & excl( const char scanset[], _Istream_Swidth & f ) { f.scanset = scanset; f.flags.inex = true; return (_Istream_Sstr &)f; }
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_Squote & ignore( _Istream_Squote & f ) { f.sstr.flags.ignore = true; return (_Istream_Squote &)f; }
143 _Istream_Sstr & ignore( _Istream_Sstr & f ) { f.flags.ignore = true; return (_Istream_Sstr &)f; }
144} // distribution
145
146forall( istype & | basic_istream( istype ) ) {
147 istype & ?|?( istype & is, _Istream_Squote f );
148 istype & ?|?( istype & is, _Istream_Sstr f );
149 static inline istype & ?|?( istype & is, _Istream_Swidth f ) { return is | *(_Istream_Sstr *)&f; }
150}
151
152// Concatenation
153void ?+=?( string & s, char c );
154PBOOST void ?+=?( string & s, string );
155void append( string & s, const string & s2, size_t maxlen );
156void ?+=?( string & s, const char * s2 );
157void append( string & s, const char * buffer, size_t bsize );
158
159string ?+?( string s, char c );
160string ?+?( char c, string s );
161PBOOST string ?+?( string s, string s2 );
162string ?+?( const char * s, char c ); // not backwards compatible
163string ?+?( char c, const char * s );
164string ?+?( const char * c, const char * s );
165string ?+?( const char * c, string s );
166string ?+?( string s, const char * c );
167string ?+?( char, char ); // not being called 8-(
168
169static inline string & strcat( string & s, const string & s2 ) { s += s2; return s; }
170static inline string & strcat( string & s, const char * c ) { s += c; return s; }
171static inline string & strncat( string & s, const string & s2, size_t maxlen ) { append( s, s2, maxlen ); return s; }
172static inline string & strncat( string & s, const char * buffer, size_t bsize ) { append( s, buffer, bsize ); return s; }
173
174// Repetition
175
176// Type `signed long long int` chosen for `factor` argument to achieve cost detente.
177// This way, the call `'a' * 3` gets the same safe conversion cost calling here as for
178// the built-in definition `int * int`.
179typedef signed long long int strmul_factor_t;
180
181void ?*=?( string & s, strmul_factor_t factor );
182string ?*?( char c, strmul_factor_t factor ); // not backwards compatible
183PBOOST string ?*?( string s, strmul_factor_t factor );
184string ?*?( const char * s, strmul_factor_t factor );
185static inline string ?*?( strmul_factor_t factor, char c ) { return c * factor; }
186PBOOST static inline string ?*?( strmul_factor_t factor, string s ) { return s * factor; }
187static inline string ?*?( strmul_factor_t factor, const char * s ) { return s * factor; }
188
189// Character access
190char ?[?]( const string & s, size_t index );
191string ?[?]( string & s, size_t index ); // mutable length-1 slice of original
192//char codePointAt(const string & s, size_t index ); // to revisit under Unicode
193
194// Comparisons
195static inline int strcmp( const string & s1, const string & s2 ) { return strcmp( *s1.inner, *s2.inner ); }
196int strncmp( const string & s1, const string & s2, size_t maxlen );
197static inline bool ?==?( const string & s1, const string & s2 ) { return *s1.inner == *s2.inner; }
198static inline bool ?!=?( const string & s1, const string & s2 ) { return *s1.inner != *s2.inner; }
199static inline bool ?>? ( const string & s1, const string & s2 ) { return *s1.inner > *s2.inner; }
200static inline bool ?>=?( const string & s1, const string & s2 ) { return *s1.inner >= *s2.inner; }
201static inline bool ?<=?( const string & s1, const string & s2 ) { return *s1.inner <= *s2.inner; }
202static inline bool ?<? ( const string & s1, const string & s2 ) { return *s1.inner < *s2.inner; }
203
204static inline int strcmp( const string & s1, const char * s2 ) { return strcmp( *s1.inner, s2 ); }
205int strncmp( const string & s1, const char * s2, size_t maxlen );
206static inline bool ?==?( const string & s1, const char * s2 ) { return *s1.inner == s2; }
207static inline bool ?!=?( const string & s1, const char * s2 ) { return *s1.inner != s2; }
208static inline bool ?>? ( const string & s1, const char * s2 ) { return *s1.inner > s2; }
209static inline bool ?>=?( const string & s1, const char * s2 ) { return *s1.inner >= s2; }
210static inline bool ?<=?( const string & s1, const char * s2 ) { return *s1.inner <= s2; }
211static inline bool ?<? ( const string & s1, const char * s2 ) { return *s1.inner < s2; }
212
213static inline int strcmp( const char * s1, const string & s2 ) { return strcmp( s1, *s2.inner ); }
214int strncmp( const char * s1, const string & s2, size_t maxlen );
215static inline bool ?==?( const char * s1, const string & s2 ) { return s1 == *s2.inner; }
216static inline bool ?!=?( const char * s1, const string & s2 ) { return s1 != *s2.inner; }
217static inline bool ?>? ( const char * s1, const string & s2 ) { return s1 > *s2.inner; }
218static inline bool ?>=?( const char * s1, const string & s2 ) { return s1 >= *s2.inner; }
219static inline bool ?<=?( const char * s1, const string & s2 ) { return s1 <= *s2.inner; }
220static inline bool ?<? ( const char * s1, const string & s2 ) { return s1 < *s2.inner; }
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 );
237
238bool includes( const string & s, const string & mask );
239bool includes( const string & s, const char * mask );
240bool includes( const string & s, const char * mask, size_t masksize );
241
242bool startsWith( const string & s, const string & prefix );
243bool startsWith( const string & s, const char * prefix );
244bool startsWith( const string & s, const char * prefix, size_t prefixsize );
245
246bool endsWith( const string & s, const string & suffix );
247bool endsWith( const string & s, const char * suffix );
248bool endsWith( const string & s, const char * suffix, size_t suffixsize );
249
250// Slicing
251string ?()( string & s, ssize_t start, ssize_t len );
252static inline string ?()( const string & s, ssize_t start, ssize_t len ) { string & w = (string &)s; return w( start, len ); } // FIX ME
253string ?()( string & s, ssize_t start );
254static inline string ?()( const string & s, ssize_t start ) { string & w = (string &)s; return w( start ); } // FIX ME
255static inline string ?()( string & s, char m ) { return s( find( s, m ), 1 )`share; }
256static inline string ?()( const string & s, char m ) { string & w = (string &)s; return w( find( s, m ), 1 )`share; } // FIX ME
257static inline string ?()( string & s, const char * m ) { return s( find( s, m ), len( m ) )`share; }
258static inline string ?()( const string & s, const char * m ) { string & w = (string &)s; return w( find( s, m ), len( m ) )`share; } // FIX ME
259static inline string ?()( string & s, const string & m ) { return s( find( s, m ), len( m ) )`share; }
260static inline string ?()( const string & s, const string & m ) { string & w = (string &)s; return w( find( s, m ), len( m ) )`share; } // FIX ME
261
262struct charclass {
263 charclass_res * inner;
264};
265
266void ?{}( charclass & ) = void;
267void ?{}( charclass &, charclass ) = void;
268charclass ?=?( charclass &, charclass ) = void;
269
270void ?{}( charclass &, const string & chars );
271void ?{}( charclass &, const char * chars );
272void ?{}( charclass &, const char * chars, size_t charssize );
273void ^?{}( charclass & );
274
275size_t include( const string & s, const charclass & mask );
276static inline size_t include( const string & s, const char * mask ) { return include( s, (charclass){ mask } ); }
277static inline size_t include( const string & s, const string & mask ) { return include( s, (charclass){ mask } ); }
278static inline size_t include( const char * cs, const charclass & mask ) { return include( (string){ cs }, mask ); }
279static inline size_t include( const char * cs, const char * mask ) { return include( (string){ cs }, (charclass){ mask } ); }
280static inline size_t include( const char * cs, const string & mask ) { return include( (string){ cs }, (charclass){ mask } ); }
281
282static inline string include( const string & s, const charclass & mask ) { return s( 0, include( s, mask ) ); }
283static inline string include( const string & s, const char * mask ) { return s( 0, include( s, (charclass){ mask } ) ); }
284static inline string include( const string & s, const string & mask ) { return s( 0, include( s, (charclass){ mask } ) ); }
285static inline string include( const char * cs, const charclass & mask ) { const string s = cs; return s( 0, include( s, mask ) ); }
286static inline string include( const char * cs, const char * mask ) { const string s = cs; return s( 0, include( s, (charclass){ mask } ) ); }
287static inline string include( const char * cs, const string & mask ) { const string s = cs; return s( 0, include( s, (charclass){ mask } ) ); }
288
289size_t exclude( const string & s, const charclass & mask );
290static inline size_t exclude( const string & s, const char * mask ) { return exclude( s, (charclass){ mask } ); }
291static inline size_t exclude( const string & s, const string & mask ) { return exclude( s, (charclass){ mask } ); }
292static inline size_t exclude( const char * cs, const charclass & mask ) { return exclude( (string){ cs }, mask ); }
293static inline size_t exclude( const char * cs, const string & mask ) { return exclude( (string){ cs }, (charclass){ mask } ); }
294static inline size_t exclude( const char * cs, const char * mask ) { return exclude( (string){ cs }, (charclass){ mask } ); }
295
296static inline string exclude( const string & s, const charclass & mask ) { return s( 0, exclude( s, mask ) ); }
297static inline string exclude( const string & s, const char * mask ) { return s( 0, exclude( s, (charclass){ mask } ) ); }
298static inline string exclude( const string & s, const string & mask ) { return s( 0, exclude( s, (charclass){ mask } ) ); }
299static inline string exclude( const char * cs, const charclass & mask ) { const string s = cs; return s( 0, exclude( s, mask ) ); }
300static inline string exclude( const char * cs, const string & mask ) { const string s = cs; return s( 0, exclude( s, (charclass){ mask } ) ); }
301static inline string exclude( const char * cs, const char * mask ) { const string s = cs; return s( 0, exclude( s, (charclass){ mask } ) ); }
302
303size_t include( const string & s, int (* f)( int ) ); // for C character-class functions, e.g., isdigit
304static inline size_t include( const char * cs, int (* f)( int ) ) { return include( (string){ cs }, f ); }
305static inline string include( const string & s, int (* f)( int ) ) { return s( 0, include( s, f ) ); }
306static inline string include( const char * cs, int (* f)( int ) ) { const string s = cs; return s( 0, include( s, f ) ); }
307
308static inline size_t include( const string & s, bool (* f)( char ) ) { return include( s, (int (*)( int ))f ); }
309static inline size_t include( const char * cs, bool (* f)( char ) ) { return include( (string){ cs }, f ); }
310static inline string include( const string & s, bool (* f)( char ) ) { return s( 0, include( s, f ) ); }
311static inline string include( const char * cs, bool (* f)( char ) ) { const string s = cs; return s( 0, include( s, f ) ); }
312
313size_t exclude( const string & s, int (* f)( int ) ); // for C character-class functions, e.g., isdigit
314static inline size_t exclude( const char * cs, int (* f)( int ) ) { return exclude( (string){ cs }, f ); }
315static inline string exclude( const string & s, int (* f)( int ) ) { return s( 0, exclude( s, f ) ); }
316static inline string exclude( const char * cs, int (* f)( int ) ) { const string s = cs; return s( 0, exclude( s, f ) ); }
317
318static inline size_t exclude( const string & s, bool (* f)( char ) ) { return exclude( s, (int (*)( int ))f ); }
319static inline size_t exclude( const char * cs, bool (* f)( char ) ) { return exclude( (string){ cs }, f ); }
320static inline string exclude( const string & s, bool (* f)( char ) ) { return s( 0, exclude( s, f ) ); }
321static inline string exclude( const char * cs, bool (* f)( char ) ) { const string s = cs; return s( 0, exclude( s, f ) ); }
322
323string replace( const string & s, const string & from, const string & to );
324static inline string replace( const char * s, const char * from, const char * to ) { return replace( (string){ s }, (string){ from }, (string){ to } ); }
325static inline string replace( const string & s, const char * from, const char * to ) { return replace( s, (string){ from }, (string){ to } ); }
326static inline string replace( const string & s, const char * from, const string & to ) { return replace( s, (string){ from }, to ); }
327static inline string replace( const string & s, string & from, const char * to ) { return replace( s, from, (string){ to } ); }
328
329string translate( const string & s, int (* f)( int ) ); // for C character-class functions, e.g., isdigit
330static inline string translate( const char * cs, int (* f)( int ) ) { return translate( (string){ cs }, f ); }
331
332static inline string translate( const string & s, bool (* f)( char ) ) { return translate( s, (int (*)( int ))f ); }
333static inline string translate( const char * cs, bool (* f)( char ) ) { return translate( (string){ cs }, f ); }
334
335#ifndef _COMPILING_STRING_CFA_
336#undef PBOOST
337#endif
Note: See TracBrowser for help on using the repository browser.