source: libcfa/src/containers/string.hfa @ 7e1dbd7

Last change on this file since 7e1dbd7 was 7e1dbd7, checked in by Peter A. Buhr <pabuhr@…>, 9 months ago

first attempt at input manipulators for strings

  • Property mode set to 100644
File size: 6.8 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 : Mon Aug 28 18:32:59 2023
13// Update Count     : 40
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);
31
32// RAII, assignment
33void ?{}(string & this); // empty string
34void ?{}(string & s, const char * initial); // copy from string literal (NULL-terminated)
35void ?{}(string & s, const char * buffer, size_t bsize); // copy specific length from buffer
36
37void ?{}(string & s, const string & s2);
38void ?{}(string & s, string & s2);
39
40void ?=?(string & s, const char * other); // copy assignment from literal
41void ?=?(string & s, const string & other);
42void ?=?(string & s, char other);
43string & ?=?(string & s, string & other);  // surprising ret seems to help avoid calls to autogen
44//string ?=?( string &, string ) = void;
45void ^?{}(string & s);
46
47// Alternate construction: request shared edits
48struct string_WithSharedEdits {
49    string * s;
50};
51string_WithSharedEdits ?`shareEdits( string & this );
52void ?{}( string & this, string_WithSharedEdits src );
53
54// IO Operator
55ofstream & ?|?(ofstream & out, const string & s);
56void ?|?(ofstream & out, const string & s);
57ifstream & ?|?(ifstream & in, string & s);
58void ?|?( ifstream & in, string & this );
59
60
61struct _Istream_str {
62        string & s;
63        union {
64                const char * scanset;
65                char delimit[2];
66        };
67        int rwd;                                                                                        // read width
68        union {
69                unsigned char all;
70                struct {
71                        unsigned char ignore:1;                                         // do not change input argument
72                        unsigned char inex:1;                                           // include/exclude characters in scanset
73                        unsigned char delimit:1;                                        // delimit character
74                        unsigned char rwd:1;                                            // read width
75                } flags;
76        };
77}; // _Istream_str
78
79static inline {
80        // read width does not include null terminator
81        _Istream_str wdi( unsigned int rwd, string & s ) { return (_Istream_str)@{ s, {0p}, rwd, {.flags.rwd : true} }; }
82        _Istream_str skip( const char scanset[] ) { return (_Istream_str)@{ *0p, {scanset}, -1, {.all : 0} }; }
83        _Istream_str skip( unsigned int wd ) { return (_Istream_str)@{ *0p, {0p}, wd, {.all : 0} }; }
84        _Istream_str getline( string & s, const char delimit = '\n' ) {
85                return (_Istream_str)@{ s, {.delimit : { delimit, '\0' } }, -1, {.flags.delimit : true, .flags.inex : true} };
86        }
87        _Istream_str & getline( _Istream_str & fmt, const char delimit = '\n' ) {
88                fmt.delimit[0] = delimit; fmt.delimit[1] = '\0'; fmt.flags.delimit = true; fmt.flags.inex = true; return fmt;
89        }
90        _Istream_str incl( const char scanset[], string & s ) { return (_Istream_str)@{ s, {scanset}, -1, {.flags.inex : false} }; }
91        _Istream_str & incl( const char scanset[], _Istream_str & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; }
92        _Istream_str excl( const char scanset[], string & s ) { return (_Istream_str)@{ s, {scanset}, -1, {.flags.inex : true} }; }
93        _Istream_str & excl( const char scanset[], _Istream_str & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; }
94        _Istream_str ignore( string & s ) { return (_Istream_str)@{ s, {0p}, -1, {.flags.ignore : true} }; }
95        _Istream_str & ignore( _Istream_str & fmt ) { fmt.flags.ignore = true; return fmt; }
96} // distribution
97ifstream & ?|?( ifstream & is, _Istream_str f );
98void ?|?( ifstream & is, _Istream_str t );
99void getline( ifstream & in, string & this, const char delimit = '\n' );
100
101// Concatenation
102void ?+=?(string & s, char other); // append a character
103void ?+=?(string & s, const string & s2); // append-concatenate to first string
104void ?+=?(string & s, const char * other); // append-concatenate to first string
105string ?+?(const string & s, char other); // add a character to a copy of the string
106string ?+?(const string & s, const string & s2); // copy and concatenate both strings
107string ?+?(const char * s1, const char * s2); // concatenate both strings
108string ?+?(const string & s, const char * other); // copy and concatenate with NULL-terminated string
109
110// Repetition
111string ?*?(const string & s, size_t factor);
112string ?*?(char c, size_t size);
113string ?*?(const char *s, size_t size);
114
115// Character access
116char ?[?](const string & s, size_t index);
117string ?[?](string & s, size_t index);  // mutable length-1 slice of original
118//char codePointAt(const string & s, size_t index);  // to revisit under Unicode
119
120// Comparisons
121bool ?==?(const string & s, const string & other);
122bool ?!=?(const string & s, const string & other);
123bool ?==?(const string & s, const char * other);
124bool ?!=?(const string & s, const char * other);
125
126// Slicing
127string ?()( string & this, size_t start, size_t end );  // TODO const?
128string ?()( string & this, size_t start);
129
130// String search
131bool contains(const string & s, char ch); // single character
132
133int find(const string & s, char search);
134int find(const string & s, const string & search);
135int find(const string & s, const char * search);
136int find(const string & s, const char * search, size_t searchsize);
137
138int findFrom(const string & s, size_t fromPos, char search);
139int findFrom(const string & s, size_t fromPos, const string & search);
140int findFrom(const string & s, size_t fromPos, const char * search);
141int findFrom(const string & s, size_t fromPos, const char * search, size_t searchsize);
142
143bool includes(const string & s, const string & search);
144bool includes(const string & s, const char * search);
145bool includes(const string & s, const char * search, size_t searchsize);
146
147bool startsWith(const string & s, const string & prefix);
148bool startsWith(const string & s, const char * prefix);
149bool startsWith(const string & s, const char * prefix, size_t prefixsize);
150
151bool endsWith(const string & s, const string & suffix);
152bool endsWith(const string & s, const char * suffix);
153bool endsWith(const string & s, const char * suffix, size_t suffixsize);
154
155// Modifiers
156void padStart(string & s, size_t n);
157void padStart(string & s, size_t n, char padding);
158void padEnd(string & s, size_t n);
159void padEnd(string & s, size_t n, char padding);
160
161
162
163struct charclass {
164    charclass_res * inner;
165};
166
167void ?{}( charclass & ) = void;
168void ?{}( charclass &, charclass) = void;
169charclass ?=?( charclass &, charclass) = void;
170
171void ?{}( charclass &, const string & chars);
172void ?{}( charclass &, const char * chars );
173void ?{}( charclass &, const char * chars, size_t charssize );
174void ^?{}( charclass & );
175
176int include(const string & s, const charclass & mask);
177
178int exclude(const string & s, const charclass & mask);
179
180/*
181What to do with?
182StrRet include(string & s, const charclass & mask);
183StrRet exclude(string & s, const charclass & mask);
184*/
Note: See TracBrowser for help on using the repository browser.