source: libcfa/src/collections/string.hfa @ 479fbe3

Last change on this file since 479fbe3 was 479fbe3, checked in by Peter A. Buhr <pabuhr@…>, 6 months ago

formatting, add string constructor for char, add string *= operator, simplify ?+? and ?*? operations

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