Changeset d03a386 for libcfa/src


Ignore:
Timestamp:
Apr 11, 2025, 1:23:39 AM (5 months ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
master
Children:
dab6e39
Parents:
570e7ad
Message:

Give a few string operator overloads a preference boost.

Intent is to approximate: When selecting +/* candidates, treat it ambiguous until finding a user-given arithmetic-vs-string constraint, such as assigning the result to a string. Once a string interpretation is imposed, prefer an alternative that converts to string as soon as possible.

This description is not directly achievable with the CFA type system. The approximation has the known flaw shown in the string-operator test, where a fairly built-up expression that should be ambiguous is actually defaulting to the string version.

This change is the last of the string-overload reorganizations that the string-operator test was originally meant to illustrate. In Mike's opinion, the resulting state is ideal, except for the just-mentioned flaw.

Location:
libcfa/src/collections
Files:
2 edited

Legend:

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

    r570e7ad rd03a386  
    1414//
    1515
     16#define _COMPILING_STRING_CFA_
     17
    1618#include "string.hfa"
    1719#include "string_res.hfa"
     
    4042}
    4143
    42 void ?{}( string & s, string c ) {  // c is a memcpy of the real src string
     44PBOOST void ?{}( string & s, string c ) {  // c is a memcpy of the real src string
    4345        (s.inner) { malloc() };
    4446        ?{}( *s.inner, *c.inner, COPY_VALUE );
     
    146148// Assignment
    147149
    148 string & ?=?( string & s, string c ) {
     150PBOOST string & ?=?( string & s, string c ) {
    149151        (*s.inner) = (*c.inner);
    150152        return s;
     
    326328}
    327329
    328 string ?+?( string s, string s2 ) {
     330PBOOST string ?+?( string s, string s2 ) {
    329331        string ret = s;
    330332        ret += s2;
     
    375377}
    376378
    377 string ?*?( string s, strmul_factor_t factor ) {
     379PBOOST string ?*?( string s, strmul_factor_t factor ) {
    378380        string ret = s;
    379381        ret *= factor;
  • libcfa/src/collections/string.hfa

    r570e7ad rd03a386  
    1919#include <string_res.hfa>
    2020
     21static struct __cfa_string_preference_boost_t {} __cfa_string_preference_boost;
     22#define PBOOST forall ( | { __cfa_string_preference_boost_t __cfa_string_preference_boost; } )
     23
    2124struct string {
    2225        string_res * inner;
     
    2831void ?{}( string & s );                                                                 // empty string
    2932void ?{}( string & s, string s2, size_t maxlen );
    30 void ?{}( string & s, string s2 );
     33PBOOST void ?{}( string & s, string s2 );
    3134void ?{}( string & s, char );
    3235void ?{}( string & s, const char * c );                                 // copy from string literal (NULL-terminated)
     
    4851// string str( long double _Complex rhs );
    4952
    50 string & ?=?( string & s, string c );
     53PBOOST string & ?=?( string & s, string c );
    5154string & ?=?( string & s, const char * c );                             // copy from "literal"
    5255string & ?=?( string & s, char c );                                             // copy from 'l'
     
    162165string ?+?( string s, char c );
    163166string ?+?( char c, string s );
    164 string ?+?( string s, string s2 );
     167PBOOST string ?+?( string s, string s2 );
    165168string ?+?( const char * s, char c );                                   // not backwards compatible
    166169string ?+?( char c, const char * s );
     
    184187void ?*=?( string & s, strmul_factor_t factor );
    185188string ?*?( char c, strmul_factor_t factor );                                   // not backwards compatible
    186 string ?*?( string s, strmul_factor_t factor );
     189PBOOST string ?*?( string s, strmul_factor_t factor );
    187190string ?*?( const char * s, strmul_factor_t factor );
    188191static inline string ?*?( strmul_factor_t factor, char s ) { return s * factor; }
    189 static inline string ?*?( strmul_factor_t factor, string s ) { return s * factor; }
     192PBOOST static inline string ?*?( strmul_factor_t factor, string s ) { return s * factor; }
    190193static inline string ?*?( strmul_factor_t factor, const char * s ) { return s * factor; }
    191194
     
    314317        return translate( S, f );
    315318}
     319
     320#ifndef _COMPILING_STRING_CFA_
     321#undef PBOOST
     322#endif
Note: See TracChangeset for help on using the changeset viewer.