source: libcfa/src/collections/string.hfa @ 34c6e1e6

Last change on this file since 34c6e1e6 was 34c6e1e6, checked in by Peter A. Buhr <pabuhr@…>, 10 months ago

add string output manipulators, third attempt at input manipulators for strings

  • Property mode set to 100644
File size: 7.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 : 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
118bool ?==?(const string & s, const string & other);
119bool ?!=?(const string & s, const string & other);
120bool ?==?(const string & s, const char * other);
121bool ?!=?(const string & s, const char * other);
122
123// Slicing
124string ?()( string & this, size_t start, size_t end );  // TODO const?
125string ?()( string & this, size_t start);
126
127// String search
128bool contains(const string & s, char ch); // single character
129
130int find(const string & s, char search);
131int find(const string & s, const string & search);
132int find(const string & s, const char * search);
133int find(const string & s, const char * search, size_t searchsize);
134
135int findFrom(const string & s, size_t fromPos, char search);
136int findFrom(const string & s, size_t fromPos, const string & search);
137int findFrom(const string & s, size_t fromPos, const char * search);
138int findFrom(const string & s, size_t fromPos, const char * search, size_t searchsize);
139
140bool includes(const string & s, const string & search);
141bool includes(const string & s, const char * search);
142bool includes(const string & s, const char * search, size_t searchsize);
143
144bool startsWith(const string & s, const string & prefix);
145bool startsWith(const string & s, const char * prefix);
146bool startsWith(const string & s, const char * prefix, size_t prefixsize);
147
148bool endsWith(const string & s, const string & suffix);
149bool endsWith(const string & s, const char * suffix);
150bool endsWith(const string & s, const char * suffix, size_t suffixsize);
151
152// Modifiers
153void padStart(string & s, size_t n);
154void padStart(string & s, size_t n, char padding);
155void padEnd(string & s, size_t n);
156void padEnd(string & s, size_t n, char padding);
157
158
159
160struct charclass {
161    charclass_res * inner;
162};
163
164void ?{}( charclass & ) = void;
165void ?{}( charclass &, charclass) = void;
166charclass ?=?( charclass &, charclass) = void;
167
168void ?{}( charclass &, const string & chars);
169void ?{}( charclass &, const char * chars );
170void ?{}( charclass &, const char * chars, size_t charssize );
171void ^?{}( charclass & );
172
173int include(const string & s, const charclass & mask);
174
175int exclude(const string & s, const charclass & mask);
176
177/*
178What to do with?
179StrRet include(string & s, const charclass & mask);
180StrRet exclude(string & s, const charclass & mask);
181*/
Note: See TracBrowser for help on using the repository browser.