Changeset e891349


Ignore:
Timestamp:
Jan 17, 2024, 2:16:04 PM (12 months ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
master
Children:
5bf685f
Parents:
06280ad
Message:

Tweak string assignment-strcpy-strncpy and concatenate-strcat-strncat declarations.

Implement -n- versions correctly.

Refactor to include string_res layer.

Add missing tests.

Files:
6 edited

Legend:

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

    r06280ad re891349  
    103103}
    104104
     105void assign(string & s, const string & c, size_t n) {
     106    assign(*s.inner, *c.inner, n);
     107}
     108void assign(string & s, const char * c, size_t n) {
     109    assign(*s.inner, c, n);
     110}
     111
    105112
    106113////////////////////////////////////////////////////////
     
    205212}
    206213
     214void append(string & s, const string & s2, size_t maxlen) {
     215    append( (*s.inner), (*s2.inner), maxlen );
     216}
     217
    207218void ?+=?(string & s, const char * c) {
    208219    (*s.inner) += c;
     220}
     221
     222void append(string & s, const char * buffer, size_t bsize) {
     223    append( (*s.inner), buffer, bsize );
    209224}
    210225
  • libcfa/src/collections/string.hfa

    r06280ad re891349  
    3636void ?{}(string & s, string & s2);
    3737
    38 void ?{}(string & s, const char);
     38void ?{}(string & s, char);
    3939void ?{}(string & s, const char * c); // copy from string literal (NULL-terminated)
    4040void ?{}(string & s, const char * c, size_t size); // copy specific length from buffer
    4141
    4242void ?=?(string & s, const char * c); // copy assignment from literal
    43 static inline string & strcpy(string & s, const char * c) { s = c; return s; }
    44 static inline string & strncpy(string & s, const char * c, size_t n) { s = c; return s; }
    4543void ?=?(string & s, const string & c);
    46 static inline string & strcpy(string & s, const string c) { s = c; return s; }
    4744void ?=?(string & s, char c);
    4845string & ?=?(string & s, string & c);  // surprising ret seems to help avoid calls to autogen
     46void assign(string & s, const string & c, size_t n);
     47void assign(string & s, const char * c, size_t n);
    4948//string ?=?( string &, string ) = void;
     49
     50static inline string & strcpy(string & s, const char * c) { s = c; return s; }
     51static inline string & strncpy(string & s, const char * c, size_t n) { assign( s, c, n); return s; }
     52static inline string & strcpy(string & s, const string & c) { s = c; return s; }
     53static inline string & strncpy(string & s, const string & c, size_t n) { assign(s, c, n); return s; }
     54
    5055void ^?{}(string & s);
    5156
     
    104109void ?+=?(string & s, char c); // append a character
    105110void ?+=?(string & s, const string & s2); // append-concatenate to first string
    106 static inline string & strcat(string & s, const string & s2) { s += s2; return s; }
    107 void ?+=?(string & s, const char * s2); // append-concatenate to first string
    108 static inline string & strcat(string & s, const char * c) { s += c; return s; }
     111void append(string & s, const string & s2, size_t maxlen);  // append-concatenate to first string, up to maxlen
     112void ?+=?(string & s, const char * s2); // append-concatenate NULL-terminated string to first string
     113void append(string & s, const char * buffer, size_t bsize);  // append-concatenate given range to first string
     114
    109115string ?+?(const string & s, char c); // add a character to a copy of the string
    110116string ?+?(const string & s, const string & s2); // copy and concatenate both strings
    111 string ?+?(const char * s1, const char * s2); // concatenate both strings
     117string ?+?(const char * s1, const char * s2); // copy and concatenate both strings
    112118string ?+?(const string & s, const char * c); // copy and concatenate with NULL-terminated string
     119
     120static inline string & strcat(string & s, const string & s2) { s += s2; return s; }
     121static inline string & strcat(string & s, const char * c) { s += c; return s; }
     122static inline string & strncat(string & s, const string & s2, size_t maxlen) { append(s, s2, maxlen); return s; }
     123static inline string & strncat(string & s, const char * buffer, size_t bsize) { append(s, buffer, bsize); return s; }
    113124
    114125// Repetition
  • libcfa/src/collections/string_res.cfa

    r06280ad re891349  
    563563}
    564564
     565string_res & assign(string_res & s, const string_res & src, size_t maxlen) {
     566    return assign_(s, src.Handle.s, min(src.Handle.lnth, maxlen), *0p);
     567}
     568
    565569string_res & assign(string_res & s, const char * buffer, size_t bsize) {
    566570    return assign_(s, buffer, bsize, *0p);
     
    637641}
    638642
     643void append(string_res & str1, const string_res & str2, size_t maxlen) {
     644    append( str1, str2.Handle.s, min(str2.Handle.lnth, maxlen) );
     645}
     646
    639647void ?+=?(string_res & s, char c) {
    640648    append( s, & c, 1 );
    641649}
    642 
     650void ?+=?(string_res & s, const char * c) {
     651    append( s, c, strlen(c) );
     652}
    643653
    644654///////////////////////////////////////////////////////////////////
  • libcfa/src/collections/string_res.hfa

    r06280ad re891349  
    8888}
    8989
     90string_res & assign(string_res & s, const string_res & src, size_t maxlen); // copy specific length from other string
    9091string_res & assign(string_res & s, const char * buffer, size_t bsize); // copy specific length from buffer
    9192static inline string_res & ?=?(string_res & s, const char * c) {  // copy from string literal (NULL-terminated)
     
    129130
    130131// Concatenation
     132void ?+=?(string_res & s, const string_res & s2);
     133void ?+=?(string_res & s, char c);
     134void append(string_res & s, const string_res & s2, size_t maxlen);
     135void ?+=?(string_res & s, const char * c);
    131136void append(string_res & s, const char * buffer, size_t bsize);
    132 void ?+=?(string_res & s, char c); // append a character
    133 void ?+=?(string_res & s, const string_res & s2); // append-concatenate to first string
    134 static inline void ?+=?(string_res & s, const char * c) {
    135     append( s, c, strlen(c) );
    136 }
     137
     138static inline string_res & strcat(string_res & s, const string_res & s2) { s += s2; return s; }
     139static inline string_res & strcat(string_res & s, const char * c) { s += c; return s; }
     140static inline string_res & strncat(string_res & s, const string_res & s2, size_t maxlen) { append(s, s2, maxlen); return s; }
     141static inline string_res & strncat(string_res & s, const char * buffer, size_t bsize) { append(s, buffer, bsize); return s; }
    137142
    138143// Repetition
  • tests/collections/.expect/string-api-coverage.txt

    r06280ad re891349  
    2020hello
    2121world
     221234567
     23hello
     24Q
     25123
     26hell
     271234567
     28hello
     29123
     30hell
    22315
    2332helloworld
     
    2938hello, friend
    3039bye, friend
     40ohello
     41ohell
     42ohell
     43omydarling
     44omy
     45omy
    3146hellohellohellohello
    3247hellohellohello
  • tests/collections/string-api-coverage.cfa

    r06280ad re891349  
    9090    }
    9191                                            assertWellFormedHandleList( 10 );
     92    //
     93    // Assignments
     94    //
     95    {
     96        string b = "xxx";
     97
     98        b = "1234567";
     99        sout | b; // 1234567
     100
     101        b = "xxx";
     102        b = s;
     103        sout | b; // hello
     104       
     105        b = "xxx";
     106        b = 'Q';
     107        sout | b; // Q
     108       
     109        b = "xxx";
     110        assign( b, "1234567", 3 );
     111        sout | b; // 123
     112
     113        b = "xxx";
     114        assign( b, s, 4 );
     115        sout | b; // hell
     116
     117        b = "xxx";
     118        strcpy(b, "1234567");
     119        sout | b; // 1234567
     120
     121        b = "xxx";
     122        strcpy(b, s);
     123        sout | b; // hello
     124       
     125        b = "xxx";
     126        strncpy( b, "1234567", 3 );
     127        sout | b; // 123
     128
     129        b = "xxx";
     130        strncpy( b, s, 4 );
     131        sout | b; // hell
     132    }
     133                                            assertWellFormedHandleList( 10 );
     134
     135
    92136
    93137    sout | size(s); // 5
     
    125169    sx = "bye, " + "friend";
    126170    sout | sx; // bye, friend
     171
     172    sx = "o";
     173    strcat( sx, s );
     174    sout | sx; // ohello
     175
     176    sx = "o";
     177    append( sx, s, 4 );
     178    sout | sx; // ohell
     179
     180    sx = "o";
     181    strncat( sx, s, 4 );
     182    sout | sx; // ohell
     183
     184    sx = "o";
     185    strcat( sx, "mydarling" );
     186    sout | sx; // omydarling
     187
     188    sx = "o";
     189    append( sx, "mydarling", 2 );
     190    sout | sx; // omy
     191
     192    sx = "o";
     193    strncat( sx, "mydarling", 2 );
     194    sout | sx; // omy
    127195
    128196    //
Note: See TracChangeset for help on using the changeset viewer.