| OLD | NEW |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 #define CONSTEXPR_STATIC_ASSERT(condition, message) \ | 33 #define CONSTEXPR_STATIC_ASSERT(condition, message) \ |
| 34 static_assert(condition, message) | 34 static_assert(condition, message) |
| 35 #else | 35 #else |
| 36 #define CONSTEXPR_STATIC_ASSERT(condition, message) CHECK(condition) << message | 36 #define CONSTEXPR_STATIC_ASSERT(condition, message) CHECK(condition) << message |
| 37 #endif | 37 #endif |
| 38 | 38 |
| 39 namespace { | 39 namespace { |
| 40 | 40 |
| 41 template <typename TIntType, typename TLongType> | 41 template <typename TIntType, typename TLongType> |
| 42 struct StringToIntegerTraits { | 42 struct StringToIntegerTraits { |
| 43 typedef TIntType IntType; | 43 using IntType = TIntType; |
| 44 typedef TLongType LongType; | 44 using LongType = TLongType; |
| 45 static void TypeCheck() { | 45 static void TypeCheck() { |
| 46 static_assert(std::numeric_limits<TIntType>::is_integer && | 46 static_assert(std::numeric_limits<TIntType>::is_integer && |
| 47 std::numeric_limits<TLongType>::is_integer, | 47 std::numeric_limits<TLongType>::is_integer, |
| 48 "IntType and LongType must be integer"); | 48 "IntType and LongType must be integer"); |
| 49 static_assert(std::numeric_limits<TIntType>::is_signed == | 49 static_assert(std::numeric_limits<TIntType>::is_signed == |
| 50 std::numeric_limits<TLongType>::is_signed, | 50 std::numeric_limits<TLongType>::is_signed, |
| 51 "IntType and LongType signedness must agree"); | 51 "IntType and LongType signedness must agree"); |
| 52 CONSTEXPR_STATIC_ASSERT(std::numeric_limits<TIntType>::min() >= | 52 CONSTEXPR_STATIC_ASSERT(std::numeric_limits<TIntType>::min() >= |
| 53 std::numeric_limits<TLongType>::min() && | 53 std::numeric_limits<TLongType>::min() && |
| 54 std::numeric_limits<TIntType>::min() < | 54 std::numeric_limits<TIntType>::min() < |
| (...skipping 13 matching lines...) Expand all Loading... |
| 68 static void TypeCheck() { | 68 static void TypeCheck() { |
| 69 static_assert(std::numeric_limits<TIntType>::is_signed, | 69 static_assert(std::numeric_limits<TIntType>::is_signed, |
| 70 "StringToSignedTraits IntType must be signed"); | 70 "StringToSignedTraits IntType must be signed"); |
| 71 return super::TypeCheck(); | 71 return super::TypeCheck(); |
| 72 } | 72 } |
| 73 static bool IsNegativeOverflow(TLongType value) { | 73 static bool IsNegativeOverflow(TLongType value) { |
| 74 return value < std::numeric_limits<TIntType>::min(); | 74 return value < std::numeric_limits<TIntType>::min(); |
| 75 } | 75 } |
| 76 | 76 |
| 77 private: | 77 private: |
| 78 typedef StringToIntegerTraits<TIntType, TLongType> super; | 78 using super = StringToIntegerTraits<TIntType, TLongType>; |
| 79 }; | 79 }; |
| 80 | 80 |
| 81 template <typename TIntType, typename TLongType> | 81 template <typename TIntType, typename TLongType> |
| 82 struct StringToUnsignedIntegerTraits | 82 struct StringToUnsignedIntegerTraits |
| 83 : public StringToIntegerTraits<TIntType, TLongType> { | 83 : public StringToIntegerTraits<TIntType, TLongType> { |
| 84 static void TypeCheck() { | 84 static void TypeCheck() { |
| 85 static_assert(!std::numeric_limits<TIntType>::is_signed, | 85 static_assert(!std::numeric_limits<TIntType>::is_signed, |
| 86 "StringToUnsignedTraits IntType must be unsigned"); | 86 "StringToUnsignedTraits IntType must be unsigned"); |
| 87 return super::TypeCheck(); | 87 return super::TypeCheck(); |
| 88 } | 88 } |
| 89 static bool IsNegativeOverflow(TLongType value) { return false; } | 89 static bool IsNegativeOverflow(TLongType value) { return false; } |
| 90 | 90 |
| 91 private: | 91 private: |
| 92 typedef StringToIntegerTraits<TIntType, TLongType> super; | 92 using super = StringToIntegerTraits<TIntType, TLongType>; |
| 93 }; | 93 }; |
| 94 | 94 |
| 95 struct StringToIntTraits : public StringToSignedIntegerTraits<int, long> { | 95 struct StringToIntTraits : public StringToSignedIntegerTraits<int, long> { |
| 96 static LongType Convert(const char* str, char** end, int base) { | 96 static LongType Convert(const char* str, char** end, int base) { |
| 97 return strtol(str, end, base); | 97 return strtol(str, end, base); |
| 98 } | 98 } |
| 99 }; | 99 }; |
| 100 | 100 |
| 101 struct StringToUnsignedIntTraits | 101 struct StringToUnsignedIntTraits |
| 102 : public StringToUnsignedIntegerTraits<unsigned int, unsigned long> { | 102 : public StringToUnsignedIntegerTraits<unsigned int, unsigned long> { |
| 103 static LongType Convert(const char* str, char** end, int base) { | 103 static LongType Convert(const char* str, char** end, int base) { |
| 104 if (str[0] == '-') { | 104 if (str[0] == '-') { |
| 105 return 0; | 105 return 0; |
| 106 } | 106 } |
| 107 return strtoul(str, end, base); | 107 return strtoul(str, end, base); |
| 108 } | 108 } |
| 109 }; | 109 }; |
| 110 | 110 |
| 111 template <typename Traits> | 111 template <typename Traits> |
| 112 bool StringToIntegerInternal(const base::StringPiece& string, | 112 bool StringToIntegerInternal(const base::StringPiece& string, |
| 113 typename Traits::IntType* number) { | 113 typename Traits::IntType* number) { |
| 114 typedef typename Traits::IntType IntType; | 114 using IntType = typename Traits::IntType; |
| 115 typedef typename Traits::LongType LongType; | 115 using LongType = typename Traits::LongType; |
| 116 | 116 |
| 117 Traits::TypeCheck(); | 117 Traits::TypeCheck(); |
| 118 | 118 |
| 119 if (string.empty() || isspace(string[0])) { | 119 if (string.empty() || isspace(string[0])) { |
| 120 return false; | 120 return false; |
| 121 } | 121 } |
| 122 | 122 |
| 123 if (string[string.length()] != '\0') { | 123 if (string[string.length()] != '\0') { |
| 124 // The implementations use the C standard library’s conversion routines, | 124 // The implementations use the C standard library’s conversion routines, |
| 125 // which rely on the strings having a trailing NUL character. std::string | 125 // which rely on the strings having a trailing NUL character. std::string |
| (...skipping 21 matching lines...) Expand all Loading... |
| 147 | 147 |
| 148 bool StringToNumber(const base::StringPiece& string, int* number) { | 148 bool StringToNumber(const base::StringPiece& string, int* number) { |
| 149 return StringToIntegerInternal<StringToIntTraits>(string, number); | 149 return StringToIntegerInternal<StringToIntTraits>(string, number); |
| 150 } | 150 } |
| 151 | 151 |
| 152 bool StringToNumber(const base::StringPiece& string, unsigned int* number) { | 152 bool StringToNumber(const base::StringPiece& string, unsigned int* number) { |
| 153 return StringToIntegerInternal<StringToUnsignedIntTraits>(string, number); | 153 return StringToIntegerInternal<StringToUnsignedIntTraits>(string, number); |
| 154 } | 154 } |
| 155 | 155 |
| 156 } // namespace crashpad | 156 } // namespace crashpad |
| OLD | NEW |