cppannotations/annotations/yo/string/ops.yo
Frank B. Brokken 75d04f6683 typos
2016-11-08 21:54:15 +01:00

87 lines
4.7 KiB
Text

Some of the operations that can be performed on strings return indices within
the strings. Whenever such an operation fails to find an appropriate index,
the em(value) hi(npos)tt(string::npos) is returned. This value is a symbolic
value of type hi(size_type)ti(string::size_type), which is (for all practical
purposes) an (tt(unsigned)) tt(int).
All tt(string) members accepting tt(string) objects as arguments also accept
tt(char const *) (NTBS) arguments. The same usually holds true
for operators accepting tt(string) objects.
Some tt(string)-members use em(iterators). Iterators are formally introduced
in section ref(ITERATORS). Member functions using iterators are listed in
the next section (ref(STRINGOVERVIEW)), but the iterator concept itself
is not further covered by this chapter.
Strings support a large variety of members and operators. A short overview
listing their capabilities is provided in this section, with subsequent
sections offering a detailed discussion. The bottom line: bf(C++) strings are
extremely versatile and there is hardly a reason for falling back on the bf(C)
library to process text. bf(C++) strings handle all the required memory
management and thus memory related problems, which are the #1 source of
problems in bf(C) programs, can be prevented when bf(C++) strings are
used. Strings do come at a price, though. The class's extensive capabilities
have also turned it into a beast. It's hard to learn and master all its
features and in the end you'll find that not all that you expected is actually
there. For example, tt(std::string) doesn't offer case-insensitive
comparisons. But in the end it isn't even as simple as that. It em(is) there,
but it is somewhat hidden and at this point in the annotations() it's too
early to study into that hidden corner yet. Instead, realize that bf(C)'s
standard library em(does) offer useful functions that can be used as long as
we're aware of their limitations and are able to avoid their traps. So for
now, to perform a traditional i(case-insensitive) comparison of the contents
of two tt(std::string) objects tt(str1) and tt(str2) the following will do:
hi(strcasecmp)
verb(
strcasecmp(str1.c_str(), str2.c_str());
)
Strings support the following functionality:
itemization(
ittq(initialization)
(when string objects are defined they are always properly
initialized. In other words, they are always in a i(valid state). Strings may
be initialized empty or already existing text can be used to initialize
strings.)
ittq(assignment)
(strings may be given new values. New values may be assigned using
member functions (like tt(assign)) but a plain assignment operator (i.e.,
tt(=))may also be used. Furthermore, assignment em(to) a character buffer is
also supported.)
ittq(conversions)
(the partial or complete contents of string objects may be interpreted
as bf(C) strings but the string's contents may also be processed as a series
of raw binary bytes, not necessarily terminating in a 0-valued
character. Furthermore, in many situations plain characters and bf(C) strings
may be used where tt(std::string)s are accepted as well.)
ittq(breakdown)
(the individual characters stored in a string can be accessed
using the familiar index operator (tt([])) allowing us to either access or
modify information in the middle of a string.)
ittq(comparisons)
(strings may be compared to other strings (NTB strings) using the
familiar logical comparison operators tt(==, !=, <, <=, >) and tt(>=). There
are also member functions available offering a more fine-grained comparison.)
ittq(modification)
(the contents of strings may be modified in many ways. Operators are
available to add information to string objects, to insert information in
the middle of string objects, or to replace or erase (parts of) a
string's contents.)
ittq(swapping)
(the string's swapping capability allows us in principle to exchange
the contents of two string objects without a byte-by-byte copying
operation of the string's contents.)
ittq(searching)
(the locations of characters, sets of characters, or series of
characters may be searched for from any position within the string object
and either searching in a forward or backward direction.)
ittq(housekeeping)
(several housekeeping facilities are offered: the string's length,
or its empty-state may be interrogated. But string objects may also be
resized.)
ittq(stream I/O)
(strings may be extracted from or inserted into streams. In addition
to plain string extraction a line of a text file may be read without running
the risk of a buffer overrun. Since extraction and insertion operations are
stream based the I/O facilities are em(device independent).)
)