File: StrToInt.cpp

package info (click to toggle)
librepfunc 1.11.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 468 kB
  • sloc: cpp: 1,768; makefile: 270
file content (47 lines) | stat: -rw-r--r-- 1,571 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*******************************************************************************
 * librepfunc: A tiny lib of common functions and tasks in my c++ projects.
 * See the README file for copyright information and how to reach the author.
 ******************************************************************************/
#include <string>    // std::stoll
#include <iostream>  // std::cerr
#include <exception> // std::exception
#define __STDC_LIMIT_MACROS
#include <cmath>     // INTMAX_MAX
#include <repfunc.h>

template<class T>
std::intmax_t StrToIntT(const std::basic_string<T>& s, size_t* pos, size_t base, const char* f) {
  constexpr std::intmax_t err = INTMAX_MAX; /* greater or equal 2^63 - 1 */

  if (s.empty()) {
     std::cerr << f
               << ": ERROR: cannot convert empty string to integer."
               << std::endl;
     return err;
     }
  else if ((base == 1) or (base > 36)) {
     std::cerr << f
               << ": ERROR: invalid base: " + IntToStr(base)
               << std::endl;
     return err;
     }
  else {
     try {
        return std::stoll(s, pos, base);
        }
     catch(const std::exception& e) {
        std::cerr << f
               << ": ERROR: cannot convert to integer: "
               << e.what() << std::endl;
        return err;
        }
     }
}

std::intmax_t StrToInt(const std::string& s, size_t* pos, size_t base) {
  return StrToIntT(s, pos, base, __PRETTY_FUNCTION__);
}

std::intmax_t WStrToInt(const std::wstring& s, size_t* pos, size_t base) {
  return StrToIntT(s, pos, base, __PRETTY_FUNCTION__);
}