Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 4110

Show
Ignore:
Timestamp:
11/22/08 04:53:36 (3 hours ago)
Author:
kris
Message:

added opSlice() and reserve().

Also adjusted fill(src) and read(dst) to be more careful about how they reset the buffer indices, in order to preserve slicing positions within the buffer.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/tango/io/Buffer.d

    r3838 r4110  
    386386        /*********************************************************************** 
    387387 
     388                Retrieve the valid content 
     389 
     390                Returns: 
     391                a void[] slice of the buffer 
     392 
     393                Remarks: 
     394                Return a void[] slice of the buffer, from the current position 
     395                up to the limit of valid content. The content remains in the 
     396                buffer for future extraction. 
     397 
     398        ***********************************************************************/ 
     399 
     400        void[] slice () 
     401        { 
     402                return  data [index .. extent]; 
     403        } 
     404 
     405        /*********************************************************************** 
     406         
     407                Return a void[] slice of the buffer from start to end, where 
     408                end is exclusive 
     409 
     410        ***********************************************************************/ 
     411 
     412        final void[] opSlice (uint start, uint end) 
     413        { 
     414                assert (start <= extent && end <= extent && start <= end); 
     415                return data [start .. end]; 
     416        } 
     417 
     418        /*********************************************************************** 
     419 
    388420                Access buffer content 
    389421 
     
    431463                   // in the buffer as possible, such that entire records may 
    432464                   // be aliased directly from within. 
    433                    if (size > writable
     465                   if (size > (dimension - index)
    434466                      { 
    435467                      if (size > dimension) 
     
    619651        /*********************************************************************** 
    620652 
    621                 Retrieve the valid content 
    622  
    623                 Returns: 
    624                 a void[] slice of the buffer 
    625  
    626                 Remarks: 
    627                 Return a void[] slice of the buffer, from the current position 
    628                 up to the limit of valid content. The content remains in the 
    629                 buffer for future extraction. 
    630  
    631         ***********************************************************************/ 
    632  
    633         void[] slice () 
    634         { 
    635                 return  data [index .. extent]; 
    636         } 
    637  
    638         /*********************************************************************** 
    639  
    640653                Move the current read location 
    641654 
     
    773786        /*********************************************************************** 
    774787 
     788                Reserve the specified space within the buffer, compressing 
     789                existing content as necessary to make room 
     790 
     791                Returns the current read point, after compression if that 
     792                was required 
     793 
     794        ***********************************************************************/ 
     795 
     796        final uint reserve (uint space) 
     797        {        
     798                assert (space < dimension); 
     799 
     800                if ((dimension - index) < space) 
     801                     compress; 
     802                return index; 
     803        } 
     804 
     805        /*********************************************************************** 
     806 
    775807                Write into this buffer 
    776808 
     
    855887 
    856888        IBuffer compress () 
    857         { 
    858                 uint r = readable ()
     889        {        
     890                uint r = readable
    859891 
    860892                if (index > 0 && r > 0) 
     
    887919                if (src is null) 
    888920                    return IConduit.Eof; 
    889  
     921/+ 
     922                // should not reset here, since we're only filling! 
    890923                if (readable is 0 && canCompress) 
    891924                    index = extent = 0;  // same as clear(), without call-chain 
     
    893926                   if (writable is 0) 
    894927                       return 0; 
    895  
     928+/ 
    896929                return write (&src.read); 
    897930        } 
     
    13211354                          content = source.read (dst); 
    13221355                      else 
     1356                         { 
     1357                         if (writable is 0) 
     1358                             index = extent = 0;  // same as clear(), without call-chain 
     1359 
    13231360                         // keep buffer partially populated 
    13241361                         if ((content = fill(source)) != IConduit.Eof && content > 0) 
    13251362                              content = read (dst); 
     1363                         } 
    13261364                      } 
    13271365                   else 
     
    16241662        } 
    16251663} 
     1664 
     1665 
     1666/****************************************************************************** 
     1667 
     1668******************************************************************************/ 
     1669 
     1670debug (Buffer) 
     1671{ 
     1672        void main() 
     1673        {        
     1674                auto b = new Buffer(6); 
     1675                b.append ("fubar"); 
     1676                b.reserve (1); 
     1677                b.slice (5); 
     1678                b.reserve (4); 
     1679        } 
     1680} 
  • trunk/tango/io/model/IBuffer.d

    r3598 r4110  
    7878        /*********************************************************************** 
    7979         
    80                 Return a void[] slice of the buffer up to the limit of 
    81                 valid content. 
    82  
    83         ***********************************************************************/ 
    84  
    85         abstract void[] slice (); 
    86  
    87         /*********************************************************************** 
    88          
    8980                Set the backing array with all content readable. Writing 
    9081                to this will either flush it to an associated conduit, or 
     
    169160 
    170161        abstract void consume (void[] src); 
     162 
     163        /*********************************************************************** 
     164         
     165                Return a void[] slice of the buffer up to the limit of 
     166                valid content. 
     167 
     168        ***********************************************************************/ 
     169 
     170        abstract void[] slice (); 
     171 
     172        /*********************************************************************** 
     173         
     174                Return a void[] slice of the buffer from start to end, where 
     175                end is exclusive 
     176 
     177        ***********************************************************************/ 
     178 
     179        abstract void[] opSlice (uint start, uint end); 
    171180 
    172181        /*********************************************************************** 
     
    363372 
    364373        /*********************************************************************** 
     374 
     375                Reserve the specified space within the buffer, compressing 
     376                existing content as necessary to make room 
     377 
     378                Returns the current read point, after compression if that 
     379                was required 
     380 
     381        ***********************************************************************/ 
     382 
     383        abstract uint reserve (uint space); 
     384 
     385        /*********************************************************************** 
    365386         
    366387                Returns the limit of readable content within this buffer.