EXT C++ Library - parse

The ext::parse function template provides mapping of type to the most suitable C standard library conversion function. The std::string and std::wstring versions are available for all intrinsic types.


Interface:

template <typename T>
T ext::parse (const std::string &);

template <typename T>
T ext::parse (const std::wstring &);

// specializations follows

template <> bool parse <bool> (const std::string &);
template <> bool parse <bool> (const std::wstring &);

template <> char parse <char> (const std::string &);
template <> char parse <char> (const std::wstring &);

template <> wchar_t parse <wchar_t> (const std::string &);
template <> wchar_t parse <wchar_t> (const std::wstring &);

template <> signed char parse <signed char> (const std::string &);
template <> signed char parse <signed char> (const std::wstring &);
template <> signed short parse <signed short> (const std::string &);
template <> signed short parse <signed short> (const std::wstring &);
template <> signed int parse <signed int> (const std::string &);
template <> signed int parse <signed int> (const std::wstring &);
template <> signed long parse <signed long> (const std::string &);
template <> signed long parse <signed long> (const std::wstring &);
template <> signed long long parse <signed long long> (const std::string &);
template <> signed long long parse <signed long long> (const std::wstring &);

template <> unsigned char parse <unsigned char> (const std::string &);
template <> unsigned char parse <unsigned char> (const std::wstring &);
template <> unsigned short parse <unsigned short> (const std::string &);
template <> unsigned short parse <unsigned short> (const std::wstring &);
template <> unsigned int parse <unsigned int> (const std::string &);
template <> unsigned int parse <unsigned int> (const std::wstring &);
template <> unsigned long parse <unsigned long> (const std::string &);
template <> unsigned long parse <unsigned long> (const std::wstring &);
template <> unsigned long long parse <unsigned long long> (const std::string &);
template <> unsigned long long parse <unsigned long long> (const std::wstring &);

template <> float parse <float> (const std::string &);
template <> float parse <float> (const std::wstring &);
template <> double parse <double> (const std::string &);
template <> double parse <double> (const std::wstring &);
template <> long double parse <long double> (const std::string &);
template <> long double parse <long double> (const std::wstring &);

Details:

Parsing of bool type follows these rules:

  1. Empty string or string consisting of only whitespaces is converted to false.
  2. Trimmed string that is case-insensitive equal to "false" is converted to false.
  3. String that can be converted to integral number equal to zero is converted to false.
  4. String that can be converted to floating-point number equal to zero is converted to false.
  5. Otherwise the string is converted to true.

When converting to char or wchar_t, the first character of the string is used if any. The ext::common_echo is used for conversion if the character-type of the string and the target type are not the same.

Otherwise the appropriate C library conversion function is used:

Examples:

#include <ext/c++>

template <typename T>
T ask_for () {
    char tmp [64];
    std::fgets (tmp, 64, stdin);
    return ext::parse <T> (tmp);
};

int main () {
    std::printf ("Enter count: ");
    unsigned int n = ask_for <unsigned int> ();

    std::printf ("Enter value: ");
    float f = ask_for <float> ();

    // ...

    return 0;
};

Remarks

Note that the declarations presented in interface section above is actually expanded using preprocessor macros in header file.

Due to automatic conversion from string literals to appropriate std::string or std::wstring you can also write expressions like: ext::parse <T> ("123.456");


[index]