| [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
|
|---|
| [38de914] | 12 | // Last Modified On : Tue Aug 29 18:28:04 2023
|
|---|
| 13 | // Update Count : 47
|
|---|
| [f450f2f] | 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #pragma once
|
|---|
| 17 |
|
|---|
| 18 | #include <fstream.hfa>
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | // in string_res.hfa
|
|---|
| 22 | struct string_res;
|
|---|
| 23 | struct charclass_res;
|
|---|
| 24 |
|
|---|
| 25 | struct string {
|
|---|
| 26 | string_res * inner;
|
|---|
| 27 | };
|
|---|
| 28 |
|
|---|
| 29 | // Getters
|
|---|
| [6264087] | 30 | size_t size(const string & s);
|
|---|
| [f450f2f] | 31 |
|
|---|
| 32 | // RAII, assignment
|
|---|
| [6264087] | 33 | void ?{}(string & this); // empty string
|
|---|
| 34 | void ?{}(string & s, const char * initial); // copy from string literal (NULL-terminated)
|
|---|
| 35 | void ?{}(string & s, const char * buffer, size_t bsize); // copy specific length from buffer
|
|---|
| [f450f2f] | 36 |
|
|---|
| [6264087] | 37 | void ?{}(string & s, const string & s2);
|
|---|
| 38 | void ?{}(string & s, string & s2);
|
|---|
| [f450f2f] | 39 |
|
|---|
| [6264087] | 40 | void ?=?(string & s, const char * other); // copy assignment from literal
|
|---|
| 41 | void ?=?(string & s, const string & other);
|
|---|
| 42 | void ?=?(string & s, char other);
|
|---|
| 43 | string & ?=?(string & s, string & other); // surprising ret seems to help avoid calls to autogen
|
|---|
| [4b3b352] | 44 | //string ?=?( string &, string ) = void;
|
|---|
| [6264087] | 45 | void ^?{}(string & s);
|
|---|
| [f450f2f] | 46 |
|
|---|
| 47 | // Alternate construction: request shared edits
|
|---|
| 48 | struct string_WithSharedEdits {
|
|---|
| 49 | string * s;
|
|---|
| 50 | };
|
|---|
| 51 | string_WithSharedEdits ?`shareEdits( string & this );
|
|---|
| 52 | void ?{}( string & this, string_WithSharedEdits src );
|
|---|
| 53 |
|
|---|
| 54 | // IO Operator
|
|---|
| [6264087] | 55 | ofstream & ?|?(ofstream & out, const string & s);
|
|---|
| 56 | void ?|?(ofstream & out, const string & s);
|
|---|
| 57 | ifstream & ?|?(ifstream & in, string & s);
|
|---|
| 58 | void ?|?( ifstream & in, string & this );
|
|---|
| [7e1dbd7] | 59 |
|
|---|
| 60 | struct _Istream_str {
|
|---|
| 61 | string & s;
|
|---|
| [38de914] | 62 | inline _Istream_str_base;
|
|---|
| [7e1dbd7] | 63 | }; // _Istream_str
|
|---|
| 64 |
|
|---|
| 65 | static inline {
|
|---|
| 66 | // read width does not include null terminator
|
|---|
| [38de914] | 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}} }; }
|
|---|
| [7e1dbd7] | 70 | _Istream_str getline( string & s, const char delimit = '\n' ) {
|
|---|
| [38de914] | 71 | return (_Istream_str)@{ s, {{.delimit : { delimit, '\0' } }, -1, {.flags.delimit : true, .flags.inex : true}} };
|
|---|
| [7e1dbd7] | 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 | }
|
|---|
| [38de914] | 76 | _Istream_str incl( const char scanset[], string & s ) { return (_Istream_str)@{ s, {{scanset}, -1, {.flags.inex : false}} }; }
|
|---|
| [7e1dbd7] | 77 | _Istream_str & incl( const char scanset[], _Istream_str & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; }
|
|---|
| [38de914] | 78 | _Istream_str excl( const char scanset[], string & s ) { return (_Istream_str)@{ s, {{scanset}, -1, {.flags.inex : true}} }; }
|
|---|
| [7e1dbd7] | 79 | _Istream_str & excl( const char scanset[], _Istream_str & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; }
|
|---|
| [38de914] | 80 | _Istream_str ignore( string & s ) { return (_Istream_str)@{ s, {{0p}, -1, {.flags.ignore : true}} }; }
|
|---|
| [7e1dbd7] | 81 | _Istream_str & ignore( _Istream_str & fmt ) { fmt.flags.ignore = true; return fmt; }
|
|---|
| 82 | } // distribution
|
|---|
| 83 | ifstream & ?|?( ifstream & is, _Istream_str f );
|
|---|
| 84 | void ?|?( ifstream & is, _Istream_str t );
|
|---|
| [88001dd] | 85 | void getline( ifstream & in, string & this, const char delimit = '\n' );
|
|---|
| [f450f2f] | 86 |
|
|---|
| 87 | // Concatenation
|
|---|
| [6264087] | 88 | void ?+=?(string & s, char other); // append a character
|
|---|
| 89 | void ?+=?(string & s, const string & s2); // append-concatenate to first string
|
|---|
| 90 | void ?+=?(string & s, const char * other); // append-concatenate to first string
|
|---|
| 91 | string ?+?(const string & s, char other); // add a character to a copy of the string
|
|---|
| 92 | string ?+?(const string & s, const string & s2); // copy and concatenate both strings
|
|---|
| 93 | string ?+?(const char * s1, const char * s2); // concatenate both strings
|
|---|
| 94 | string ?+?(const string & s, const char * other); // copy and concatenate with NULL-terminated string
|
|---|
| [f450f2f] | 95 |
|
|---|
| 96 | // Repetition
|
|---|
| [6264087] | 97 | string ?*?(const string & s, size_t factor);
|
|---|
| [f450f2f] | 98 | string ?*?(char c, size_t size);
|
|---|
| 99 | string ?*?(const char *s, size_t size);
|
|---|
| 100 |
|
|---|
| 101 | // Character access
|
|---|
| [6264087] | 102 | char ?[?](const string & s, size_t index);
|
|---|
| 103 | string ?[?](string & s, size_t index); // mutable length-1 slice of original
|
|---|
| 104 | //char codePointAt(const string & s, size_t index); // to revisit under Unicode
|
|---|
| [f450f2f] | 105 |
|
|---|
| 106 | // Comparisons
|
|---|
| [6264087] | 107 | bool ?==?(const string & s, const string & other);
|
|---|
| 108 | bool ?!=?(const string & s, const string & other);
|
|---|
| 109 | bool ?==?(const string & s, const char * other);
|
|---|
| 110 | bool ?!=?(const string & s, const char * other);
|
|---|
| [f450f2f] | 111 |
|
|---|
| 112 | // Slicing
|
|---|
| 113 | string ?()( string & this, size_t start, size_t end ); // TODO const?
|
|---|
| 114 | string ?()( string & this, size_t start);
|
|---|
| 115 |
|
|---|
| 116 | // String search
|
|---|
| [6264087] | 117 | bool contains(const string & s, char ch); // single character
|
|---|
| [f450f2f] | 118 |
|
|---|
| [6264087] | 119 | int find(const string & s, char search);
|
|---|
| 120 | int find(const string & s, const string & search);
|
|---|
| 121 | int find(const string & s, const char * search);
|
|---|
| 122 | int find(const string & s, const char * search, size_t searchsize);
|
|---|
| [f450f2f] | 123 |
|
|---|
| [6264087] | 124 | int findFrom(const string & s, size_t fromPos, char search);
|
|---|
| 125 | int findFrom(const string & s, size_t fromPos, const string & search);
|
|---|
| 126 | int findFrom(const string & s, size_t fromPos, const char * search);
|
|---|
| 127 | int findFrom(const string & s, size_t fromPos, const char * search, size_t searchsize);
|
|---|
| [08ed947] | 128 |
|
|---|
| [6264087] | 129 | bool includes(const string & s, const string & search);
|
|---|
| 130 | bool includes(const string & s, const char * search);
|
|---|
| 131 | bool includes(const string & s, const char * search, size_t searchsize);
|
|---|
| [f450f2f] | 132 |
|
|---|
| [6264087] | 133 | bool startsWith(const string & s, const string & prefix);
|
|---|
| 134 | bool startsWith(const string & s, const char * prefix);
|
|---|
| 135 | bool startsWith(const string & s, const char * prefix, size_t prefixsize);
|
|---|
| [f450f2f] | 136 |
|
|---|
| [6264087] | 137 | bool endsWith(const string & s, const string & suffix);
|
|---|
| 138 | bool endsWith(const string & s, const char * suffix);
|
|---|
| 139 | bool endsWith(const string & s, const char * suffix, size_t suffixsize);
|
|---|
| [f450f2f] | 140 |
|
|---|
| 141 | // Modifiers
|
|---|
| [6264087] | 142 | void padStart(string & s, size_t n);
|
|---|
| 143 | void padStart(string & s, size_t n, char padding);
|
|---|
| 144 | void padEnd(string & s, size_t n);
|
|---|
| 145 | void padEnd(string & s, size_t n, char padding);
|
|---|
| [f450f2f] | 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 | struct charclass {
|
|---|
| 150 | charclass_res * inner;
|
|---|
| 151 | };
|
|---|
| 152 |
|
|---|
| 153 | void ?{}( charclass & ) = void;
|
|---|
| 154 | void ?{}( charclass &, charclass) = void;
|
|---|
| 155 | charclass ?=?( charclass &, charclass) = void;
|
|---|
| 156 |
|
|---|
| 157 | void ?{}( charclass &, const string & chars);
|
|---|
| 158 | void ?{}( charclass &, const char * chars );
|
|---|
| 159 | void ?{}( charclass &, const char * chars, size_t charssize );
|
|---|
| 160 | void ^?{}( charclass & );
|
|---|
| 161 |
|
|---|
| [6264087] | 162 | int include(const string & s, const charclass & mask);
|
|---|
| [f450f2f] | 163 |
|
|---|
| [6264087] | 164 | int exclude(const string & s, const charclass & mask);
|
|---|
| [f450f2f] | 165 |
|
|---|
| 166 | /*
|
|---|
| 167 | What to do with?
|
|---|
| [6264087] | 168 | StrRet include(string & s, const charclass & mask);
|
|---|
| 169 | StrRet exclude(string & s, const charclass & mask);
|
|---|
| [f450f2f] | 170 | */
|
|---|