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

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

formatting, change cmp to strcmp, add strlen and strcat

  • Property mode set to 100644
File size: 8.4 KB
RevLine 
[f450f2f]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
[6264087]11// Last Modified By : Peter A. Buhr
[681e12f]12// Last Modified On : Thu Jan  4 11:27:35 2024
13// Update Count     : 75
[f450f2f]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
[6264087]30size_t size(const string & s);
[681e12f]31static inline size_t strlen(const string & s) { return size( s ); }
[f450f2f]32
33// RAII, assignment
[681e12f]34void ?{}(string & s); // empty string
[6264087]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
[f450f2f]37
[6264087]38void ?{}(string & s, const string & s2);
39void ?{}(string & s, string & s2);
[f450f2f]40
[681e12f]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
[4b3b352]47//string ?=?( string &, string ) = void;
[6264087]48void ^?{}(string & s);
[f450f2f]49
50// Alternate construction: request shared edits
51struct string_WithSharedEdits {
52    string * s;
53};
[681e12f]54string_WithSharedEdits ?`shareEdits( string & s );
55void ?{}( string & s, string_WithSharedEdits src );
[f450f2f]56
57// IO Operator
[6264087]58ofstream & ?|?(ofstream & out, const string & s);
59void ?|?(ofstream & out, const string & s);
60ifstream & ?|?(ifstream & in, string & s);
[681e12f]61void ?|?( ifstream & in, string & s );
[7e1dbd7]62
[34c6e1e6]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 {
[7e1dbd7]78        string & s;
[38de914]79        inline _Istream_str_base;
[34c6e1e6]80}; // _Istream_Sstr
[7e1dbd7]81
82static inline {
83        // read width does not include null terminator
[34c6e1e6]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' ) {
[681e12f]86                return (_Istream_Sstr)@{ s, {{.delimiters : { delimiter, '\0' } }, -1, {.flags.delimiter : true, .flags.inex : true}} };
[7e1dbd7]87        }
[34c6e1e6]88        _Istream_Sstr & getline( _Istream_Sstr & fmt, const char delimiter = '\n' ) {
[681e12f]89                fmt.delimiters[0] = delimiter; fmt.delimiters[1] = '\0'; fmt.flags.delimiter = true; fmt.flags.inex = true; return fmt;
[7e1dbd7]90        }
[34c6e1e6]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; }
[7e1dbd7]97} // distribution
[34c6e1e6]98ifstream & ?|?( ifstream & is, _Istream_Sstr f );
99void ?|?( ifstream & is, _Istream_Sstr t );
[f450f2f]100
101// Concatenation
[681e12f]102void ?+=?(string & s, char c); // append a character
[6264087]103void ?+=?(string & s, const string & s2); // append-concatenate to first string
[681e12f]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
[6264087]108string ?+?(const string & s, const string & s2); // copy and concatenate both strings
109string ?+?(const char * s1, const char * s2); // concatenate both strings
[681e12f]110string ?+?(const string & s, const char * c); // copy and concatenate with NULL-terminated string
[f450f2f]111
112// Repetition
[6264087]113string ?*?(const string & s, size_t factor);
[f450f2f]114string ?*?(char c, size_t size);
115string ?*?(const char *s, size_t size);
116
117// Character access
[6264087]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
[f450f2f]121
122// Comparisons
[681e12f]123int  strcmp (const string &, const string &);
[416b443]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
[681e12f]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 *);
[416b443]138
[681e12f]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 &);
[416b443]146
[f450f2f]147
148// Slicing
[681e12f]149string ?()( string & s, size_t start, size_t end );  // TODO const?
150string ?()( string & s, size_t start);
[f450f2f]151
152// String search
[6264087]153bool contains(const string & s, char ch); // single character
[f450f2f]154
[6264087]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);
[f450f2f]159
[6264087]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);
[08ed947]164
[6264087]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);
[f450f2f]168
[6264087]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);
[f450f2f]172
[6264087]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);
[f450f2f]176
177// Modifiers
[6264087]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);
[f450f2f]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
[6264087]197int include(const string & s, const charclass & mask);
[f450f2f]198
[6264087]199int exclude(const string & s, const charclass & mask);
[f450f2f]200
201/*
202What to do with?
[6264087]203StrRet include(string & s, const charclass & mask);
204StrRet exclude(string & s, const charclass & mask);
[f450f2f]205*/
Note: See TracBrowser for help on using the repository browser.