source: libcfa/src/containers/string.hfa@ 38de914

Last change on this file since 38de914 was 38de914, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

second attempt at input manipulators for strings

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