source: libcfa/src/collections/string.hfa @ 544deb9

Last change on this file since 544deb9 was e8b3717, checked in by Michael Brooks <mlbrooks@…>, 5 months ago

Modify substring interface from start-end to start-len, and add a missing test.

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