|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#pragma once |
| 21 | + |
| 22 | +#include <algorithm> |
| 23 | +#include <cctype> |
| 24 | +#include <string_view> |
| 25 | + |
| 26 | +/// \file iceberg/util/uri.h |
| 27 | +/// \brief URI scheme detection utilities per RFC 3986. |
| 28 | + |
| 29 | +namespace iceberg { |
| 30 | + |
| 31 | +/// \brief Check whether a string begins with a valid RFC 3986 URI scheme |
| 32 | +/// followed by ':'. |
| 33 | +/// |
| 34 | +/// A scheme (RFC 3986 §3.1) is defined as: |
| 35 | +/// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) |
| 36 | +/// |
| 37 | +/// A single character before ':' is treated as a Windows drive letter, not a |
| 38 | +/// scheme (e.g., "C:\path"). |
| 39 | +/// |
| 40 | +/// \param value The string to inspect. |
| 41 | +/// \return true if \p value starts with a valid URI scheme followed by ':'. |
| 42 | +inline bool IsUriScheme(std::string_view value) { |
| 43 | + auto colon_pos = value.find(':'); |
| 44 | + if (colon_pos == std::string_view::npos || colon_pos <= 1) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + if (!std::isalpha(static_cast<unsigned char>(value[0]))) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + return std::ranges::all_of(value.substr(1, colon_pos - 1), [](char c) { |
| 51 | + return std::isalpha(static_cast<unsigned char>(c)) || |
| 52 | + std::isdigit(static_cast<unsigned char>(c)) || c == '+' || c == '-' || |
| 53 | + c == '.'; |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +} // namespace iceberg |
0 commit comments