Ignore:
Timestamp:
Apr 11, 2025, 12:29:56 AM (6 months ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
master
Children:
d03a386
Parents:
3f631d6
Message:

Make string operator-overload costs match their intuitively equivalent arithmetics.

Replace many by-reference string args with by-value args to work around noise from the reference-cost column.

Use a special arithmetic type for the factor argument of ?*? to match conversion cost of (char*int=int).

Removes cost-function noise of char-arithmetic operators being preferred over their string-concatenation equivalents in the reference-cost column.

Notably, all former Spanish-A and numeric outputs have become ambiguous or been associated with a reproducible bug.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/collections/string.hfa

    r3f631d6 r570e7ad  
    2727
    2828void ?{}( string & s );                                                                 // empty string
    29 void ?{}( string & s, const string & s2 );
    30 void ?{}( string & s, const string & s2, size_t maxlen );
    31 void ?{}( string & s, string & s2 );
    32 
     29void ?{}( string & s, string s2, size_t maxlen );
     30void ?{}( string & s, string s2 );
    3331void ?{}( string & s, char );
    3432void ?{}( string & s, const char * c );                                 // copy from string literal (NULL-terminated)
    3533void ?{}( string & s, const char * c, size_t size );    // copy specific length from buffer
    3634
    37 void ?{}( string & s, ssize_t rhs );
     35void ?{}( string & s, signed long int rhs );
    3836void ?{}( string & s, size_t rhs );
    3937void ?{}( string & s, double rhs );
     
    4139void ?{}( string & s, double _Complex rhs );
    4240void ?{}( string & s, long double _Complex rhs );
     41static inline void ?{}( string & s, int rhs ) { (s){(signed long int) rhs}; }
    4342
    4443// string str( ssize_t rhs );
     
    4948// string str( long double _Complex rhs );
    5049
    51 string & ?=?( string & s, const string & c );
    52 string & ?=?( string & s, string & c );
     50string & ?=?( string & s, string c );
    5351string & ?=?( string & s, const char * c );                             // copy from "literal"
    5452string & ?=?( string & s, char c );                                             // copy from 'l'
    5553string & assign( string & s, const string & c, size_t n );
    5654string & assign( string & s, const char * c, size_t n );
    57 string & ?=?( string & s, ssize_t rhs );
     55string & ?=?( string & s, signed long int rhs );
    5856string & ?=?( string & s, size_t rhs );
    5957string & ?=?( string & s, double rhs );
     
    6159string & ?=?( string & s, double _Complex rhs );
    6260string & ?=?( string & s, long double _Complex rhs );
     61static inline string & ?=?( string & s, int rhs ) { return s = ((signed long int) rhs); } // to match cost of (char * int): int
    6362
    6463static inline string & strcpy( string & s, const char * c ) { s = c; return s; }
     
    161160void append( string & s, const char * buffer, size_t bsize );
    162161
    163 string ?+?( const string & s, char c );
    164 string ?+?( char c, const string & s );
    165 string ?+?( const string & s, const string & s2 );
     162string ?+?( string s, char c );
     163string ?+?( char c, string s );
     164string ?+?( string s, string s2 );
    166165string ?+?( const char * s, char c );                                   // not backwards compatible
    167166string ?+?( char c, const char * s );
    168167string ?+?( const char * c, const char * s );
    169 string ?+?( const char * c, const string & s );
    170 string ?+?( const string & s, const char * c );
     168string ?+?( const char * c, string s );
     169string ?+?( string s, const char * c );
    171170string ?+?( char, char );                                                               // not being called 8-(
    172171
     
    177176
    178177// Repetition
    179 void ?*=?( string & s, size_t factor );
    180 string ?*?( char c, size_t factor );                                    // not backwards compatible
    181 string ?*?( const string & s, size_t factor );
    182 static inline string ?*?( size_t factor, const string & s ) { return s * factor; }
    183 string ?*?( const char * s, size_t factor );
    184 static inline string ?*?( size_t factor, const char * s ) { return s * factor; }
     178
     179// Type `signed long long int` chosen for `factor` argument to achieve cost detente.
     180// This way, the call `'a' * 3` gets the same safe conversion cost calling here as for
     181// the built-in definition `int * int`.
     182typedef signed long long int strmul_factor_t;
     183
     184void ?*=?( string & s, strmul_factor_t factor );
     185string ?*?( char c, strmul_factor_t factor );                                   // not backwards compatible
     186string ?*?( string s, strmul_factor_t factor );
     187string ?*?( const char * s, strmul_factor_t factor );
     188static inline string ?*?( strmul_factor_t factor, char s ) { return s * factor; }
     189static inline string ?*?( strmul_factor_t factor, string s ) { return s * factor; }
     190static inline string ?*?( strmul_factor_t factor, const char * s ) { return s * factor; }
    185191
    186192// Character access
Note: See TracChangeset for help on using the changeset viewer.