source: libcfa/src/collections/string.hfa @ 681e12f

Last change on this file since 681e12f was 681e12f, checked in by Peter A. Buhr <pabuhr@…>, 5 months ago

formatting, change cmp to strcmp, add strlen and strcat

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