1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef URL_URL_FILE_H_
6 #define URL_URL_FILE_H_
8 // Provides shared functions used by the internals of the parser and
9 // canonicalizer for file URLs. Do not use outside of these modules.
11 #include "base/strings/string_util.h"
12 #include "url/url_parse_internal.h"
16 // We allow both "c:" and "c|" as drive identifiers.
17 inline bool IsWindowsDriveSeparator(char16_t ch) {
18 return ch == ':' || ch == '|';
20 inline bool IsWindowsDriveSeparator(char ch) {
21 return IsWindowsDriveSeparator(static_cast<char16_t>(ch));
24 // Returns the index of the next slash in the input after the given index, or
25 // spec_len if the end of the input is reached.
26 template<typename CHAR>
27 inline int FindNextSlash(const CHAR* spec, int begin_index, int spec_len) {
28 int idx = begin_index;
29 while (idx < spec_len && !IsURLSlash(spec[idx]))
34 // DoesContainWindowsDriveSpecUntil returns the least number between
35 // start_offset and max_offset such that the spec has a valid drive
36 // specification starting at that offset. Otherwise it returns -1. This function
37 // gracefully handles, by returning -1, start_offset values that are equal to or
38 // larger than the spec_len, and caps max_offset appropriately to simplify
39 // callers. max_offset must be at least start_offset.
40 template <typename CHAR>
41 inline int DoesContainWindowsDriveSpecUntil(const CHAR* spec,
45 CHECK_LE(start_offset, max_offset);
46 if (start_offset > spec_len - 2)
47 return -1; // Not enough room.
48 if (max_offset > spec_len - 2)
49 max_offset = spec_len - 2;
50 for (int offset = start_offset; offset <= max_offset; ++offset) {
51 if (!base::IsAsciiAlpha(spec[offset]))
52 continue; // Doesn't contain a valid drive letter.
53 if (!IsWindowsDriveSeparator(spec[offset + 1]))
54 continue; // Isn't followed with a drive separator.
60 // Returns true if the start_offset in the given spec looks like it begins a
61 // drive spec, for example "c:". This function explicitly handles start_offset
62 // values that are equal to or larger than the spec_len to simplify callers.
64 // If this returns true, the spec is guaranteed to have a valid drive letter
65 // plus a drive letter separator (a colon or a pipe) starting at |start_offset|.
66 template <typename CHAR>
67 inline bool DoesBeginWindowsDriveSpec(const CHAR* spec,
70 return DoesContainWindowsDriveSpecUntil(spec, start_offset, start_offset,
71 spec_len) == start_offset;
76 // Returns true if the start_offset in the given text looks like it begins a
77 // UNC path, for example "\\". This function explicitly handles start_offset
78 // values that are equal to or larger than the spec_len to simplify callers.
80 // When strict_slashes is set, this function will only accept backslashes as is
81 // standard for Windows. Otherwise, it will accept forward slashes as well
82 // which we use for a lot of URL handling.
83 template<typename CHAR>
84 inline bool DoesBeginUNCPath(const CHAR* text,
87 bool strict_slashes) {
88 int remaining_len = len - start_offset;
89 if (remaining_len < 2)
93 return text[start_offset] == '\\' && text[start_offset + 1] == '\\';
94 return IsURLSlash(text[start_offset]) && IsURLSlash(text[start_offset + 1]);
101 #endif // URL_URL_FILE_H_