Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 0fc7a0e23fba46321b983a9537ffe4ab3a1285f6
Ticket: 1665628fffffffffffffffffffffffffffffffff
destructive scan of a ByteArray
User & Date: dvrsn on 2007-02-21 21:44:38
Changes

  1. assignee changed to: "dkf"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    There is no way to efficiently modify a ByteArray, either by appending data to the end or by removing data from the front (as might be useful for a binary stream buffer).  
    
    Consider one task to read from a binary socket and buffer the data and another to decode counted strings from the front of the binary string:
    
      variable buffer
      proc read_handler {sock} {
        variable buffer
        append buffer [read $sock]
      }
      
      proc process_buffer {} {
        variable buffer
        if {[string length $buffer] > 0} {
          binary scan $buffer "I" dlen
          binary scan $buffer "x4 a$dlen" data
          binary scan $buffer "x4 x$dlen a*" buffer
        }
      }
    
    The first problem is that "append" only operates on string reps, which requires the entire ByteArray to be scanned and converted to the utf-8 (or unicode) encoding.  (append also appears to unset the length of the string, meaning that a subsequent "string length" must recompute it).
    
    The second problem is that scanning a few bytes from the front of the ByteArray and the modifying the original means that the entire ByteArray needs to be copied.  (maintaining a separate cursor is efficient but clumsy).
    
    "append" should probably do the sensible thing and append ByteArrays efficiently (as well as setting the length correctly), but since this is specifically dealing with binary strings it may make sense to implement these operations entirely as subcommands of "binary".
    
    I suggest
      binary append varName ?arg arg ...?
    to append the ByteArray values of all args to the named binary value; and 
      binary dscan varName formatString ?varName varName ...?
    dscan = "destructive scan", with functionality identical to "binary scan" except that it takes a varName instead of a string, and it changes the ByteArray object to have the start offset at the end of the format arguments   This would need the ByteArray object to store an offset indicating where the start of the data is.  The length computation of a ByteArray object would need to be changed to use the offset but this should be contained within tclBinary.c.  The destructive operation could either just change the offset or actually modify the underlying bytes.  If it works by changing the offset then there would need to be some point at which it would actually modify the bytes because the memory between the start of the bytes and the offset is wasted; this point could be some hardcoded maximum or a heuristic measure based on the overall size of the data.  (i.e., if the wasted space is larger than the used space, copy the live data to the beginning of the allocated space, limiting the wasted space to 100% overhead and using a non-overlapping move operation).
    
    Another method for modifying a binary object could be
      binary set varName formatSting ?arg arg ...?
    which would be something like "binary format" working on an existing bytearray.
    
  4. foundin changed to: "obsolete: 8.4.14"
  5. is_private changed to: "0"
  6. priority changed to: "5 Medium"
  7. resolution changed to: "None"
  8. status changed to: "Open"
  9. submitter changed to: "dvrsn"
  10. subsystem changed to: "12. ByteArray Object"
  11. title changed to: "cannot efficiently modify a ByteArray"
  12. type changed to: "RFE"