scnlib  0.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Scanning API

Core part of the public scanning API. More...

Functions

template<typename Range , typename Format , typename... Args>
auto scn::scan (Range &&r, const Format &f, Args &...a) -> detail::scan_result_for_range_t< Range >
 The most fundamental part of the scanning API. More...
 
template<typename Locale , typename Range , typename Format , typename... Args>
auto scn::scan_localized (const Locale &loc, Range &&r, const Format &f, Args &...a) -> detail::scan_result_for_range_t< Range >
 Read from the range in r using the locale in loc. More...
 
template<typename Range , typename... Args>
auto scn::scan (Range &&r, detail::default_t, Args &...a) -> detail::scan_result_for_range_t< Range >
 Equivalent to scan, but with a format string with the appropriate amount of space-separated "{}"s for the number of arguments. More...
 
template<typename T , typename Range >
auto scn::scan_value (Range &&r) -> detail::scan_result_for_range_t< Range, expected< T >>
 Scans a single value with the default options, returning it instead of using an output parameter. More...
 
template<typename Range , typename Format , typename... Args>
auto scn::scanf (Range &&r, const Format &f, Args &...a) -> detail::scan_result_for_range_t< Range >
 Otherwise equivalent to scan, except it uses scanf-like format string syntax, instead of the Python-like default one. More...
 
template<typename Format , typename... Args, typename CharT = detail::ranges::range_value_t<Format>>
auto scn::input (const Format &f, Args &...a) -> detail::scan_result_for_range_t< decltype(stdin_range< CharT >())>
 Otherwise equivalent to scan, expect reads from stdin. More...
 
template<typename Format , typename... Args, typename CharT = detail::ranges::range_value_t<Format>>
auto scn::prompt (const CharT *p, const Format &f, Args &...a) -> detail::scan_result_for_range_t< decltype(stdin_range< CharT >())>
 Equivalent to input, except writes what's in p to stdout. More...
 
template<typename T , typename CharT >
expected< const CharT * > scn::parse_integer (basic_string_view< CharT > str, T &val, int base=10)
 Parses an integer into val in base base from str. More...
 

Detailed Description

Core part of the public scanning API.

Generally, the functions in this group take a range, a format string, and a list of arguments. The arguments are parsed from the range based on the information given in the format string.

The range is described further here: Input range.

If the function takes a format string and a range, they must share character types. Also, the format string must be convertible to basic_string_view<CharT>, where CharT is that aforementioned character type.

The majority of the functions in this category return a scan_result<Range, result<ptrdiff_t>>, which has the following member functions:

Function Documentation

template<typename Format , typename... Args, typename CharT = detail::ranges::range_value_t<Format>>
auto scn::input ( const Format &  f,
Args &...  a 
) -> detail::scan_result_for_range_t<decltype(stdin_range<CharT>())>

Otherwise equivalent to scan, expect reads from stdin.

Character type is determined by the format string.

Does not sync with the rest <cstdio> (like std::ios_base::sync_with_stdio(false)). To use <cstdio> (like fread or fgets) with this function, call cstdin().sync() or wcstdin().sync() before calling <cstdio>.

Definition at line 243 of file scan.h.

template<typename T , typename CharT >
expected<const CharT*> scn::parse_integer ( basic_string_view< CharT >  str,
T &  val,
int  base = 10 
)

Parses an integer into val in base base from str.

Returns a pointer past the last character read, or an error. str can't be empty, and cannot have:

  • preceding whitespace
  • preceding "0x" or "0" (base is determined by the base parameter)
  • + sign (- is fine)

Definition at line 306 of file scan.h.

template<typename Format , typename... Args, typename CharT = detail::ranges::range_value_t<Format>>
auto scn::prompt ( const CharT *  p,
const Format &  f,
Args &...  a 
) -> detail::scan_result_for_range_t<decltype(stdin_range<CharT>())>

Equivalent to input, except writes what's in p to stdout.

int i{};
scn::prompt("What's your favorite number? ", "{}", i);
// Equivalent to:
// std::fputs("What's your favorite number? ", stdout);
// scn::input("{}", i);

Definition at line 276 of file scan.h.

template<typename Range , typename Format , typename... Args>
auto scn::scan ( Range &&  r,
const Format &  f,
Args &...  a 
) -> detail::scan_result_for_range_t<Range>

The most fundamental part of the scanning API.

Reads from the range in r according to the format string f.

Definition at line 73 of file scan.h.

template<typename Range , typename... Args>
auto scn::scan ( Range &&  r,
detail::default_t  ,
Args &...  a 
) -> detail::scan_result_for_range_t<Range>

Equivalent to scan, but with a format string with the appropriate amount of space-separated "{}"s for the number of arguments.

Because this function doesn't have to parse the format string, performance is improved.

See Also
scan

Definition at line 136 of file scan.h.

template<typename Locale , typename Range , typename Format , typename... Args>
auto scn::scan_localized ( const Locale &  loc,
Range &&  r,
const Format &  f,
Args &...  a 
) -> detail::scan_result_for_range_t<Range>

Read from the range in r using the locale in loc.

loc must be a std::locale.

Use of this function is discouraged, due to the overhead involved with locales. Note, that the other functions are completely locale-agnostic, and aren't affected by changes to the global C locale.

Definition at line 104 of file scan.h.

template<typename T , typename Range >
auto scn::scan_value ( Range &&  r) -> detail::scan_result_for_range_t<Range, expected<T>>

Scans a single value with the default options, returning it instead of using an output parameter.

The parsed value is in ret.value(), if ret == true.

auto ret = scn::scan_value<int>("42");
if (ret) {
// ret.value() == 42
}

Definition at line 169 of file scan.h.

template<typename Range , typename Format , typename... Args>
auto scn::scanf ( Range &&  r,
const Format &  f,
Args &...  a 
) -> detail::scan_result_for_range_t<Range>

Otherwise equivalent to scan, except it uses scanf-like format string syntax, instead of the Python-like default one.

Definition at line 213 of file scan.h.