Fstring
The Fstring class provides Fortran-compatible single-byte ASCII character strings:
- Fixed length after construction
- Character positions indexed from 1 to the string length
- Substring support
- Concatenation support
- Equality comparisons ignore trailing spaces
There are a range of Fstring constructors, for example:
Fstring first( "Tom" ); // C string
Fstring initial( 'E' ); // char Fstring street( the_street ); // std::string Fstring essay( 500 ); // Length Fstring schedule( 1000, init ); // Length + Initializer |
Fstring can be initialized in complex ways at the time of construction, as in Fortran DATA statements, by an initializer function that is passed after a length argument. Initializers are any void functions that take a non-const reference to an Fstring.
Single characters in an Fstring are indexed starting at 1 using the [] operator:
first[1] = 'D'; // Tom -> Dom |
Substrings are generated using a start and end index or just a start index:
first(1,2) = "Sa"; // Dom -> Sam first(2) = "id"; // Sam -> Sid |
The use of a single start index as in the second example generates a tail substring starting at the specified index. Substrings are active windows onto the real string as in Fortran, not copies of the substring as obtained with std::string::substr. Substrings can be used as in Fortran except that to pass a substring to a non-const "Fstring &" argument you must use the ref member function in the function call: first(1,2).ref().
Concatenation is done with the + operator, as in std::string:
Fstring full_name( first + ' ' + last ); |
Zero-length Fstrings are supported but since they have no valid index positions indexing and substring operations cannot be performed on them.
Fstrings can interoperate with C++ std::string, providing both construction from and implicit conversion to std::string. As in std::string, member functions data, c_str, and copy are provided that return Fstring-managed C string representations. A t_str function that returns a trailing space trimmed copy in an Fstring-managed C string is also provided.
Numeric conversion functions are provided in member and non-member versions:
- The Fstring_of( number ) non-member functions provide a range of conversion to Fstring.
- The is_type< numeric_type >( Fstring ) non-member and the Fstring::is_type< numeric_type > member function templates tests whether an Fstring is a valid representation of a specified numeric type. Non-template wrapper functions for the common numeric types are provided for convenience: is_int, is_double, etc.
- The type_of< numeric_type >( Fstring ) non-member and the Fstring::type_of< numeric_type > member function templates convert an Fstring to a specified numeric type. Non-template wrapper functions for the common numeric types are provided for convenience: int_of, double_of, etc.
The Fstring implementation includes a large number of member and non-member functions: length, predicates, searching, case-changing, and Fortran intrinsic functions. See the Fstring source to explore the full capabilities.
Note: The CHAR and ACHAR functions that return a length-1 Fstring are wrapped in an extra Fortran namespace layer to prevent conflict with Windows® CHAR type and to disambiguate these versions from the variants returning char provided by char.functions.hh. To make their short names visible add these lines to your local scope:
using ObjexxFCL::Fortran::CHAR;
using ObjexxFCL::Fortran::ACHAR;
|