Handling EXTREMELY large string

ساخت وبلاگ
To debug what is going on in my complex scripts I turn on an option which causes the scripts to append trace information to a global variable which is then dumped out as part of the generated web page. For example:

PHP Code:

    if ($pdo->query($query))
    {
        if (
$debug)
            
$warn .= "<p>query='$query'</p>n";
    ...
    
showTrace(); 

where showTrace is:

PHP Code:

<?php
$warnid        
'';
function 
showTrace()
{
    global 
$warn;
    global 
$warnid;

    if (
strlen($warn) > 0)
    {
?>
    <div id='debugTrace<?php print $warnid?>' class='warning'>
      <?php print $warn?>
    </div>
<?php
    
if ($warnid == '')
        
$warnid    1;
    else
        
$warnid++;
    
$warn    '';
    }            
// output trace and warning information 
}        // showTrace

The class='warning' causes all of the text to be displayed in dark yellow.

Occasionally however this mechanism blows up because the string $warn becomes so big that it causes the storage limit to be exceeded and the script aborts without generating any output.

I am soliciting advice on techniques to address this issue:

  1. I don't know how PHP implements string concatenation internally. I presume that long strings use a block of storage which leaves some space for expansion. In this situation only a concatenation which overflows the reserved space requires copying the existing value of the string to a new location. However concatenating one character on to a 100MB string could, potentially, require allocating a new 200MB piece of storage and copying the 100MB + 1B value. Therefore should I change the implementation of $warn to be a linked list of strings? Or should I change it to use a file?
  2. I know there is not currently any way to overload the concatenate operator for a class so I will have to replace every occurrence of "$warn .= $something;" with "$warn->add($something);". This is a pain because there are tens of thousands of these concatenates throughout my code, and it cannot be addressed just by a simple regular expression invoked by PERL because sometimes the $something runs on for several lines and I not only have to replace all occurrences of "$warns*=s*" with "$warn->add(" but also of the next semicolon with ");".
  3. It would be nice if PHP defined a StringAccess interface (technically abstract class) like the existing ArrayAccess interface. Implementing ArrayAccess permits a class instance to overload array subscripting in four situations: l-value, r-value, isset, and unset. PHP only supports the magic function __string which permits a class to emulate a string as an r-value.

In case you are unfamiliar with the terminology l-value is an expression that can appear on the left hand side of an assignment while r-value is an expression that can appear on the right hand side of an assignment. In PHP, for example, a function which returns a reference can be either an r-value or an l-value while a function which returns a value can only be an r-value. See https://en.wikipedia.org/wiki/L-value

To see this functionality in action go to https://www.jamescobban.net/FamilyTr...ex.php?debug=Y
CodingForums...
ما را در سایت CodingForums دنبال می کنید

برچسب : نویسنده : codingforums بازدید : 146 تاريخ : جمعه 29 دی 1396 ساعت: 20:42