| // Copyright 2026 Google LLC | |
| // | |
| // 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 | |
| // | |
| // https://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. | |
| // | |
| #include "formats/pdf/number_validator.h" | |
| #include <cstddef> | |
| #include "absl/strings/string_view.h" | |
| namespace credentio { | |
| bool IsValidRealNumber(absl::string_view s) { | |
| if (s.empty()) return false; | |
| bool seen_decimal = false; | |
| bool seen_e = false; | |
| for (size_t i = 0; i < s.size(); ++i) { | |
| const char c = s[i]; | |
| if (c == '+' || c == '-') { | |
| if (i != 0 && s[i - 1] != 'e' && s[i - 1] != 'E') return false; | |
| if (i == s.size() - 1) return false; | |
| } else if (c == '.') { | |
| if (seen_decimal || seen_e) return false; | |
| seen_decimal = true; | |
| } else if (c == 'e' || c == 'E') { | |
| if (seen_e || i == 0 || i == s.size() - 1) return false; | |
| seen_e = true; | |
| } else if (c < '0' || c > '9') { | |
| return false; | |
| } | |
| } | |
| bool has_digit = false; | |
| for (const char c : s) { | |
| if (c >= '0' && c <= '9') { | |
| has_digit = true; | |
| break; | |
| } | |
| } | |
| return has_digit; | |
| } | |
| bool IsValidHexadecimalString(absl::string_view s) { | |
| for (const char c : s) { | |
| if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || | |
| (c >= 'A' && c <= 'F') || c == ' ' || c == '\t' || c == '\r' || | |
| c == '\n' || c == '\f' || c == '\0')) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| } // namespace credentio |