|  | // Copyright 2023 The Tint Authors. | 
|  | // | 
|  | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | // you may not use this file except in compliance with the License. | 
|  | // You may obtain a copy of the License at | 
|  | // | 
|  | //     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | // | 
|  | // Unless required by applicable law or agreed to in writing, software | 
|  | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | // See the License for the specific language governing permissions and | 
|  | // limitations under the License. | 
|  |  | 
|  | #ifndef SRC_TINT_UTILS_STRCONV_PARSE_NUM_H_ | 
|  | #define SRC_TINT_UTILS_STRCONV_PARSE_NUM_H_ | 
|  |  | 
|  | #include <optional> | 
|  | #include <string> | 
|  |  | 
|  | #include "src/tint/utils/macros/compiler.h" | 
|  | #include "src/tint/utils/result/result.h" | 
|  |  | 
|  | namespace tint { | 
|  |  | 
|  | /// Error returned by the number parsing functions | 
|  | enum class ParseNumberError { | 
|  | /// The number was unparsable | 
|  | kUnparsable, | 
|  | /// The parsed number is not representable by the target datatype | 
|  | kResultOutOfRange, | 
|  | }; | 
|  |  | 
|  | /// @param str the string | 
|  | /// @returns the string @p str parsed as a float | 
|  | Result<float, ParseNumberError> ParseFloat(std::string_view str); | 
|  |  | 
|  | /// @param str the string | 
|  | /// @returns the string @p str parsed as a double | 
|  | Result<double, ParseNumberError> ParseDouble(std::string_view str); | 
|  |  | 
|  | /// @param str the string | 
|  | /// @returns the string @p str parsed as a int | 
|  | Result<int, ParseNumberError> ParseInt(std::string_view str); | 
|  |  | 
|  | /// @param str the string | 
|  | /// @returns the string @p str parsed as a unsigned int | 
|  | Result<unsigned int, ParseNumberError> ParseUint(std::string_view str); | 
|  |  | 
|  | /// @param str the string | 
|  | /// @returns the string @p str parsed as a int64_t | 
|  | Result<int64_t, ParseNumberError> ParseInt64(std::string_view str); | 
|  |  | 
|  | /// @param str the string | 
|  | /// @returns the string @p str parsed as a uint64_t | 
|  | Result<uint64_t, ParseNumberError> ParseUint64(std::string_view str); | 
|  |  | 
|  | /// @param str the string | 
|  | /// @returns the string @p str parsed as a int32_t | 
|  | Result<int32_t, ParseNumberError> ParseInt32(std::string_view str); | 
|  |  | 
|  | /// @param str the string | 
|  | /// @returns the string @p str parsed as a uint32_t | 
|  | Result<uint32_t, ParseNumberError> ParseUint32(std::string_view str); | 
|  |  | 
|  | /// @param str the string | 
|  | /// @returns the string @p str parsed as a int16_t | 
|  | Result<int16_t, ParseNumberError> ParseInt16(std::string_view str); | 
|  |  | 
|  | /// @param str the string | 
|  | /// @returns the string @p str parsed as a uint16_t | 
|  | Result<uint16_t, ParseNumberError> ParseUint16(std::string_view str); | 
|  |  | 
|  | /// @param str the string | 
|  | /// @returns the string @p str parsed as a int8_t | 
|  | Result<int8_t, ParseNumberError> ParseInt8(std::string_view str); | 
|  |  | 
|  | /// @param str the string | 
|  | /// @returns the string @p str parsed as a uint8_t | 
|  | Result<uint8_t, ParseNumberError> ParseUint8(std::string_view str); | 
|  |  | 
|  | /// Disables the false-positive unreachable-code compiler warnings | 
|  | TINT_BEGIN_DISABLE_WARNING(UNREACHABLE_CODE); | 
|  |  | 
|  | /// @param str the string | 
|  | /// @returns the string @p str parsed as a the number @p T | 
|  | template <typename T> | 
|  | inline Result<T, ParseNumberError> ParseNumber(std::string_view str) { | 
|  | if constexpr (std::is_same_v<T, float>) { | 
|  | return ParseFloat(str); | 
|  | } | 
|  | if constexpr (std::is_same_v<T, double>) { | 
|  | return ParseDouble(str); | 
|  | } | 
|  | if constexpr (std::is_same_v<T, int>) { | 
|  | return ParseInt(str); | 
|  | } | 
|  | if constexpr (std::is_same_v<T, unsigned int>) { | 
|  | return ParseUint(str); | 
|  | } | 
|  | if constexpr (std::is_same_v<T, int64_t>) { | 
|  | return ParseInt64(str); | 
|  | } | 
|  | if constexpr (std::is_same_v<T, uint64_t>) { | 
|  | return ParseUint64(str); | 
|  | } | 
|  | if constexpr (std::is_same_v<T, int32_t>) { | 
|  | return ParseInt32(str); | 
|  | } | 
|  | if constexpr (std::is_same_v<T, uint32_t>) { | 
|  | return ParseUint32(str); | 
|  | } | 
|  | if constexpr (std::is_same_v<T, int16_t>) { | 
|  | return ParseInt16(str); | 
|  | } | 
|  | if constexpr (std::is_same_v<T, uint16_t>) { | 
|  | return ParseUint16(str); | 
|  | } | 
|  | if constexpr (std::is_same_v<T, int8_t>) { | 
|  | return ParseInt8(str); | 
|  | } | 
|  | if constexpr (std::is_same_v<T, uint8_t>) { | 
|  | return ParseUint8(str); | 
|  | } | 
|  | return ParseNumberError::kUnparsable; | 
|  | } | 
|  |  | 
|  | /// Re-enables the unreachable-code compiler warnings | 
|  | TINT_END_DISABLE_WARNING(UNREACHABLE_CODE); | 
|  |  | 
|  | }  // namespace tint | 
|  |  | 
|  | #endif  // SRC_TINT_UTILS_STRCONV_PARSE_NUM_H_ |