source: libcfa/src/collections/string.hfa @ 8ca60e4

Last change on this file since 8ca60e4 was f2898df, checked in by Michael Brooks <mlbrooks@…>, 10 months ago

Implement string initialization and assignment from various numeric types

  • Property mode set to 100644
File size: 9.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 : 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, 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);
82void ?|?( ifstream & in, string & s );
83
84static inline {
85        _Ostream_Manip(string) bin( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'b', { .all : 0 } }; }
86        _Ostream_Manip(string) oct( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'o', { .all : 0 } }; }
87        _Ostream_Manip(string) hex( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'x', { .all : 0 } }; }
88        _Ostream_Manip(string) wd( unsigned int w, string s ) { return (_Ostream_Manip(string))@{ s, w, 0, 's', { .all : 0 } }; }
89        _Ostream_Manip(string) wd( unsigned int w, unsigned int pc, string s ) { return (_Ostream_Manip(string))@{ s, w, pc, 's', { .flags.pc : true } }; }
90        _Ostream_Manip(string) & wd( unsigned int w, _Ostream_Manip(string) & fmt ) { fmt.wd = w; return fmt; }
91        _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; }
92        _Ostream_Manip(string) & left( _Ostream_Manip(string) & fmt ) { fmt.flags.left = true; return fmt; }
93        _Ostream_Manip(string) & nobase( _Ostream_Manip(string) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
94} // distribution
95ofstream & ?|?( ofstream & os, _Ostream_Manip(string) f );
96void ?|?( ofstream & os, _Ostream_Manip(string) );
97
98struct _Istream_Sstr {
99        string & s;
100        inline _Istream_str_base;
101}; // _Istream_Sstr
102
103static inline {
104        // read width does not include null terminator
105        _Istream_Sstr wdi( unsigned int rwd, string & s ) { return (_Istream_Sstr)@{ s, {{0p}, rwd, {.flags.rwd : true}} }; }
106        _Istream_Sstr getline( string & s, const char delimiter = '\n' ) {
107                return (_Istream_Sstr)@{ s, {{.delimiters : { delimiter, '\0' } }, -1, {.flags.delimiter : true, .flags.inex : true}} };
108        }
109        _Istream_Sstr & getline( _Istream_Sstr & fmt, const char delimiter = '\n' ) {
110                fmt.delimiters[0] = delimiter; fmt.delimiters[1] = '\0'; fmt.flags.delimiter = true; fmt.flags.inex = true; return fmt;
111        }
112        _Istream_Sstr incl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ s, {{scanset}, -1, {.flags.inex : false}} }; }
113        _Istream_Sstr & incl( const char scanset[], _Istream_Sstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; }
114        _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ s, {{scanset}, -1, {.flags.inex : true}} }; }
115        _Istream_Sstr & excl( const char scanset[], _Istream_Sstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; }
116        _Istream_Sstr ignore( string & s ) { return (_Istream_Sstr)@{ s, {{0p}, -1, {.flags.ignore : true}} }; }
117        _Istream_Sstr & ignore( _Istream_Sstr & fmt ) { fmt.flags.ignore = true; return fmt; }
118} // distribution
119ifstream & ?|?( ifstream & is, _Istream_Sstr f );
120void ?|?( ifstream & is, _Istream_Sstr t );
121
122// Concatenation
123void ?+=?(string & s, char c); // append a character
124void ?+=?(string & s, const string & s2); // append-concatenate to first string
125void append(string & s, const string & s2, size_t maxlen);  // append-concatenate to first string, up to maxlen
126void ?+=?(string & s, const char * s2); // append-concatenate NULL-terminated string to first string
127void append(string & s, const char * buffer, size_t bsize);  // append-concatenate given range to first string
128
129string ?+?(const string & s, char c); // add a character to a copy of the string
130string ?+?(const string & s, const string & s2); // copy and concatenate both strings
131string ?+?(const char * s1, const char * s2); // copy and concatenate both strings
132string ?+?(const string & s, const char * c); // copy and concatenate with NULL-terminated string
133
134static inline string & strcat(string & s, const string & s2) { s += s2; return s; }
135static inline string & strcat(string & s, const char * c) { s += c; return s; }
136static inline string & strncat(string & s, const string & s2, size_t maxlen) { append(s, s2, maxlen); return s; }
137static inline string & strncat(string & s, const char * buffer, size_t bsize) { append(s, buffer, bsize); return s; }
138
139// Repetition
140string ?*?(const string & s, size_t factor);
141void ?*=?(string & s, size_t factor);
142string ?*?(char c, size_t factor);
143string ?*?(const char *s, size_t factor);
144
145// Character access
146char ?[?](const string & s, size_t index);
147string ?[?](string & s, size_t index);  // mutable length-1 slice of original
148//char codePointAt(const string & s, size_t index);  // to revisit under Unicode
149
150// Comparisons
151int  strcmp (const string &, const string &);
152bool ?==?(const string &, const string &);
153bool ?!=?(const string &, const string &);
154bool ?>? (const string &, const string &);
155bool ?>=?(const string &, const string &);
156bool ?<=?(const string &, const string &);
157bool ?<? (const string &, const string &);
158
159int  strcmp (const string &, const char *);
160bool ?==?(const string &, const char *);
161bool ?!=?(const string &, const char *);
162bool ?>? (const string &, const char *);
163bool ?>=?(const string &, const char *);
164bool ?<=?(const string &, const char *);
165bool ?<? (const string &, const char *);
166
167int  strcmp (const char *, const string &);
168bool ?==?(const char *, const string &);
169bool ?!=?(const char *, const string &);
170bool ?>? (const char *, const string &);
171bool ?>=?(const char *, const string &);
172bool ?<=?(const char *, const string &);
173bool ?<? (const char *, const string &);
174
175
176// Slicing
177string ?()( string & s, size_t start, size_t len );  // TODO const?
178string ?()( string & s, size_t start);
179
180// String search
181bool contains(const string & s, char ch); // single character
182
183int find(const string & s, char search);
184int find(const string & s, const string & search);
185int find(const string & s, const char * search);
186int find(const string & s, const char * search, size_t searchsize);
187
188int findFrom(const string & s, size_t fromPos, char search);
189int findFrom(const string & s, size_t fromPos, const string & search);
190int findFrom(const string & s, size_t fromPos, const char * search);
191int findFrom(const string & s, size_t fromPos, const char * search, size_t searchsize);
192
193bool includes(const string & s, const string & search);
194bool includes(const string & s, const char * search);
195bool includes(const string & s, const char * search, size_t searchsize);
196
197bool startsWith(const string & s, const string & prefix);
198bool startsWith(const string & s, const char * prefix);
199bool startsWith(const string & s, const char * prefix, size_t prefixsize);
200
201bool endsWith(const string & s, const string & suffix);
202bool endsWith(const string & s, const char * suffix);
203bool endsWith(const string & s, const char * suffix, size_t suffixsize);
204
205// Modifiers
206void padStart(string & s, size_t n);
207void padStart(string & s, size_t n, char padding);
208void padEnd(string & s, size_t n);
209void padEnd(string & s, size_t n, char padding);
210
211
212struct charclass {
213    charclass_res * inner;
214};
215
216void ?{}( charclass & ) = void;
217void ?{}( charclass &, charclass) = void;
218charclass ?=?( charclass &, charclass) = void;
219
220void ?{}( charclass &, const string & chars);
221void ?{}( charclass &, const char * chars );
222void ?{}( charclass &, const char * chars, size_t charssize );
223void ^?{}( charclass & );
224
225int include(const string & s, const charclass & mask);
226
227int exclude(const string & s, const charclass & mask);
228
229/*
230What to do with?
231StrRet include(string & s, const charclass & mask);
232StrRet exclude(string & s, const charclass & mask);
233*/
Note: See TracBrowser for help on using the repository browser.