Changeset 3f631d6


Ignore:
Timestamp:
Apr 10, 2025, 7:44:17 PM (5 months ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
master
Children:
570e7ad
Parents:
d175767
Message:

Switch string io to be generic upon iostream, rather than specific upon fstream.

No direct test added because we currently lack a second implementation of the abstract streams.

Benefit is removing polymorphism-induced noise from cost calculations, in upcoming string-overload reorganizations.

The change in tests 'collections/string-operator*' shows cases where we stopped picking string for the wrong reason. (If we should be picking string for a better reason, that's just ahead.)

Files:
1 added
13 edited

Legend:

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

    rd175767 r3f631d6  
    214214// Input-Output
    215215
    216 ofstream & ?|?( ofstream & out, const string & s ) {
    217         return out | (*s.inner); // print internal string_res
    218 }
    219 
    220 void ?|?( ofstream & out, const string & s ) {
    221         (ofstream &)(out | (*s.inner)); ends( out );
    222 }
    223 
    224 ofstream & ?|?( ofstream & os, _Ostream_Manip(string) f ) {
    225         size_t l = len( f.val );
    226         char cstr[l + 1];                                                                       // room for null terminator
    227         for ( i; l ) cstr[i] = f.val[i];                                        // copy string
    228         cstr[l] = '\0';                                                                         // terminate
    229         _Ostream_Manip(const char *) cf @= { cstr, f.wd, f.pc, f.base, {f.all} };
    230         return os | cf | nonl;
    231 } // ?|?
    232 
    233 void ?|?( ofstream & os, _Ostream_Manip(string) f ) {
    234         (ofstream &)(os | f); ends( os );
    235 }
    236 
    237 ifstream & ?|?( ifstream & in, string & s ) {
    238         return in | (*s.inner); // read to internal string_res
    239 }
    240 
    241 ifstream & ?|?( ifstream & is, _Istream_Squoted f ) {
    242         _Istream_Rquoted f2 = { { f.sstr.s.inner, (_Istream_str_base)f.sstr } };
    243         return is | f2;
    244 } // ?|?
    245 
    246 ifstream & ?|?( ifstream & is, _Istream_Sstr f ) {
    247 //      _Istream_Rstr f2 = {f.sstr.s.inner, (_Istream_str_base)f.sstr};
    248         _Istream_Rstr f2 = {f.s.inner, (_Istream_str_base)f};
    249         return is | f2;
    250 } // ?|?
     216forall( ostype & | basic_ostream( ostype ) ) {
     217
     218        ostype & ?|?( ostype & out, string s ) {
     219                return out | (*s.inner); // print internal string_res
     220        }
     221
     222        void ?|?( ostype & out, string s ) {
     223                (ostype &)(out | (*s.inner)); ends( out );
     224        }
     225
     226        ostype & ?|?( ostype & os, _Ostream_Manip(string) f ) {
     227                size_t l = len( f.val );
     228                char cstr[l + 1];                                                                       // room for null terminator
     229                for ( i; l ) cstr[i] = f.val[i];                                        // copy string
     230                cstr[l] = '\0';                                                                         // terminate
     231                _Ostream_Manip(const char *) cf @= { cstr, f.wd, f.pc, f.base, {f.all} };
     232                return os | cf | nonl;
     233        } // ?|?
     234
     235        void ?|?( ostype & os, _Ostream_Manip(string) f ) {
     236                (ostype &)(os | f); ends( os );
     237        }
     238}
     239
     240forall( istype & | basic_istream( istype ) ) {
     241
     242        istype & ?|?( istype & in, string & s ) {
     243                return in | (*s.inner); // read to internal string_res
     244        }
     245
     246        istype & ?|?( istype & is, _Istream_Squoted f ) {
     247                _Istream_Rquoted f2 = { { f.sstr.s.inner, (_Istream_str_base)f.sstr } };
     248                return is | f2;
     249        } // ?|?
     250
     251        istype & ?|?( istype & is, _Istream_Sstr f ) {
     252        //      _Istream_Rstr f2 = {f.sstr.s.inner, (_Istream_str_base)f.sstr};
     253                _Istream_Rstr f2 = {f.s.inner, (_Istream_str_base)f};
     254                return is | f2;
     255        } // ?|?
     256}
    251257
    252258////////////////////////////////////////////////////////
  • libcfa/src/collections/string.hfa

    rd175767 r3f631d6  
    1616#pragma once
    1717
    18 #include <fstream.hfa>
     18#include <iostream.hfa>
    1919#include <string_res.hfa>
    2020
     
    8080
    8181// IO Operator
    82 ofstream & ?|?( ofstream & out, const string & s );
    83 void ?|?( ofstream & out, const string & s );
    84 ifstream & ?|?( ifstream & in, string & s );
     82forall( ostype & | basic_ostream( ostype ) ) {
     83        ostype & ?|?( ostype & out, string s );
     84        void ?|?( ostype & out, string s );
     85}
     86forall( istype & | basic_istream( istype ) )
     87istype & ?|?( istype & in, string & s );
    8588
    8689static inline {
     
    9598        _Ostream_Manip(string) & nobase( _Ostream_Manip(string) & fmt ) { fmt.flags.nobsdp = true; return fmt; }
    9699} // distribution
    97 ofstream & ?|?( ofstream & os, _Ostream_Manip(string) f );
    98 void ?|?( ofstream & os, _Ostream_Manip(string) );
     100forall( ostype & | basic_ostream( ostype ) ) {
     101        ostype & ?|?( ostype & os, _Ostream_Manip(string) f );
     102        void ?|?( ostype & os, _Ostream_Manip(string) );
     103}
    99104
    100105struct _Istream_Swidth {
     
    143148        _Istream_Sstr & ignore( _Istream_Sstr & f ) { f.flags.ignore = true; return (_Istream_Sstr &)f; }
    144149} // distribution
    145 ifstream & ?|?( ifstream & is, _Istream_Squoted f );
    146 ifstream & ?|?( ifstream & is, _Istream_Sstr f );
    147 static inline ifstream & ?|?( ifstream & is, _Istream_Swidth f ) { return is | *(_Istream_Sstr *)&f; }
     150forall( istype & | basic_istream( istype ) ) {
     151        istype & ?|?( istype & is, _Istream_Squoted f );
     152        istype & ?|?( istype & is, _Istream_Sstr f );
     153        static inline istype & ?|?( istype & is, _Istream_Swidth f ) { return is | *(_Istream_Sstr *)&f; }
     154}
    148155
    149156// Concatenation
  • libcfa/src/collections/string_res.cfa

    rd175767 r3f631d6  
    193193
    194194// Output operator
    195 ofstream & ?|?(ofstream & out, const string_res & s) {
     195forall( ostype & | basic_ostream( ostype ) )
     196ostype & ?|?( ostype & out, const string_res & s ) {
    196197        // CFA string is NOT null terminated, so print exactly lnth characters in a minimum width of 0.
    197198        return out | wd( 0, s.Handle.lnth, s.Handle.s ) | nonl;
    198199}
    199200
    200 void ?|?(ofstream & out, const string_res & s) {
    201         (ofstream &)(out | s); ends( out );
     201forall( ostype & | basic_ostream( ostype ) )
     202void ?|?( ostype & out, const string_res & s ) {
     203        (ostype &)(out | s); ends( out );
    202204}
    203205
    204206// Input operator
    205 ifstream & ?|?(ifstream & in, string_res & s) {
     207forall( istype & | basic_istream( istype ) )
     208istype & ?|?( istype & in, string_res & s ) {
    206209        // Reading into a temp before assigning to s is near zero overhead in typical cases because of sharing.
    207210        // If s is a substring of something larger, simple assignment takes care of that case correctly.
     
    245248}
    246249
    247 ifstream & ?|?( ifstream & is, _Istream_Rquoted f ) with( f.rstr ) {
     250forall( istype & | basic_istream( istype ) )
     251istype & ?|?( istype & is, _Istream_Rquoted f ) with( f.rstr ) {
    248252        if ( eof( is ) ) throwResume ExceptionInst( end_of_file );
    249253        int args;
     
    270274}
    271275
    272 ifstream & ?|?( ifstream & is, _Istream_Rstr f ) {
     276forall( istype & | basic_istream( istype ) )
     277istype & ?|?( istype & is, _Istream_Rstr f ) {
    273278        // .---------------,
    274279        // | | | | |...|0|0| null terminator and guard if missing
  • libcfa/src/collections/string_res.hfa

    rd175767 r3f631d6  
    1616#pragma once
    1717
    18 #include <fstream.hfa>
     18#include <iostream.hfa>
    1919#include <string.h>    // e.g. strlen
    2020
     
    120120
    121121// IO Operator
    122 ofstream & ?|?(ofstream & out, const string_res & s);
    123 void ?|?(ofstream & out, const string_res & s);
    124 ifstream & ?|?(ifstream & in, string_res & s);
     122forall( ostype & | basic_ostream( ostype ) ) {
     123        ostype & ?|?(ostype & out, const string_res & s);
     124        void ?|?(ostype & out, const string_res & s);
     125}
     126forall( istype & | basic_istream( istype ) )
     127istype & ?|?(istype & in, string_res & s);
    125128
    126129struct _Istream_Rwidth {
     
    167170        _Istream_Rstr & ignore( _Istream_Rstr & f ) { f.flags.ignore = true; return (_Istream_Rstr &)f; }
    168171} // distribution
    169 ifstream & ?|?( ifstream & is, _Istream_Rquoted f );
    170 ifstream & ?|?( ifstream & is, _Istream_Rstr f );
    171 static inline ifstream & ?|?( ifstream & is, _Istream_Rwidth f ) { return is | *(_Istream_Rstr *)&f; }
     172forall( istype & | basic_istream( istype ) ) {
     173        istype & ?|?( istype & is, _Istream_Rquoted f );
     174        istype & ?|?( istype & is, _Istream_Rstr f );
     175        static inline istype & ?|?( istype & is, _Istream_Rwidth f ) { return is | *(_Istream_Rstr *)&f; }
     176}
    172177
    173178// Concatenation
  • tests/Makefile.am

    rd175767 r3f631d6  
    288288        -cp ${test} ${abspath ${@}}
    289289
     290collections/string-operator-ERR09 : collections/string-operator.cfa
     291        ${CFACOMPILE_SYNTAX} -DTRY_MR09
     292        -cp ${test} ${abspath ${@}}
     293
    290294collections/string-operator-ERR15 : collections/string-operator.cfa
    291295        ${CFACOMPILE_SYNTAX} -DTRY_MR15
  • tests/collections/.expect/string-operator.txt

    rd175767 r3f631d6  
    6565
    6666------------- Bare (sout-direct)
    67 ab
     67(skip)
    6868ab
    6969ab
     
    8585axb
    8686
    87 aaa
     87291
    8888bbb
    89 ÃÃÃ
     89585
    9090cdcdcd
    9191
    9292291
    9393bbb
    94 ababab
     94585
    9595cdcdcd
    9696
  • tests/collections/string-api-coverage.cfa

    rd175767 r3f631d6  
    1 #include <collections/string.hfa>
     1#include <string.hfa>
    22#include <string_sharectx.hfa>
     3#include <fstream.hfa>
    34
    45
  • tests/collections/string-ctx-manage.cfa

    rd175767 r3f631d6  
    22#include <string_sharectx.hfa>
    33#include <string_res.hfa>
     4#include <fstream.hfa>
    45
    56// In these tests, shared heaps are never remotely full and string sizes are tiny.
  • tests/collections/string-gc.cfa

    rd175767 r3f631d6  
    11#include <string_res.hfa>
     2#include <fstream.hfa>
    23
    34size_t bytesRemaining() {
  • tests/collections/string-istream-manip.cfa

    rd175767 r3f631d6  
    11
    22#include <fstream.hfa>
    3 #include <collections/string.hfa>
    4 #include <collections/string_res.hfa>
     3#include <string.hfa>
     4#include <string_res.hfa>
    55#include <stdio.h>
    66
  • tests/collections/string-istream.cfa

    rd175767 r3f631d6  
    1 #include <iostream.hfa>
    2 #include <collections/string.hfa>
    3 #include <collections/string_res.hfa>
     1#include <fstream.hfa>
     2#include <string.hfa>
     3#include <string_res.hfa>
    44
    55
  • tests/collections/string-operator.cfa

    rd175767 r3f631d6  
    99
    1010// These MR points do reject in the current revision, so they have satellite "-ERR" cases:
     11// MR09
    1112// MR15
    1213
     
    2021#define TRY_MR07
    2122#define TRY_MR08
    22 #define TRY_MR09
    2323#define TRY_MR10
    2424#define TRY_MR11
     
    275275        s = "x";
    276276
    277 MR09(   sout | 'a' + 'b';      )    // ab
     277MR09(   sout | 'a' + 'b';      )    // (ambiguous)
    278278        sout | 'a' + "b";           // ab
    279279        sout | "a" + 'b';           // ab
     
    299299        sout | nl;                  //
    300300
    301 MR11(   sout | 'a' * 3;          )  // aaa
     301MR11(   sout | 'a' * 3;          )  // 291
    302302        sout | "b" * 3;             // bbb
    303 MR12(   sout | ('a' + 'b') * 3;  )  // ÃÃÃ
     303MR12(   sout | ('a' + 'b') * 3;  )  // 585
    304304        sout | ('c' + "d") * 3;     // cdcdcd
    305305        sout | nl;                  //
     
    307307MR13(   sout | 3 * 'a';          )  // 291
    308308        sout | 3 * "b";             // bbb
    309 MR14(   sout | 3 * ('a' + 'b');  )  // ababab
     309MR14(   sout | 3 * ('a' + 'b');  )  // 585
    310310        sout | 3 * ('c' + "d");     // cdcdcd
    311311        sout | nl;                  //
  • tests/collections/string-overwrite.cfa

    rd175767 r3f631d6  
    1 #include <collections/string.hfa>
     1#include <string.hfa>
    22#include <string_sharectx.hfa>
     3#include <fstream.hfa>
    34
    45/*
Note: See TracChangeset for help on using the changeset viewer.