home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 7
/
FreshFishVol7.bin
/
bbs
/
gnu
/
libg++-2.6-bin.lha
/
GNU
/
info
/
libg++.info-3
(
.txt
)
< prev
next >
Wrap
GNU Info File
|
1994-02-27
|
50KB
|
1,048 lines
This is Info file libg++.info, produced by Makeinfo-1.55 from the input
file ./libg++.texi.
START-INFO-DIR-ENTRY
* Libg++:: The g++ class library.
END-INFO-DIR-ENTRY
This file documents the features and implementation of The GNU C++
library
Copyright (C) 1988, 1991, 1992 Free Software Foundation, Inc.
Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the section entitled "GNU Library General Public License" is
included exactly as in the original, and provided that the entire
resulting derived work is distributed under the terms of a permission
notice identical to this one.
Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that the section entitled "GNU Library General Public
License" and this permission notice may be included in translations
approved by the Free Software Foundation instead of in the original
English.
File: libg++.info, Node: String, Next: Integer, Prev: AllocRing, Up: Top
The String class
****************
The `String' class is designed to extend GNU C++ to support string
processing capabilities similar to those in languages like Awk. The
class provides facilities that ought to be convenient and efficient
enough to be useful replacements for `char*' based processing via the C
string library (i.e., `strcpy, strcmp,' etc.) in many applications.
Many details about String representations are described in the
Representation section.
A separate `SubString' class supports substring extraction and
modification operations. This is implemented in a way that user
programs never directly construct or represent substrings, which are
only used indirectly via String operations.
Another separate class, `Regex' is also used indirectly via String
operations in support of regular expression searching, matching, and the
like. The Regex class is based entirely on the GNU Emacs regex
functions. *Note Syntax of Regular Expressions: (emacs.info)Regexps,
for a full explanation of regular expression syntax. (For
implementation details, see the internal documentation in files
`regex.h' and `regex.c'.)
Constructors
============
Strings are initialized and assigned as in the following examples:
`String x; String y = 0; String z = "";'
Set x, y, and z to the nil string. Note that either 0 or "" may
always be used to refer to the nil string.
`String x = "Hello"; String y("Hello");'
Set x and y to a copy of the string "Hello".
`String x = 'A'; String y('A');'
Set x and y to the string value "A"
`String u = x; String v(x);'
Set u and v to the same string as String x
`String u = x.at(1,4); String v(x.at(1,4));'
Set u and v to the length 4 substring of x starting at position 1
(counting indexes from 0).
`String x("abc", 2);'
Sets x to "ab", i.e., the first 2 characters of "abc".
`String x = dec(20);'
Sets x to "20". As here, Strings may be initialized or assigned
the results of any `char*' function.
There are no directly accessible forms for declaring SubString
variables.
The declaration `Regex r("[a-zA-Z_][a-zA-Z0-9_]*");' creates a
compiled regular expression suitable for use in String operations
described below. (In this case, one that matches any C++ identifier).
The first argument may also be a String. Be careful in distinguishing
the role of backslashes in quoted GNU C++ char* constants versus those
in Regexes. For example, a Regex that matches either one or more tabs
or all strings beginning with "ba" and ending with any number of
occurrences of "na" could be declared as `Regex r =
"\\(\t+\\)\\|\\(ba\\(na\\)*\\)"' Note that only one backslash is needed
to signify the tab, but two are needed for the parenthesization and
virgule, since the GNU C++ lexical analyzer decodes and strips
backslashes before they are seen by Regex.
There are three additional optional arguments to the Regex
constructor that are less commonly useful:
`fast (default 0)'
`fast' may be set to true (1) if the Regex should be
"fast-compiled". This causes an additional compilation step that
is generally worthwhile if the Regex will be used many times.
`bufsize (default max(40, length of the string))'
This is an estimate of the size of the internal compiled
expression. Set it to a larger value if you know that the
expression will require a lot of space. If you do not know, do not
worry: realloc is used if necessary.
`transtable (default none == 0)'
The address of a byte translation table (a char[256]) that
translates each character before matching.
As a convenience, several Regexes are predefined and usable in any
program. Here are their declarations from `String.h'.
extern Regex RXwhite; // = "[ \n\t]+"
extern Regex RXint; // = "-?[0-9]+"
extern Regex RXdouble; // = "-?\\(\\([0-9]+\\.[0-9]*\\)\\|
// \\([0-9]+\\)\\|
// \\(\\.[0-9]+\\)\\)
// \\([eE][---+]?[0-9]+\\)?"
extern Regex RXalpha; // = "[A-Za-z]+"
extern Regex RXlowercase; // = "[a-z]+"
extern Regex RXuppercase; // = "[A-Z]+"
extern Regex RXalphanum; // = "[0-9A-Za-z]+"
extern Regex RXidentifier; // = "[A-Za-z_][A-Za-z0-9_]*"
Examples
========
Most `String' class capabilities are best shown via example. The
examples below use the following declarations.
String x = "Hello";
String y = "world";
String n = "123";
String z;
char* s = ",";
String lft, mid, rgt;
Regex r = "e[a-z]*o";
Regex r2("/[a-z]*/");
char c;
int i, pos, len;
double f;
String words[10];
words[0] = "a";
words[1] = "b";
words[2] = "c";
Comparing, Searching and Matching
=================================
The usual lexicographic relational operators (`==, !=, <, <=, >, >=')
are defined. A functional form `compare(String, String)' is also
provided, as is `fcompare(String, String)', which compares Strings
without regard for upper vs. lower case.
All other matching and searching operations are based on some form
of the (non-public) `match' and `search' functions. `match' and
`search' differ in that `match' attempts to match only at the given
starting position, while `search' starts at the position, and then
proceeds left or right looking for a match. As seen in the following
examples, the second optional `startpos' argument to functions using
`match' and `search' specifies the starting position of the search: If
non-negative, it results in a left-to-right search starting at position
`startpos', and if negative, a right-to-left search starting at
position `x.length() + startpos'. In all cases, the index returned is
that of the beginning of the match, or -1 if there is no match.
Three String functions serve as front ends to `search' and `match'.
`index' performs a search, returning the index, `matches' performs a
match, returning nonzero (actually, the length of the match) on success,
and `contains' is a boolean function performing either a search or
match, depending on whether an index argument is provided:
`x.index("lo")'
returns the zero-based index of the leftmost occurrence of
substring "lo" (3, in this case). The argument may be a String,
SubString, char, char*, or Regex.
`x.index("l", 2)'
returns the index of the first of the leftmost occurrence of "l"
found starting the search at position x[2], or 2 in this case.
`x.index("l", -1)'
returns the index of the rightmost occurrence of "l", or 3 here.
`x.index("l", -3)'
returns the index of the rightmost occurrence of "l" found by
starting the search at the 3rd to the last position of x,
returning 2 in this case.
`pos = r.search("leo", 3, len