General String Quick Overview
Last Updated 2001.10.16
The General String library was inspired by the Icon programming language
string manipulation features. This library has aGNU
license, i.e. it is free. I encourage you to get acquainted with the GNU license
and support it if you agree with it.
Icon has two interesting features that are very useful in string manipulation:
- Generators: A construct that can generate a sequence of values, and fails when no more values are available. This is somewhat similar to Java's Iterator. This General String library supports generators through the IGeneratorinterface.
- Backtracking: An operation can consist of two (or more) generators associated together, e.g. through an operator, if the second generator fails, we backtrack to the first generator and retry the operation again. Think of a database transaction to help you understand backtracking, although they are different. This General String library supports backtracking through the operator classes in the gstring.operators package.
Next is a simple example to show the elegance of these concepts. Suppose that you want to parse a string to find the indexes of vowels:
GString source = new GString("Hello world"); // String to be parsed
SetChar target = new SetChar("aeiou"); // Vowels
IGenerator g = source.doGenerate().find().generator(target); // a find generator
Variant v; // a variant to hold generated positions
// loop until no more generations
v= g.next(); // get next position
if (g.ok()) // if successful generation then print position
} while (g.ok());
The above example will print:
Here are some pointers to get more information about the Icon