Boo0ns / arduino

Automatically exported from code.google.com/p/arduino
0 stars 0 forks source link

String class improvements #468

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Placing these into the issue tracker, so they won't get lost.

More info available here:

http://www.pjrc.com/teensy/string_class_experimental.html

Here's the summary of changes (from dev mail list, Jan 19)

Several functions were added to accept primitive types (int, long, char, etc) 
as right hand side parameters.  That solves a lot of inefficient allocation 
patterns.

The concat operator "+" uses references to pass its left hand side input to its 
output, of course modifying it along the way.  Keeping a "same object" path 
allows a chain of + operators to work nicely.  Together with left side 
converted on the stack, realloc's ability to grow the memory in place is 
leveraged.  An inherited class is used, where the inheritance rules prevent the 
compiler from modifying the left-most input, but allows the final result to be 
used seamlessly.

Compiler optimization and rvalue refs improve efficiency for assignments.  
Please understand these optimizations apply to the results of functions and 
expressions, not the operators.  These optimizations are enabled by adding 2 
lines in Compiler.java.

An updated malloc() is included.  The malloc() shipped with Arduino has bugs.

All string size changes use realloc.  Never is a buffer copied if realloc can 
change the space allocation.

Compare and other functions that had read-base buffer overflows are fixed.

Replace and other functions were rewritten to be more efficient.

Search functions that previously created many temporary objects were rewritten 
to work only within the original string.

Write-based functions modify the original string.  That's far more efficient in 
the common case where only the result is wanted.  This is the only API change 
that should be really visible.

A few new functions were added.

Brian Cook's "no overhead" flash string, as modified by Mikal Hart for the 
FlashString library, was included as a constructor and in a limited number of 
important left side cases.

Many tests were added to the Arduino Test Suite.  Improvements in how the test 
suite measures memory usage and CPU time were made.

But most importantly, MANY hours of testing were performed, not just for 
correct behavior, but also for reasonably efficient memory allocation patterns. 
 While I did make a few tiny tweaks to the API, this is designed to be a 
drop-in replacement.  It's a huge improvement in quality over what's currently 
in 0022. 

Original issue reported on code.google.com by paul.sto...@gmail.com on 29 Jan 2011 at 12:34

Attachments:

GoogleCodeExporter commented 9 years ago
Paul's implementation was the basis for the updated String class in Arduino 
1.0.  I made some changes though, so it's possible that not everything made it 
as it should have been.  Still, I'm marking this as fixed - outstanding 
problems should probably be entered as new issues.

Original comment by dmel...@gmail.com on 2 Dec 2011 at 9:55