On 15/09/2014, at 3:31 PM, srean wrote:

> I think rope would be an overkill for simple cases of concatenation.

But not for complex ones: I changed this from concatenation to
using += and reserve .. but the reserve is a guess and I haven't done
this everywhere.

  method fun make_frame (out:string) :string = {
    var o = "";
    reserve(&o,10000+out.len.int);
    var h = #(d.heading.get_headings);

    o+=menu_js;
    o+=menu_style;
    o+=#(d.heading.emit-js);
    o+=#(d.button-factory.get-jscript);
    o+=#(d.fileseq.get-jscript);

    // Whole page defaults and background
    o+='<div style="background-color:'+page_colour+'; font-family:sans-serif; 
color:'+text_colour+'">';

    // LEFT MARGIN
    // fixed top nav bar 
    o+='<div style="position:fixed; top:10px;  left:10px; width:'+
    (toc_width - 10).str+'px; height:30px;' +
    ' background-color:'+frame_colour+'; padding:4px;'+
    ' border-top-left-radius:10px; border-top-right-radius:10px">';
    o+='</div>\n';

    // left border
    o+='<div style="position:fixed; top:40px; bottom:40px; left:10px; 
width:4px;' +
      ' background-color:'+ frame_colour+ ';">';
    o+='</div>\n';

    // Contents body
    o+='<div style="position:fixed; top:48px; bottom:48px; left:14px; '+
       '  width:'+(toc_width - 10).str+'px;'+
       ' border:4px;overflow:auto;'+
       ' font-family:sans-serif; color:#404040; background-color:#70E0E0;">\n';
   
    proc head1(s:string,i:int) { 
      o+= """<div class=m1 onclick="mshow('menu"""+ 
counter.str+"""','#"""+s+"""_h')" """;
      o+=" onmouseover='hilite(this)' onmouseout='leave1(this)' >";
      o+= s+"</div>\n";
      o+= """<div class=sm id=menu"""+counter.str+""">\n""";
    }
    proc foot1() { o+="</div>\n"; } 
    proc break1(s:string,i:int) {foot1(); ++counter; head1(s,i); }
  
    var counter = 0;
    iter proc (i:int,s:string) 
      { 
        //println$ i,s;
        if i == 1 do
          if counter == 0 do ++counter; head1 (s,i); 
          else break1 (s,i); 
          done
        elif i == 2 do
          o+="<div class=m2 onmouseover='hilite(this)'";
          o+=" onmouseout='leave2(this)'";
          o+=""" 
onclick='window.location.replace("#"""+s+"""_h")'>"""+s+"""</div>\n""";
        done
      } 
      h
    ;
    if counter > 1 do  foot1; done;
    o+="<script>counter_max="+counter.str+";</script>\n";
     // right border
    o+='<div style="position:fixed; top:40px; bottom:40px; left:'+(toc_width + 
4).str+'px; width:4px;' +
      ' background-color:'+ frame_colour+ ';">';
    o+='</div>';

    // fixed bottom nav bar
    o+='<div style="position:fixed; bottom:10px;  left:10' +
      'px; width:'+(toc_width - 10).str+'px; height:30px;' +
    ' background-color:'+frame_colour+';padding:4px;'+
    ' border-bottom-left-radius:10px; border-bottom-right-radius:10px"\n>';
    o+='</div>\n';

    // MAIN CONTENT
    // fixed top nav bar 
    o+='<div style="position:fixed; top:10px;  left:'+(toc_width+10).str+'px; 
right:10px; height:30px;' +
    ' background-color:'+frame_colour+'; padding:4px;'+
    ' border-top-left-radius:10px; border-top-right-radius:10px">';
    o+=#(d.heading.emit-buttons);
    o+=#(d.fileseq.shownav);
    o+='</div>\n';

    // left border
    o+='<div style="position:fixed; top:40px; bottom:40px; 
left:'+(toc_width+10).str+'px; width:4px;' +
      ' background-color:'+ frame_colour+ ';">';
    o+='</div>';

    // body
    o+='<div style="position:fixed; top:48px; bottom:48px; 
left:'+(toc_width+14).str+
       'px; right:14px; padding:8px;'+
    ' border:4px;overflow:auto; font-family:sans-serif;'+
    ' color:'+text_colour+'; background-color:'+page_colour+';">';
    o+=out;
    o+='</div>\n';

     // right border
    o+='<div style="position:fixed; top:40px; bottom:40px; right:10px; 
width:4px;' +
      ' background-color:'+ frame_colour+ ';">';
    o+='</div>';

    // fixed bottom nav bar
    o+='<div style="position:fixed; bottom:10px;  left:'+(toc_width+10).str+
      'px; right:10px; height:30px;' +
    ' background-color:'+frame_colour+';padding:4px;'+
    ' border-bottom-left-radius:10px; border-bottom-right-radius:10px"\n>';
    o+=#(d.fileseq.shownav);
    o+='</div>\n';

    o+='</div>'; // whole page end
    return o;
  }


> Concatenation using the "grow by 1.x times and copy" would be half decent, 
> but only half decent. Isn't there a "+= "operator for C++ strings or an 
> "append" that does that already. One can go the expression templates route, 
> but again that is more complicated.

Actually it isn't:

        reduce concat3 (x:string, y:string, z:string) : x + y + z => cat3 (x,y);

That will reduce

        x + y + z + a + b => cat3 (cat3 (x,y,z),a,b);

which is a lot better than binary. Slows down the compiler though.

> fgets interface leads to copying _at_least_ twice
> 
> (i) copying from the buffer in C's stdlib to the buffer provided to fgets as 
> an arg
> ii) copying the contents of the arg for concatenation

No, (ii) should be "making a C++ string from a C string".
Any concatenation or whatever is on top of that again.

> 
> If one uses raw read then one can reduce one copy,

How? You still have to copy stuff out of the raw buffer into the
line buffer. And if the line buffer isn't big enough you have
to reallocate it. I'm assuming the C library fgets can do this
better than I can.

The bottom line is that immutability helps.
If you have that, functional strings seem a good idea too.

Python uses the short string optimisation too (the first so many characters
are stored in the object. If the string gets longer, they're replace by a 
pointer
to a heap allocated string).

I don't know how it does substrings though.

--
john skaller
skal...@users.sourceforge.net
http://felix-lang.org




------------------------------------------------------------------------------
Want excitement?
Manually upgrade your production database.
When you want reliability, choose Perforce
Perforce version control. Predictably reliable.
http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to