blob: 796d12c1880832e22d489c3ed4fc8f3a3e67813a [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
zhongyi23960342016-04-12 23:13:2011#include "base/strings/string_util.h"
[email protected]318076b2013-04-18 21:19:4512#include "url/url_parse_internal.h"
[email protected]e7bba5f82013-04-10 20:10:5213
[email protected]0318f922014-04-22 00:09:2314namespace url {
[email protected]e7bba5f82013-04-10 20:10:5215
16#ifdef WIN32
17
18// We allow both "c:" and "c|" as drive identifiers.
[email protected]3774f832013-06-11 21:21:5719inline bool IsWindowsDriveSeparator(base::char16 ch) {
[email protected]e7bba5f82013-04-10 20:10:5220 return ch == ':' || ch == '|';
21}
[email protected]e7bba5f82013-04-10 20:10:5222
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.
27template<typename CHAR>
28inline 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|.
43template<typename CHAR>
44inline 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.
zhongyi23960342016-04-12 23:13:2049 if (!base::IsAsciiAlpha(spec[start_offset]))
[email protected]e7bba5f82013-04-10 20:10:5250 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.
63template<typename CHAR>
64inline 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]0318f922014-04-22 00:09:2379} // namespace url
[email protected]e7bba5f82013-04-10 20:10:5280
[email protected]318076b2013-04-18 21:19:4581#endif // URL_URL_FILE_H_