source: libcfa/src/collections/string.hfa @ b1eefe50

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

Adjust string assignment declarations for consistent/standard return type.

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