Upload upstream chromium 67.0.3396
[platform/framework/web/chromium-efl.git] / url / url_file.h
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.
4
5 #ifndef URL_URL_FILE_H_
6 #define URL_URL_FILE_H_
7
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
11 #include "base/strings/string_util.h"
12 #include "url/url_parse_internal.h"
13
14 namespace url {
15
16 #ifdef WIN32
17
18 // We allow both "c:" and "c|" as drive identifiers.
19 inline bool IsWindowsDriveSeparator(base::char16 ch) {
20   return ch == ':' || ch == '|';
21 }
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.
49   if (!base::IsAsciiAlpha(spec[start_offset]))
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
79 }  // namespace url
80
81 #endif  // URL_URL_FILE_H_