#include <basic_substring.h>
Public Types | |
| typedef basic_string<Ch>::size_type | size_type |
| The string's size_type. | |
Public Methods | |
Constructors. | |
| basic_substring (basic_string<Ch>& s, size_type start, size_type len, bool doReverse=false) | |
| Manual constructor. More... | |
| basic_substring (basic_string<Ch>& s, const basic_string<Ch>& s2, bool doReverse=false) | |
| String constructor. More... | |
| basic_substring (basic_string<Ch>& s, const Ch* p, bool doReverse=false) | |
| Character string constructor. More... | |
Assignment operators. | |
The assignment operators will modify the original string this substring was constructed from.
The substring will attempt to find the next substring matching the original substring.
If this substring is invalid, nothing happens (not even a thrown exception). This runs counter to the original idea in Stroustrup's book. I view it as an enhancement. Avoid infinite loops with the 'bool' operator. | |
| basic_substring& | operator= (const basic_string<Ch>& in) |
| Assigns a string to the substring. More... | |
| basic_substring& | operator= (const basic_substring<Ch>& in) |
| Assigns a substring to this substring. More... | |
| basic_substring& | operator= (const Ch* in) |
| Assigns a set of characters to the substring. More... | |
| basic_substring& | operator= (Ch in) |
| Assigns a character to the substring. More... | |
Inserts text before or after the substring. | |
This does not advance the substring.
Use the iterators afterwards. | |
| basic_substring& | prepend (const basic_string<Ch>& in) |
| Inserts the text of the string before the substring. More... | |
| basic_substring& | prepend (const Ch* in) |
| Inserts the text of the string before the substring. More... | |
| basic_substring& | append (const basic_string<Ch>& in) |
| Inserts the text of the string after the substring. More... | |
| basic_substring& | append (const Ch* in) |
| Inserts the text of the string after the substring. More... | |
Casting operators. | |
| operator const basic_string () const | |
| Acquires the text of the substring. More... | |
| operator const bool () const | |
| Acquires the state of the substring. More... | |
Iterator operators. | |
When the iterator operator reaches the end, the substring becomes invalid.
Once a substring becomes invalid, it will always remain invalid. You cannot reverse-iterate back to a valid substring. | |
| basic_substring& | operator++ () |
| basic_substring& | operator++ (int) |
| basic_substring& | operator-- () |
| basic_substring& | operator-- (int) |
This is inspired by Stroustrup's example in Section 20.3.13 of his "The C++ Programming Language". In it, he describes a substring class that's a little lighter than this one. He also mentioned that it might be more interesting to create a regular expression class along the same line (perhaps a project for another day). I thought it might be useful, so I extended it.
Basically, use the constructors to create a new substring. If you add a 'true' to the list of parameters, the substring will work in reverse (starting at the end, and working its way towards the beginning).
You may iterate to the next matching substring using either of the iterator operators(post or prefix,decrement or increment). The iterators will not modify the actual string.
The only way to actually modify a string through the substring is to assign a string to the substring. This causes the substring to replace the substring in the string with whatever you assigned, and iterate the substring to the next matching substring.
When you iterate past the end, the substring becomes invalid, and is no longer useful. You may, however, assign another substring to it (assigning substrings to a substring acts as a copy, and does not modify the string).
You may use the string operator to acquire the text of the substring.
You may also use the bool operator to test whether the substring is valid or not.
Some examples:
#include "basic_substring.h" using namespace std; using namespace tvr_std; string someText("I'm a little teapot, short and stout."); cout << someText << endl; substring sub(someText, "little"); sub = "gaudy"; sub = substring(someText, "short"); sub = "gaudy"; sub = substring(someText, "stout"); sub = "gaudy"; // By now, the string is "I'm a gaudy teapot, gaudy and gaudy.". cout << someText << endl; sub = substring(someText, "gaudy"); while (sub) { // uses the bool operator to check for validity sub = "little"; } // Now, the string is "I'm a little teapot, little and little." cout << someText << endl; sub = substring(someText, "little", true); // reverse substring. sub = "stout"; sub = "short"; // And so, we should be back to "I'm a little teapot, short and stout." cout << someText << endl;
|
||||||||||
|
Manual constructor. Lets you manually set up the substring.
|
|
||||||||
|
String constructor. Lets you create a substring based on another string.
|
|
||||||||
|
Character string constructor. Lets you create a substring based on the base character string type.
|
|
||||
|
Inserts the text of the string after the substring.
|
|
||||
|
Inserts the text of the string after the substring.
|
|
||||
|
Acquires the text of the substring. In case you forgot. |
|
||||
|
Acquires the state of the substring.
|
|
||||
|
Assigns a character to the substring. This replaces the contents of the substring with a single character. This iterates to the next substring thereafter.
|
|
||||
|
Assigns a set of characters to the substring. This replaces the contents of the substring with a new string, iterating to the next substring.
|
|
||||
|
Assigns a substring to this substring. This completely changes the original state of the substring. This doesn't merely take the incoming substring's 'string' and assign it to this substring, this changes the internal state to match that of the incoming string. Use this operator to reuse substring variables.
|
|
||||
|
Assigns a string to the substring. This replaces the contents of the substring with a new string, iterating to the next substring.
|
|
||||
|
Inserts the text of the string before the substring.
|
|
||||
|
Inserts the text of the string before the substring.
|
1.2.2 written by Dimitri van Heesch,
© 1997-2000