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

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

test some string operation changes

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