translating foreign data

Stefan Ram stefan.ram at 1
Sat Jun 23 19:33:24 EDT 2018


  To: Richard Damon
From: ram at zedat.fu-berlin.de (Stefan Ram)

Richard Damon <Richard at Damon-Family.org> writes:
>Now, if I have a parser that doesn't use the locale, but some other rule
>base than I just need to provide it with the right rules, which is
>basically just defining the right locale.

  Here's an example C++ program I wrote. It uses the class s
  to provide rules for an ad hoc locale which then is used to
  imbue a temporary string stream which then can parse numbers
  using the thousands separator given by s.

  main.cpp

#include <iostream>
#include <ostream>
#include <sstream>
#include <locale>
#include <string>

using namespace ::std::literals;

struct s : ::std::numpunct< char >
{ char do_thousands_sep() const override { return ','; }
  ::std::string do_grouping() const override { return "\3"; }};

static double double_value_of( ::std::string const & string ) {
::std::stringstream source { string };
  source.imbue( ::std::locale( source.getloc(), new s ));
  double number; source >> number; return number; }

int main()
{ ::std::cout << double_value_of( "4,800.1"s )<< '\n';
  ::std::cout << double_value_of( "3,334.5e9"s )<< '\n'; }

  transcript

4800.1
3.3345e+012

--- BBBS/Li6 v4.10 Toy-3
 * Origin: Prism bbs (1:261/38)



More information about the Python-list mailing list