3 // Copyright (C) 1994, 1999, 2000 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // 21.3.6.1 basic_string find
32 typedef std::string::size_type csize_type
;
33 typedef std::string::const_reference cref
;
34 typedef std::string::reference ref
;
35 csize_type npos
= std::string::npos
;
36 csize_type csz01
, csz02
;
38 const char str_lit01
[] = "mave";
39 const std::string
str01("mavericks, santa cruz");
40 std::string
str02(str_lit01
);
41 std::string
str03("s, s");
44 // size_type find(const string&, size_type pos = 0) const;
45 csz01
= str01
.find(str01
);
47 csz01
= str01
.find(str01
, 4);
48 test
&= csz01
== npos
;
49 csz01
= str01
.find(str02
, 0);
51 csz01
= str01
.find(str02
, 3);
52 test
&= csz01
== npos
;
53 csz01
= str01
.find(str03
, 0);
55 csz01
= str01
.find(str03
, 3);
57 csz01
= str01
.find(str03
, 12);
58 test
&= csz01
== npos
;
60 // An empty string consists of no characters
61 // therefore it should be found at every point in a string,
62 // except beyond the end
63 csz01
= str01
.find(str04
, 0);
65 csz01
= str01
.find(str04
, 5);
67 csz01
= str01
.find(str04
, str01
.size());
68 test
&= csz01
== npos
;
70 // size_type find(const char* s, size_type pos, size_type n) const;
71 csz01
= str01
.find(str_lit01
, 0, 3);
73 csz01
= str01
.find(str_lit01
, 3, 0);
76 // size_type find(const char* s, size_type pos = 0) const;
77 csz01
= str01
.find(str_lit01
);
79 csz01
= str01
.find(str_lit01
, 3);
80 test
&= csz01
== 3; // zero length string should be found at pos
82 // size_type find(char c, size_type pos = 0) const;
83 csz01
= str01
.find('z');
84 csz02
= str01
.size() - 1;
85 test
&= csz01
== csz02
;
86 csz01
= str01
.find('/');
87 test
&= csz01
== npos
;
89 // size_type find_first_of(const string&, size_type pos = 0) const;
90 std::string
str05("xena rulez");
91 csz01
= str01
.find_first_of(str01
);
93 csz01
= str01
.find_first_of(str01
, 4);
95 csz01
= str01
.find_first_of(str02
, 0);
97 csz01
= str01
.find_first_of(str02
, 3);
99 csz01
= str01
.find_first_of(str03
, 0);
101 csz01
= str01
.find_first_of(str03
, 3);
103 csz01
= str01
.find_first_of(str03
, 12);
105 csz01
= str01
.find_first_of(str05
, 0);
107 csz01
= str01
.find_first_of(str05
, 4);
110 // An empty string consists of no characters
111 // therefore it should be found at every point in a string,
112 // except beyond the end
113 // However, str1.find_first_of(str2,pos) finds the first character in
114 // str1 (starting at pos) that exists in str2, which is none for empty str2
115 csz01
= str01
.find_first_of(str04
, 0);
116 test
&= csz01
== npos
;
117 csz01
= str01
.find_first_of(str04
, 5);
118 test
&= csz01
== npos
;
120 // size_type find_first_of(const char* s, size_type pos, size_type n) const;
121 csz01
= str01
.find_first_of(str_lit01
, 0, 3);
123 csz01
= str01
.find_first_of(str_lit01
, 3, 0);
124 test
&= csz01
== npos
;
126 // size_type find_first_of(const char* s, size_type pos = 0) const;
127 csz01
= str01
.find_first_of(str_lit01
);
129 csz01
= str01
.find_first_of(str_lit01
, 3);
132 // size_type find_first_of(char c, size_type pos = 0) const;
133 csz01
= str01
.find_first_of('z');
134 csz02
= str01
.size() - 1;
135 test
&= csz01
== csz02
;
137 // size_type find_last_of(const string& str, size_type pos = 0) const;
138 // size_type find_last_of(const char* s, size_type pos, size_type n) const;
139 // size_type find_last_of(const char* s, size_type pos = 0) const;
140 // size_type find_last_of(char c, size_type pos = 0) const;
143 // from tstring.cc, from jason merrill, et. al.
145 std::string::size_type pos
;
146 pos
= x
.find_last_not_of('X');
148 pos
= x
.find_last_not_of("XYZ");
152 pos
= y
.find_last_not_of('X');
154 pos
= y
.find_last_not_of('a');
156 pos
= y
.find_last_not_of("XYZ");
158 pos
= y
.find_last_not_of("a");
162 pos
= z
.find_last_not_of('X');
164 pos
= z
.find_last_not_of("XYZ");
166 pos
= z
.find_last_not_of('b');
168 pos
= z
.find_last_not_of("Xb");
170 pos
= z
.find_last_not_of("Xa");
172 pos
= z
.find_last_of("ab");
174 pos
= z
.find_last_of("Xa");
176 pos
= z
.find_last_of("Xb");
178 pos
= z
.find_last_of("XYZ");
179 test
&= pos
== std::string::npos
;
180 pos
= z
.find_last_of('a');
182 pos
= z
.find_last_of('b');
184 pos
= z
.find_last_of('X');
185 test
&= pos
== std::string::npos
;