|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | + |
| 42 | +#ifndef CHARCONV_SHIM_H_ |
| 43 | +#define CHARCONV_SHIM_H_ |
| 44 | + |
| 45 | +// Poor man's <charconv> substitute for unsigned integer types only. |
| 46 | +// Allows Ada to be compiled on pre-C++17 macOS <10.15 (GR-48310). |
| 47 | + |
| 48 | +#warning "<charconv> not available, using alternative implementation" |
| 49 | + |
| 50 | +#include <type_traits> |
| 51 | +#include <limits> |
| 52 | +#include <system_error> // std::errc |
| 53 | +#include <algorithm> // std::min |
| 54 | + |
| 55 | +namespace charconv_shim { |
| 56 | + struct from_chars_result { |
| 57 | + const char* ptr; |
| 58 | + std::errc ec; |
| 59 | + }; |
| 60 | + struct to_chars_result { |
| 61 | + char* ptr; |
| 62 | + std::errc ec; |
| 63 | + }; |
| 64 | + |
| 65 | + constexpr char to_lower(char x) { |
| 66 | + return (x | 0x20); |
| 67 | + } |
| 68 | + |
| 69 | + template <typename UINT_T, UINT_T max = std::numeric_limits<UINT_T>::max(), |
| 70 | + typename std::enable_if<std::is_integral<UINT_T>::value, int>::type = 0, |
| 71 | + typename std::enable_if<!std::is_signed<UINT_T>::value, int>::type = 0> |
| 72 | + inline from_chars_result from_chars(const char* first, const char* last, UINT_T& value, int base = 10) { |
| 73 | + if (base < 2 || base > 36) { |
| 74 | + return {first, std::errc::invalid_argument}; |
| 75 | + } |
| 76 | + const UINT_T mul_limit = max / base; |
| 77 | + UINT_T result = 0; |
| 78 | + auto cursor = first; |
| 79 | + auto ec = std::errc{}; |
| 80 | + while (cursor < last) { |
| 81 | + char c = *cursor; |
| 82 | + char l = to_lower(c); |
| 83 | + char d; |
| 84 | + if (c >= '0' && c < '0' + std::min(base, 10)) { |
| 85 | + d = c - '0'; |
| 86 | + } else if (base > 10 && l >= 'a' && l < 'a' + (base - 10)) { |
| 87 | + d = l - 'a'; |
| 88 | + } else { |
| 89 | + break; |
| 90 | + } |
| 91 | + if (result > mul_limit) { |
| 92 | + // multiplication would result in integer overflow |
| 93 | + ec = std::errc::result_out_of_range; |
| 94 | + // advance until last valid digit |
| 95 | + } |
| 96 | + result *= base; |
| 97 | + if (result > max - d) { |
| 98 | + // addition would result in integer overflow |
| 99 | + ec = std::errc::result_out_of_range; |
| 100 | + // advance until last valid digit |
| 101 | + } |
| 102 | + result += d; |
| 103 | + ++cursor; |
| 104 | + } |
| 105 | + if (cursor == first) { |
| 106 | + return {first, std::errc::invalid_argument}; |
| 107 | + } else if (ec == std::errc{}) { |
| 108 | + value = result; |
| 109 | + } |
| 110 | + return {cursor, ec}; |
| 111 | + } |
| 112 | + |
| 113 | + template <typename UINT_T, |
| 114 | + typename std::enable_if<std::is_integral<UINT_T>::value, int>::type = 0, |
| 115 | + typename std::enable_if<!std::is_signed<UINT_T>::value, int>::type = 0> |
| 116 | + inline to_chars_result to_chars(char* first, char* last, UINT_T value, int base = 10) { |
| 117 | + if (base < 2 || base > 36) { |
| 118 | + return {first, std::errc::invalid_argument}; |
| 119 | + } |
| 120 | + int length = 1; |
| 121 | + for (auto v = value; v >= base; v /= base) { |
| 122 | + ++length; |
| 123 | + } |
| 124 | + if (last - first < length) { |
| 125 | + return {last, std::errc::value_too_large}; |
| 126 | + } |
| 127 | + |
| 128 | + const char *const digits = "0123456789abcdefghijklmnopqrstuvwxyz"; |
| 129 | + last = first + length; |
| 130 | + char* cursor = last; |
| 131 | + if (value == 0) { |
| 132 | + *--cursor = '0'; |
| 133 | + } else { |
| 134 | + for (auto v = value; v > 0; v /= base) { |
| 135 | + *--cursor = digits[v % base]; |
| 136 | + } |
| 137 | + } |
| 138 | + return {last, {}}; |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +namespace std { |
| 143 | + using ::charconv_shim::from_chars_result; |
| 144 | + using ::charconv_shim::to_chars_result; |
| 145 | + using ::charconv_shim::from_chars; |
| 146 | + using ::charconv_shim::to_chars; |
| 147 | +} |
| 148 | + |
| 149 | +#endif |
0 commit comments