Changes for FXString

A number of changes have been made for the FXString class.

Embedded nulls in strings

Strings can now contain embedded null bytes. One consequence of this change is that the length of the string returned by the length() member function is not necessarily the same result that you'll get by calling the C library's strlen() function on that string's text, since strlen() considers a null value the end of the string.

before() and after()

The behavior of the before() and after() member functions has changed in an attempt to simplify their use. As an illustration, the behavior of before() went something like this:

  • before() returned the contents of a string up to the nth occurrence of a specified search character.

  • If n was positive, the search was performed from the beginning of the string. If no occurences of the search character were found, before() returned the entire string.

  • If n was negative, the search started at the end of the string. If no occurrences of the search character were found, before() returned the empty string.

  • If n was identically zero, before() returned an empty string.

The after() member function worked like this:

  • after() returned the contents of a string after the nth occurrence of a specified search character.

  • If n was positive, the search was performed from the beginning of the string. If no occurences of the search character were found, after() returned the empty string.

  • If n was negative, the search started at the end of the string. If no occurrences of the search character were found, after() returned the entire string.

  • If n was identically zero, after() returned an empty string.

To simplify things, each of these member functions has been split into two different functions. The before() and after() member functions still work the same as they did in FOX 1.0 for positive values of n, but for values of n less than or equal to zero, before() now returns the empty string and after() returns the entire string. To handle the less common case of starting the search at the end of the string, use the new rbefore() and rafter() member functions.

Simplifying whitespace in strings

The new simplify() member function "simplifies" a string by compressing runs of whitespace down to a single space. All of the leading whitespace (i.e. up to the first non-space character) is removed, as is all of the trailing whitespace. So, for example:

// This string has lots of extra spaces...
FXString greetings = "   Hello  There!    ";

// Simplify it...
greetings.simplify();

// Now the string contains the simplified text "Hello There!" (with no extra spaces anywhere)

Handling embedded escape codes

The new escape() and unescape() functions can be used to escape (or unescape) special characters in a string. This is useful for working with strings that contain embedded control characters (e.g. tabs and newlines), backslashes, and so on. For example, this string:

FXString nasty = "Now is the time\nfor all good men\nto come to the aid\nof their country.";

contains four embedded newline characters. If you were to print this string directly to a file (or the screen) you would get four lines of text:

Now is the time
for all good men
to come to the aid
of their country.

However, if you were to print the results of "escaping" this string, i.e.

FXString happy = escape(nasty);

you would instead see all the output on one line and with the newline codes escaped, e.g.

Now is the time\nfor all good men\nto come to the aid\nof their country.

Other miscellaneous changes

  • The overloaded size() member function, which was used to query or set the capacity of a string, has been removed.

  • The new swap() function can be used to efficiently swap the contents of two strings.

  • The overloaded findf() and findb() member functions, used for searching strings for characters and substrings, have been renamed to find() and rfind(), respectively. The new names are consistent with those used by the standard C++ library's std::string class.

  • The new overloaded find_first_of(), find_last_of(), find_first_not_of() and find_last_not_of() member functions can be used to find the first (or last) occurrence of any of a set of characters in a string. They serve the same purpose as identically-named functions in the standard C++ library's std::string class. Note that these are different from the find() and rfind() member functions, which search for a single character or a (contiguous) substring of characters in a string; the new member functions will match on any of the character(s) in the search set.

  • Added the overloaded assign() member function, which makes it slightly easier to assign new text to an FXString.

  • The new, overloaded contains() member function returns the number of occurrences of some character or substring in a string.

  • The new, overloaded substitute() member function replaces the first (or all) occurrences of some substring with another.

  • The overloaded extract() member function has been replaced with the more flexible section().