- Notifications
You must be signed in to change notification settings - Fork5
2015 004 Addition of Buffer module
Author: Ken Friis Larsen
Last revised: August 11, 2015
Status: proposed
Discussion:issue #4
signature BUFFER =sigtypebufval new : int -> bufval contents : buf -> stringval size : buf -> intval clear : buf -> unitval reset : buf -> unitval addChar : buf -> char -> unitval addString : buf -> string -> unitval addSubString : buf -> substring -> unitendstructure Buffer :> BUFFER
type bufA type of mutable string buffers that allows efficientconcatenation at the end and automatically expand as necessary. Itprovides accumulative concatenation of strings in quasi-linear time(instead of quadratic time when strings are concatenated pairwise).
new hintcreates a new empty buffer. RaisesSizeifhint <= 0orhint > String.maxSize.The argument hint is used as the initial size of the internalstring that holds the buffer contents. The internal string isautomatically reallocated as contents is stored in the buffer. Forbest performance, hint should be of the same order of magnitude asthe number of characters that are expected to be stored in thebuffer (for instance, 80 for a buffer that holds one output line).Nothing bad will happen if the buffer grows beyond that limit,however. In doubt, takehint = 16for instance.contents bufreturns the contents ofbuf.size bufreturns the size of the contents ofbuf.clear bufemptysbuf.reset bufemptysbufand shrink the internal string to the initial hint.addChar buf cappendscat the end ofbuf.addString buf sappendssat the end ofbuf.addSubString buf ssappendsssat the end ofbuf.
Generating strings efficiently is needed for many programming tasks. It is possible to implement this module with other parts of the Basis Library, however to get top performance you need to use the internal representation of strings (for some systems at least).
This module is shipped as part ofMoscow ML's library, since version 2.10.
Adopting this proposal should not affect existing programs.
This module is a natural candidate for inclusion in the Basis Library. Similar structureshave been found to be useful in other programming languages, for instanceStringBuilder in Java and theBuffer module in O'Caml (the original inspiration for this module).
- [2015-08-11] Proposed
