source: libcfa/src/collections/string.hfa@ 96a11655

stuck-waitfor-destruct
Last change on this file since 96a11655 was 6b765d5, checked in by Peter A. Buhr <pabuhr@…>, 11 months ago

add non-backwards compatible string operations

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