blob: 76308780c46e75fb5a7bf40fceb318012ab16320 [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_PARSE_INTERNAL_H_
6#define URL_URL_PARSE_INTERNAL_H_
7
[email protected]e7bba5f82013-04-10 20:10:528// Contains common inline helper functions used by the URL parsing routines.
9
tfarina018de6e2015-05-26 17:41:2010#include "url/third_party/mozilla/url_parse.h"
[email protected]e7bba5f82013-04-10 20:10:5211
[email protected]0318f922014-04-22 00:09:2312namespace url {
[email protected]e7bba5f82013-04-10 20:10:5213
qyearsley2bc727d2015-08-14 20:17:1514// We treat slashes and backslashes the same for IE compatibility.
[email protected]3774f832013-06-11 21:21:5715inline bool IsURLSlash(base::char16 ch) {
[email protected]e7bba5f82013-04-10 20:10:5216 return ch == '/' || ch == '\\';
17}
18
19// Returns true if we should trim this character from the URL because it is a
20// space or a control character.
[email protected]3774f832013-06-11 21:21:5721inline bool ShouldTrimFromURL(base::char16 ch) {
[email protected]e7bba5f82013-04-10 20:10:5222 return ch <= ' ';
23}
24
25// Given an already-initialized begin index and length, this shrinks the range
26// to eliminate "should-be-trimmed" characters. Note that the length does *not*
27// indicate the length of untrimmed data from |*begin|, but rather the position
28// in the input string (so the string starts at character |*begin| in the spec,
29// and goes until |*len|).
30template<typename CHAR>
[email protected]369e84f72013-11-23 01:53:5231inline void TrimURL(const CHAR* spec, int* begin, int* len,
32 bool trim_path_end = true) {
[email protected]e7bba5f82013-04-10 20:10:5233 // Strip leading whitespace and control characters.
34 while (*begin < *len && ShouldTrimFromURL(spec[*begin]))
35 (*begin)++;
36
[email protected]369e84f72013-11-23 01:53:5237 if (trim_path_end) {
38 // Strip trailing whitespace and control characters. We need the >i test
39 // for when the input string is all blanks; we don't want to back past the
40 // input.
41 while (*len > *begin && ShouldTrimFromURL(spec[*len - 1]))
42 (*len)--;
43 }
[email protected]e7bba5f82013-04-10 20:10:5244}
45
46// Counts the number of consecutive slashes starting at the given offset
47// in the given string of the given length.
48template<typename CHAR>
49inline int CountConsecutiveSlashes(const CHAR *str,
50 int begin_offset, int str_len) {
51 int count = 0;
52 while (begin_offset + count < str_len &&
53 IsURLSlash(str[begin_offset + count]))
54 ++count;
55 return count;
56}
57
58// Internal functions in url_parse.cc that parse the path, that is, everything
59// following the authority section. The input is the range of everything
60// following the authority section, and the output is the identified ranges.
61//
62// This is designed for the file URL parser or other consumers who may do
63// special stuff at the beginning, but want regular path parsing, it just
64// maps to the internal parsing function for paths.
65void ParsePathInternal(const char* spec,
66 const Component& path,
67 Component* filepath,
68 Component* query,
69 Component* ref);
[email protected]3774f832013-06-11 21:21:5770void ParsePathInternal(const base::char16* spec,
[email protected]e7bba5f82013-04-10 20:10:5271 const Component& path,
72 Component* filepath,
73 Component* query,
74 Component* ref);
75
76
77// Given a spec and a pointer to the character after the colon following the
78// scheme, this parses it and fills in the structure, Every item in the parsed
79// structure is filled EXCEPT for the scheme, which is untouched.
80void ParseAfterScheme(const char* spec,
81 int spec_len,
82 int after_scheme,
83 Parsed* parsed);
[email protected]3774f832013-06-11 21:21:5784void ParseAfterScheme(const base::char16* spec,
[email protected]e7bba5f82013-04-10 20:10:5285 int spec_len,
86 int after_scheme,
87 Parsed* parsed);
88
[email protected]0318f922014-04-22 00:09:2389} // namespace url
[email protected]e7bba5f82013-04-10 20:10:5290
[email protected]318076b2013-04-18 21:19:4591#endif // URL_URL_PARSE_INTERNAL_H_