tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Reuse strtonum(3) and reallocarray(3) from OpenBSD



"Kamil Rytarowski" <n54%gmx.com@localhost> wrote:
 |Hello,
 |
 |I've prepared a small patch adding strtonum(3)

This is a ridiculous interface.
You just cannot object to what Jörg Sonnenberger said and at the
same time propose a function that requires multiple steps in order
to decide wether the return value is a valid one or indicates an
error condition instead.
If you want at all costs a good number converting facility, use
e.g. that:

  static void a_DoDec(CString const &_expr) {
          char const      *err;
          auto IntDecoder id;
  
          (void)id.setup(_expr.data(), _expr.length()).call();
          switch(id.error()) {
          case IntDecoder::err_none:      err = "none"; break;
          case IntDecoder::err_ill:       err = "no or bad input"; break;
          case IntDecoder::err_base:      err = "illegal char"; break;
          case IntDecoder::err_overflow:  err = "overflow"; break;
          }
  
          (void)IO::Std::sout->writeFormat(
                  "Expression: %S.%R"
                  "    Result: %qu (%qd).%R"
                  "      Base: %u.%R"
                  "    Signed: %#B.%R"
                  "     Error: %s.%R",
                  &_expr,
                  id.result(), s(si64,id.result()),
                  id.base(),
                  id.isNegative(),
                  err);
          return;
  }

  pub IntDecoder(void)
  pub ~IntDecoder(void)
  pub boolean isSetup(void) const
  pub IntDecoder &setup(char const *_buf, ui32 _blen, ui8 _base=0,
    boolean _sign=tru1);
  pub IntDecoder &setLocale(Locale::Handle _lc)
  pub boolean isCalled(void) const
  pub IntDecoder &call(void);
  pub IntDecoder &operator()(void)
  pub CallError error(void) const
  pub Locale::Handle Locale(void) const
  pub ui32 base(void) const
  pub ui64 result(void) const
  pub const char *buffer(void) const
  pub const char *start(void) const
  pub ui32 count(void) const
  pub ui32 remainingLength(void) const
  pub boolean isNegative(void) const

Upon interest i'd be willing and able to provide a C port of that,
only problems would be (lc is Locale::Handle)

  const ui8       *atoicnv;// lc->aToIChrNo()
                                ...-> const ui8 *charno; // atoi [256]

  while(CType::isSpace(*buf.c, lc)) {

  if(*buf.c && !CType::isSpace(*buf.c, lc))

P.S.: granted this is much to primitive to deal with languages
with a number system that doesn't use decimal notation.  Which are
quite a lot, btw.

--steffen


Home | Main Index | Thread Index | Old Index