[email protected] | 51bcc5d | 2013-04-24 01:41:37 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 4 | |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 5 | #ifndef URL_URL_FILE_H_ |
| 6 | #define URL_URL_FILE_H_ |
| 7 | |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 8 | // Provides shared functions used by the internals of the parser and |
| 9 | // canonicalizer for file URLs. Do not use outside of these modules. |
| 10 | |
zhongyi | 2396034 | 2016-04-12 23:13:20 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 12 | #include "url/url_parse_internal.h" |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 13 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 14 | namespace url { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 15 | |
| 16 | #ifdef WIN32 |
| 17 | |
| 18 | // We allow both "c:" and "c|" as drive identifiers. |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 19 | inline bool IsWindowsDriveSeparator(base::char16 ch) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 20 | return ch == ':' || ch == '|'; |
| 21 | } |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 22 | |
| 23 | #endif // WIN32 |
| 24 | |
| 25 | // Returns the index of the next slash in the input after the given index, or |
| 26 | // spec_len if the end of the input is reached. |
| 27 | template<typename CHAR> |
| 28 | inline int FindNextSlash(const CHAR* spec, int begin_index, int spec_len) { |
| 29 | int idx = begin_index; |
| 30 | while (idx < spec_len && !IsURLSlash(spec[idx])) |
| 31 | idx++; |
| 32 | return idx; |
| 33 | } |
| 34 | |
| 35 | #ifdef WIN32 |
| 36 | |
| 37 | // Returns true if the start_offset in the given spec looks like it begins a |
| 38 | // drive spec, for example "c:". This function explicitly handles start_offset |
| 39 | // values that are equal to or larger than the spec_len to simplify callers. |
| 40 | // |
| 41 | // If this returns true, the spec is guaranteed to have a valid drive letter |
| 42 | // plus a colon starting at |start_offset|. |
| 43 | template<typename CHAR> |
| 44 | inline bool DoesBeginWindowsDriveSpec(const CHAR* spec, int start_offset, |
| 45 | int spec_len) { |
| 46 | int remaining_len = spec_len - start_offset; |
| 47 | if (remaining_len < 2) |
| 48 | return false; // Not enough room. |
zhongyi | 2396034 | 2016-04-12 23:13:20 | [diff] [blame] | 49 | if (!base::IsAsciiAlpha(spec[start_offset])) |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 50 | return false; // Doesn't start with a valid drive letter. |
| 51 | if (!IsWindowsDriveSeparator(spec[start_offset + 1])) |
| 52 | return false; // Isn't followed with a drive separator. |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | // Returns true if the start_offset in the given text looks like it begins a |
| 57 | // UNC path, for example "\\". This function explicitly handles start_offset |
| 58 | // values that are equal to or larger than the spec_len to simplify callers. |
| 59 | // |
| 60 | // When strict_slashes is set, this function will only accept backslashes as is |
| 61 | // standard for Windows. Otherwise, it will accept forward slashes as well |
| 62 | // which we use for a lot of URL handling. |
| 63 | template<typename CHAR> |
| 64 | inline bool DoesBeginUNCPath(const CHAR* text, |
| 65 | int start_offset, |
| 66 | int len, |
| 67 | bool strict_slashes) { |
| 68 | int remaining_len = len - start_offset; |
| 69 | if (remaining_len < 2) |
| 70 | return false; |
| 71 | |
| 72 | if (strict_slashes) |
| 73 | return text[start_offset] == '\\' && text[start_offset + 1] == '\\'; |
| 74 | return IsURLSlash(text[start_offset]) && IsURLSlash(text[start_offset + 1]); |
| 75 | } |
| 76 | |
| 77 | #endif // WIN32 |
| 78 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 79 | } // namespace url |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 80 | |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 81 | #endif // URL_URL_FILE_H_ |