Rationale
Library structure
When designing a library it is always a problem to find a balance between generalization
and usability. A generic utility can have a wider range of usage with more options for extensibility,
but it can also bring unwanted complexity for everyday usage.
Imagine a library for drawing geometric objects. It can contain one generic function draw()
with many parameters specifying what to draw, like size, number of edges, shape etc.
This would allow you to draw almost anything, but usually a user only needs to draw
only a triangle or a square and she will have to specify this simple request in a
very complicated way.For this purpose two functions, draw_triangle()
and
draw_square()
, would suit much better then a generic draw()
function.
The String Algorithm Library solves this problem by dividing the interface into two layers.
The first layer (defined in the namespace boost) contains ready to use algorithms specialized
for common tasks. They are provided in multiple variants to better suit specific needs.
The second layer (defined in the namespace boost::string_algo
), provides generic interfaces with
more options for extending and tunning.
For instance, a boost::trim() algorithm trims spaces from
an input string. When there is a need to trim something else, there is
boost::string_algo::trim() which interface allows one to specify a
predicate which selects the characters to be removed.
Locales
Locales have a very close relation to string processing. They contain information about
the character sets and are used, for example, to change the case of characters and
to classify the characters.
C++ allows to work with multiple different instances of locales at once. If an algorithm
manipulates some data in a way that requires the usage of locales, there must be a way
to specify them. However, one instance of locales is sufficient for most of the applications,
and for a user it could be very tedious to specify which locales to use on every place
where it is needed.
Fortunately, the C++ standard allows to specify the global locales (using static member
function std:locale::global()
). When instantiating an
std::locale
class without explicit information, the instance will
be initialized with the global locale. It means, that if an algorithm needs a locale,
it should have an std::locale
parameter with default value std::locale()
.
If a user needs to specify locales explicitly, she can do so. Otherwise the global
locales are used.
Regular Expressions
Regular expressions are an essential part of text processing. For this reason, the library
provides also regex variants of some algorithms. The library does not try to replace
Boost.Regex, but it merely wraps its functionality in a new interface.
As a part of this library regex algorithms integrate smoothly with other components which
brings additional value.