source: libcfa/src/collections/string.hfa @ 2c8946b

Last change on this file since 2c8946b was 211def2, checked in by Peter A. Buhr <pabuhr@…>, 9 months ago

omnibus I/O changes to get quoted manipulator to work

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