Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <charconv>
#include <locale>
#include <sstream>
#include <string_view>
#include <type_traits>
 
#include "core/common/common.h"
 
namespace onnxruntime {
 
namespace detail {
 
// Whether we will use std::from_chars() to parse to `T`.
#if defined(_LIBCPP_VERSION)
// Note: Currently (e.g., in LLVM 19), libc++'s std::from_chars() doesn't support floating point types yet.
template <typename T>
constexpr bool ParseWithFromChars = !std::is_same_v<bool, T> && std::is_integral_v<T>;
#else
template <typename T>
constexpr bool ParseWithFromChars = !std::is_same_v<bool, T> && (std::is_integral_v<T> || std::is_floating_point_v<T>);
#endif
 
}  // namespace detail
 
/**
 * Tries to parse a value from an entire string.
 * If successful, sets `value` and returns true. Otherwise, does not modify `value` and returns false.
 */
template <typename T>
std::enable_if_t<detail::ParseWithFromChars<T>, bool>
TryParseStringWithClassicLocale(std::string_view str, T& value) {
  T parsed_value{};
 
  std::from_chars_result conversion_result{};
  if constexpr (std::is_integral_v<T> && std::is_unsigned_v<T>) {
    // For unsigned integral types, also handle hex values, i.e., those beginning with "0x".
    // std::from_chars() does not accept the "0x" prefix.
    const bool has_hex_prefix = str.size() >= 2 &&
                                str[0] == '0' &&
                                (str[1] == 'x' || str[1] == 'X');
 
    if (has_hex_prefix) {
      str = str.substr(2);
    }
 
    const int base = has_hex_prefix ? 16 : 10;
    conversion_result = std::from_chars(str.data(), str.data() + str.size(), parsed_value, base);
  } else {
    conversion_result = std::from_chars(str.data(), str.data() + str.size(), parsed_value);
  }
 
  if (conversion_result.ec != std::errc{}) {
    return false;
  }
 
  if (conversion_result.ptr != str.data() + str.size()) {
    return false;
  }
 
  value = parsed_value;
  return true;
}
 
template <typename T>
std::enable_if_t<!detail::ParseWithFromChars<T>, bool>
TryParseStringWithClassicLocale(std::string_view str, T& value) {
  // don't allow leading whitespace
  if (!str.empty() && std::isspace(str[0], std::locale::classic())) {
    return false;
  }
 
  std::istringstream is{std::string{str}};
  is.imbue(std::locale::classic());
  T parsed_value{};
 
  const bool parse_successful =
      is >> parsed_value &&
      is.get() == std::istringstream::traits_type::eof();  // don't allow trailing characters
  if (!parse_successful) {
    return false;
  }
 
  value = std::move(parsed_value);
  return true;
}
 
inline bool TryParseStringWithClassicLocale(std::string_view str, std::string& value) {
  value = str;
  return true;
}
 
inline bool TryParseStringWithClassicLocale(std::string_view str, bool& value) {
  if (str == "0" || str == "False" || str == "false") {
    value = false;
    return true;
  }
 
  if (str == "1" || str == "True" || str == "true") {
    value = true;
    return true;
  }
 
  return false;
}
 
/**
 * Parses a value from an entire string.
 */
template <typename T>
Status ParseStringWithClassicLocale(std::string_view s, T& value) {
  ORT_RETURN_IF_NOT(TryParseStringWithClassicLocale(s, value), "Failed to parse value: \"", value, "\"");
  return Status::OK();
}
 
/**
 * Parses a value from an entire string.
 */
template <typename T>
T ParseStringWithClassicLocale(std::string_view s) {
  T value{};
  ORT_THROW_IF_ERROR(ParseStringWithClassicLocale(s, value));
  return value;
}
 
}  // namespace onnxruntime