source: libcfa/src/collections/string.hfa @ 5a1ae14

Last change on this file since 5a1ae14 was 416b443, checked in by Michael Brooks <mlbrooks@…>, 14 months ago

Implement full set of relational operators for strings

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