OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef URL_URL_FILE_H_ | 5 #ifndef URL_URL_FILE_H_ |
6 #define URL_URL_FILE_H_ | 6 #define URL_URL_FILE_H_ |
7 | 7 |
8 // Provides shared functions used by the internals of the parser and | 8 // Provides shared functions used by the internals of the parser and |
9 // canonicalizer for file URLs. Do not use outside of these modules. | 9 // canonicalizer for file URLs. Do not use outside of these modules. |
10 | 10 |
| 11 #include "base/strings/string_util.h" |
11 #include "url/url_parse_internal.h" | 12 #include "url/url_parse_internal.h" |
12 | 13 |
13 namespace url { | 14 namespace url { |
14 | 15 |
15 #ifdef WIN32 | 16 #ifdef WIN32 |
16 | 17 |
17 // We allow both "c:" and "c|" as drive identifiers. | 18 // We allow both "c:" and "c|" as drive identifiers. |
18 inline bool IsWindowsDriveSeparator(base::char16 ch) { | 19 inline bool IsWindowsDriveSeparator(base::char16 ch) { |
19 return ch == ':' || ch == '|'; | 20 return ch == ':' || ch == '|'; |
20 } | 21 } |
21 inline bool IsWindowsDriveLetter(base::char16 ch) { | |
22 return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'); | |
23 } | |
24 | 22 |
25 #endif // WIN32 | 23 #endif // WIN32 |
26 | 24 |
27 // Returns the index of the next slash in the input after the given index, or | 25 // Returns the index of the next slash in the input after the given index, or |
28 // spec_len if the end of the input is reached. | 26 // spec_len if the end of the input is reached. |
29 template<typename CHAR> | 27 template<typename CHAR> |
30 inline int FindNextSlash(const CHAR* spec, int begin_index, int spec_len) { | 28 inline int FindNextSlash(const CHAR* spec, int begin_index, int spec_len) { |
31 int idx = begin_index; | 29 int idx = begin_index; |
32 while (idx < spec_len && !IsURLSlash(spec[idx])) | 30 while (idx < spec_len && !IsURLSlash(spec[idx])) |
33 idx++; | 31 idx++; |
34 return idx; | 32 return idx; |
35 } | 33 } |
36 | 34 |
37 #ifdef WIN32 | 35 #ifdef WIN32 |
38 | 36 |
39 // Returns true if the start_offset in the given spec looks like it begins a | 37 // Returns true if the start_offset in the given spec looks like it begins a |
40 // drive spec, for example "c:". This function explicitly handles start_offset | 38 // drive spec, for example "c:". This function explicitly handles start_offset |
41 // values that are equal to or larger than the spec_len to simplify callers. | 39 // values that are equal to or larger than the spec_len to simplify callers. |
42 // | 40 // |
43 // If this returns true, the spec is guaranteed to have a valid drive letter | 41 // If this returns true, the spec is guaranteed to have a valid drive letter |
44 // plus a colon starting at |start_offset|. | 42 // plus a colon starting at |start_offset|. |
45 template<typename CHAR> | 43 template<typename CHAR> |
46 inline bool DoesBeginWindowsDriveSpec(const CHAR* spec, int start_offset, | 44 inline bool DoesBeginWindowsDriveSpec(const CHAR* spec, int start_offset, |
47 int spec_len) { | 45 int spec_len) { |
48 int remaining_len = spec_len - start_offset; | 46 int remaining_len = spec_len - start_offset; |
49 if (remaining_len < 2) | 47 if (remaining_len < 2) |
50 return false; // Not enough room. | 48 return false; // Not enough room. |
51 if (!IsWindowsDriveLetter(spec[start_offset])) | 49 if (!base::IsAsciiAlpha(spec[start_offset])) |
52 return false; // Doesn't start with a valid drive letter. | 50 return false; // Doesn't start with a valid drive letter. |
53 if (!IsWindowsDriveSeparator(spec[start_offset + 1])) | 51 if (!IsWindowsDriveSeparator(spec[start_offset + 1])) |
54 return false; // Isn't followed with a drive separator. | 52 return false; // Isn't followed with a drive separator. |
55 return true; | 53 return true; |
56 } | 54 } |
57 | 55 |
58 // Returns true if the start_offset in the given text looks like it begins a | 56 // Returns true if the start_offset in the given text looks like it begins a |
59 // UNC path, for example "\\". This function explicitly handles start_offset | 57 // UNC path, for example "\\". This function explicitly handles start_offset |
60 // values that are equal to or larger than the spec_len to simplify callers. | 58 // values that are equal to or larger than the spec_len to simplify callers. |
61 // | 59 // |
(...skipping 12 matching lines...) Expand all Loading... |
74 if (strict_slashes) | 72 if (strict_slashes) |
75 return text[start_offset] == '\\' && text[start_offset + 1] == '\\'; | 73 return text[start_offset] == '\\' && text[start_offset + 1] == '\\'; |
76 return IsURLSlash(text[start_offset]) && IsURLSlash(text[start_offset + 1]); | 74 return IsURLSlash(text[start_offset]) && IsURLSlash(text[start_offset + 1]); |
77 } | 75 } |
78 | 76 |
79 #endif // WIN32 | 77 #endif // WIN32 |
80 | 78 |
81 } // namespace url | 79 } // namespace url |
82 | 80 |
83 #endif // URL_URL_FILE_H_ | 81 #endif // URL_URL_FILE_H_ |
OLD | NEW |