// // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // string -- variable-length, mutable run of text, with value semantics // // Author : Michael L. Brooks // Created On : Fri Sep 03 11:00:00 2021 // Last Modified By : Peter A. Buhr // Last Modified On : Thu Jan 4 11:27:35 2024 // Update Count : 75 // #pragma once #include // in string_res.hfa struct string_res; struct charclass_res; struct string { string_res * inner; }; // Getters size_t size(const string & s); static inline size_t strlen(const string & s) { return size( s ); } // RAII, assignment void ?{}(string & s); // empty string void ?{}(string & s, const char * initial); // copy from string literal (NULL-terminated) void ?{}(string & s, const char * buffer, size_t bsize); // copy specific length from buffer void ?{}(string & s, const string & s2); void ?{}(string & s, string & s2); void ?=?(string & s, const char * c); // copy assignment from literal static inline string & strcpy(string & s, const char * c) { s = c; return s; } void ?=?(string & s, const string & c); static inline string & strcpy(string & s, const string c) { s = c; return s; } void ?=?(string & s, char c); string & ?=?(string & s, string & c); // surprising ret seems to help avoid calls to autogen //string ?=?( string &, string ) = void; void ^?{}(string & s); // Alternate construction: request shared edits struct string_WithSharedEdits { string * s; }; string_WithSharedEdits ?`shareEdits( string & s ); void ?{}( string & s, string_WithSharedEdits src ); // IO Operator ofstream & ?|?(ofstream & out, const string & s); void ?|?(ofstream & out, const string & s); ifstream & ?|?(ifstream & in, string & s); void ?|?( ifstream & in, string & s ); static inline { _Ostream_Manip(string) bin( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'b', { .all : 0 } }; } _Ostream_Manip(string) oct( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'o', { .all : 0 } }; } _Ostream_Manip(string) hex( string s ) { return (_Ostream_Manip(string))@{ s, 1, 0, 'x', { .all : 0 } }; } _Ostream_Manip(string) wd( unsigned int w, string s ) { return (_Ostream_Manip(string))@{ s, w, 0, 's', { .all : 0 } }; } _Ostream_Manip(string) wd( unsigned int w, unsigned int pc, string s ) { return (_Ostream_Manip(string))@{ s, w, pc, 's', { .flags.pc : true } }; } _Ostream_Manip(string) & wd( unsigned int w, _Ostream_Manip(string) & fmt ) { fmt.wd = w; return fmt; } _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; } _Ostream_Manip(string) & left( _Ostream_Manip(string) & fmt ) { fmt.flags.left = true; return fmt; } _Ostream_Manip(string) & nobase( _Ostream_Manip(string) & fmt ) { fmt.flags.nobsdp = true; return fmt; } } // distribution ofstream & ?|?( ofstream & os, _Ostream_Manip(string) f ); void ?|?( ofstream & os, _Ostream_Manip(string) ); struct _Istream_Sstr { string & s; inline _Istream_str_base; }; // _Istream_Sstr static inline { // read width does not include null terminator _Istream_Sstr wdi( unsigned int rwd, string & s ) { return (_Istream_Sstr)@{ s, {{0p}, rwd, {.flags.rwd : true}} }; } _Istream_Sstr getline( string & s, const char delimiter = '\n' ) { return (_Istream_Sstr)@{ s, {{.delimiters : { delimiter, '\0' } }, -1, {.flags.delimiter : true, .flags.inex : true}} }; } _Istream_Sstr & getline( _Istream_Sstr & fmt, const char delimiter = '\n' ) { fmt.delimiters[0] = delimiter; fmt.delimiters[1] = '\0'; fmt.flags.delimiter = true; fmt.flags.inex = true; return fmt; } _Istream_Sstr incl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ s, {{scanset}, -1, {.flags.inex : false}} }; } _Istream_Sstr & incl( const char scanset[], _Istream_Sstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = false; return fmt; } _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ s, {{scanset}, -1, {.flags.inex : true}} }; } _Istream_Sstr & excl( const char scanset[], _Istream_Sstr & fmt ) { fmt.scanset = scanset; fmt.flags.inex = true; return fmt; } _Istream_Sstr ignore( string & s ) { return (_Istream_Sstr)@{ s, {{0p}, -1, {.flags.ignore : true}} }; } _Istream_Sstr & ignore( _Istream_Sstr & fmt ) { fmt.flags.ignore = true; return fmt; } } // distribution ifstream & ?|?( ifstream & is, _Istream_Sstr f ); void ?|?( ifstream & is, _Istream_Sstr t ); // Concatenation void ?+=?(string & s, char c); // append a character void ?+=?(string & s, const string & s2); // append-concatenate to first string static inline string & strcat(string & s, const string & s2) { s += s2; return s; } void ?+=?(string & s, const char * s2); // append-concatenate to first string static inline string & strcat(string & s, const char * c) { s += c; return s; } string ?+?(const string & s, char c); // add a character to a copy of the string string ?+?(const string & s, const string & s2); // copy and concatenate both strings string ?+?(const char * s1, const char * s2); // concatenate both strings string ?+?(const string & s, const char * c); // copy and concatenate with NULL-terminated string // Repetition string ?*?(const string & s, size_t factor); string ?*?(char c, size_t size); string ?*?(const char *s, size_t size); // Character access char ?[?](const string & s, size_t index); string ?[?](string & s, size_t index); // mutable length-1 slice of original //char codePointAt(const string & s, size_t index); // to revisit under Unicode // Comparisons int strcmp (const string &, const string &); bool ?==?(const string &, const string &); bool ?!=?(const string &, const string &); bool ?>? (const string &, const string &); bool ?>=?(const string &, const string &); bool ?<=?(const string &, const string &); bool ?? (const string &, const char *); bool ?>=?(const string &, const char *); bool ?<=?(const string &, const char *); bool ?? (const char *, const string &); bool ?>=?(const char *, const string &); bool ?<=?(const char *, const string &); bool ?