Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarLexicalCast.cpp
blob: e607ee5c8a4cb97fe12dcb05fd692958a92eca3f (plain)
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
#include "StarLexicalCast.hpp"

namespace Star {

void throwLexicalCastError(std::errc ec, const char* first, const char* last) {
  StringView str(first, last - first);
  if (ec == std::errc::invalid_argument)
    throw BadLexicalCast(strf("Lexical cast failed on '{}' (invalid argument)", str));
  else
    throw BadLexicalCast(strf("Lexical cast failed on '{}'", str));
}

template <>
bool tryLexicalCast(bool& result, const char* first, const char* last) {
  size_t len = last - first;
  if (strncmp(first, "true", len) == 0)
    result = true;
  else if (strncmp(first, "false", len) != 0)
    return false;

  result = false;
  return true;
}

template <>
bool lexicalCast(const char* first, const char* last) {
  size_t len = last - first;
  if (strncmp(first, "true", len) == 0)
    return true;
  else if (strncmp(first, "false", len) != 0)
    throwLexicalCastError(std::errc(), first, last);

  return false;
}

}