// // 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 : Sun Oct 13 10:50:30 2024 // Update Count : 131 // #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 string & s2 ); void ?{}( string & s, const string & s2, size_t maxlen ); void ?{}( string & s, string & s2 ); void ?{}( string & s, char ); void ?{}( string & s, const char * c ); // copy from string literal (NULL-terminated) void ?{}( string & s, const char * c, size_t size ); // copy specific length from buffer void ?{}( string & s, ssize_t rhs ); void ?{}( string & s, size_t rhs ); void ?{}( string & s, double rhs ); void ?{}( string & s, long double rhs ); void ?{}( string & s, double _Complex rhs ); void ?{}( string & s, long double _Complex rhs ); string str( ssize_t rhs ); string str( size_t rhs ); string str( double rhs ); string str( long double rhs ); string str( double _Complex rhs ); string str( long double _Complex rhs ); string & ?=?( string & s, const string & c ); string & ?=?( string & s, string & c ); string & ?=?( string & s, const char * c ); // copy from "literal" string & ?=?( string & s, char c ); // copy from 'l' string & assign( string & s, const string & c, size_t n ); string & assign( string & s, const char * c, size_t n ); static inline string & strcpy( string & s, const char * c ) { s = c; return s; } static inline string & strncpy( string & s, const char * c, size_t n ) { assign( s, c, n ); return s; } static inline string & strcpy( string & s, const string & c ) { s = c; return s; } static inline string & strncpy( string & s, const string & c, size_t n ) { assign( s, c, n ); return s; } string & ?=?( string & s, ssize_t rhs ); string & ?=?( string & s, size_t rhs ); string & ?=?( string & s, double rhs ); string & ?=?( string & s, long double rhs ); string & ?=?( string & s, double _Complex rhs ); string & ?=?( string & s, long double _Complex rhs ); 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 ); 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_Swidth { string & s; inline _Istream_str_base; }; // _Istream_Swidth struct _Istream_Squoted { _Istream_Swidth sstr; }; // _Istream_Squoted struct _Istream_Sstr { string & s; inline _Istream_str_base; // _Istream_Swidth sstr; }; // _Istream_Sstr static inline { // read width does not include null terminator _Istream_Swidth wdi( unsigned int rwd, string & s ) { return (_Istream_Swidth)@{ .s = s, { {.scanset = 0p}, .wd = rwd, {.flags.rwd = true} } }; } _Istream_Sstr getline( string & s, const char delimiter = '\n' ) { // return (_Istream_Sstr)@{ { .s = s, { {.delimiters = { delimiter, '\0' } }, .wd = -1, {.flags.delimiter = true} } } }; return (_Istream_Sstr)@{ .s = s, { {.delimiters = { delimiter, '\0' } }, .wd = -1, {.flags.delimiter = true} } }; } _Istream_Sstr & getline( _Istream_Swidth & f, const char delimiter = '\n' ) { f.delimiters[0] = delimiter; f.delimiters[1] = '\0'; f.flags.delimiter = true; return (_Istream_Sstr &)f; } _Istream_Squoted quoted( string & s, const char Ldelimiter = '\"', const char Rdelimiter = '\0' ) { return (_Istream_Squoted)@{ { .s = s, { {.delimiters = { Ldelimiter, Rdelimiter, '\0' }}, .wd = -1, {.flags.rwd = true} } } }; } _Istream_Squoted & quoted( _Istream_Swidth & f, const char Ldelimiter = '"', const char Rdelimiter = '\0' ) { f.delimiters[0] = Ldelimiter; f.delimiters[1] = Rdelimiter; f.delimiters[2] = '\0'; return (_Istream_Squoted &)f; } // _Istream_Sstr incl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ { .s = s, { {.scanset = scanset}, .wd = -1, {.flags.inex = false} } } }; } _Istream_Sstr incl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ .s = s, { {.scanset = scanset}, .wd = -1, {.flags.inex = false} } }; } _Istream_Sstr & incl( const char scanset[], _Istream_Swidth & f ) { f.scanset = scanset; f.flags.inex = false; return (_Istream_Sstr &)f; } // _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ { .s = s, { {.scanset = scanset}, .wd = -1, {.flags.inex = true} } } }; } _Istream_Sstr excl( const char scanset[], string & s ) { return (_Istream_Sstr)@{ .s = s, { {.scanset = scanset}, .wd = -1, {.flags.inex = true} } }; } _Istream_Sstr & excl( const char scanset[], _Istream_Swidth & f ) { f.scanset = scanset; f.flags.inex = true; return (_Istream_Sstr &)f; } // _Istream_Sstr ignore( string & s ) { return (_Istream_Sstr)@{ { .s = s, { {.scanset = 0p}, .wd = -1, {.flags.ignore = true} } } }; } _Istream_Sstr ignore( string & s ) { return (_Istream_Sstr)@{ .s = s, { {.scanset = 0p}, .wd = -1, {.flags.ignore = true} } }; } _Istream_Sstr & ignore( _Istream_Swidth & f ) { f.flags.ignore = true; return (_Istream_Sstr &)f; } _Istream_Squoted & ignore( _Istream_Squoted & f ) { f.sstr.flags.ignore = true; return (_Istream_Squoted &)f; } // _Istream_Sstr & ignore( _Istream_Sstr & f ) { f.sstr.flags.ignore = true; return (_Istream_Sstr &)f; } _Istream_Sstr & ignore( _Istream_Sstr & f ) { f.flags.ignore = true; return (_Istream_Sstr &)f; } } // distribution ifstream & ?|?( ifstream & is, _Istream_Squoted f ); ifstream & ?|?( ifstream & is, _Istream_Sstr f ); static inline ifstream & ?|?( ifstream & is, _Istream_Swidth f ) { return is | *(_Istream_Sstr *)&f; } // Concatenation void ?+=?( string & s, char c ); // append a character void ?+=?( string & s, const string & s2 ); // append-concatenate to first string void append( string & s, const string & s2, size_t maxlen ); // append-concatenate to first string, up to maxlen void ?+=?( string & s, const char * s2 ); // append-concatenate NULL-terminated string to first string void append( string & s, const char * buffer, size_t bsize ); // append-concatenate given range to first string string ?+?( const string & s, char c ); // add a character to a copy of the string string ?+?( char c, const string & s ); // add a character to a copy of the string string ?+?( const string & s, const string & s2 ); // copy and concatenate both strings string ?+?( const char * s, char c ); // add a character to a copy of the string string ?+?( char c, const char * s ); // add a character to a copy of the string string ?+?( const char * c, const char * s ); // copy and add with two NULL-terminated string string ?+?( const char * c, string & s ); // copy and add with NULL-terminated string string ?+?( const string & s, const char * c ); // copy and add with NULL-terminated string static inline string & strcat( string & s, const string & s2 ) { s += s2; return s; } static inline string & strcat( string & s, const char * c ) { s += c; return s; } static inline string & strncat( string & s, const string & s2, size_t maxlen ) { append( s, s2, maxlen ); return s; } static inline string & strncat( string & s, const char * buffer, size_t bsize ) { append( s, buffer, bsize ); return s; } // Repetition string ?*?( const string & s, size_t factor ); void ?*=?( string & s, size_t factor ); string ?*?( char c, size_t factor ); string ?*?( const char *s, size_t factor ); // 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 ?