blob: 540cb2543eb8cf9b37f95b6cfb8b0034f107a218 [file] [log] [blame]
[email protected]51bcc5d2013-04-24 01:41:371// 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]e7bba5f82013-04-10 20:10:524
[email protected]318076b2013-04-18 21:19:455#ifndef URL_URL_FILE_H_
6#define URL_URL_FILE_H_
7
[email protected]e7bba5f82013-04-10 20:10:528// Provides shared functions used by the internals of the parser and
9// canonicalizer for file URLs. Do not use outside of these modules.
10
[email protected]318076b2013-04-18 21:19:4511#include "url/url_parse_internal.h"
[email protected]e7bba5f82013-04-10 20:10:5212
[email protected]0318f922014-04-22 00:09:2313namespace url {
[email protected]e7bba5f82013-04-10 20:10:5214
15#ifdef WIN32
16
17// We allow both "c:" and "c|" as drive identifiers.
[email protected]3774f832013-06-11 21:21:5718inline bool IsWindowsDriveSeparator(base::char16 ch) {
[email protected]e7bba5f82013-04-10 20:10:5219 return ch == ':' || ch == '|';
20}
[email protected]3774f832013-06-11 21:21:5721inline bool IsWindowsDriveLetter(base::char16 ch) {
[email protected]e7bba5f82013-04-10 20:10:5222 return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
23}
24
25#endif // WIN32
26
27// 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.
29template<typename CHAR>
30inline int FindNextSlash(const CHAR* spec, int begin_index, int spec_len) {
31 int idx = begin_index;
32 while (idx < spec_len && !IsURLSlash(spec[idx]))
33 idx++;
34 return idx;
35}
36
37#ifdef WIN32
38
39// 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
41// values that are equal to or larger than the spec_len to simplify callers.
42//
43// If this returns true, the spec is guaranteed to have a valid drive letter
44// plus a colon starting at |start_offset|.
45template<typename CHAR>
46inline bool DoesBeginWindowsDriveSpec(const CHAR* spec, int start_offset,
47 int spec_len) {
48 int remaining_len = spec_len - start_offset;
49 if (remaining_len < 2)
50 return false; // Not enough room.
51 if (!IsWindowsDriveLetter(spec[start_offset]))
52 return false; // Doesn't start with a valid drive letter.
53 if (!IsWindowsDriveSeparator(spec[start_offset + 1]))
54 return false; // Isn't followed with a drive separator.
55 return true;
56}
57
58// 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
60// values that are equal to or larger than the spec_len to simplify callers.
61//
62// When strict_slashes is set, this function will only accept backslashes as is
63// standard for Windows. Otherwise, it will accept forward slashes as well
64// which we use for a lot of URL handling.
65template<typename CHAR>
66inline bool DoesBeginUNCPath(const CHAR* text,
67 int start_offset,
68 int len,
69 bool strict_slashes) {
70 int remaining_len = len - start_offset;
71 if (remaining_len < 2)
72 return false;
73
74 if (strict_slashes)
75 return text[start_offset] == '\\' && text[start_offset + 1] == '\\';
76 return IsURLSlash(text[start_offset]) && IsURLSlash(text[start_offset + 1]);
77}
78
79#endif // WIN32
80
[email protected]0318f922014-04-22 00:09:2381} // namespace url
[email protected]e7bba5f82013-04-10 20:10:5282
[email protected]318076b2013-04-18 21:19:4583#endif // URL_URL_FILE_H_