• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors
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_PARSE_INTERNAL_H_
6 #define URL_URL_PARSE_INTERNAL_H_
7 
8 // Contains common inline helper functions used by the URL parsing routines.
9 
10 #include "url/third_party/mozilla/url_parse.h"
11 
12 namespace url {
13 
14 // We treat slashes and backslashes the same for IE compatibility.
IsURLSlash(char16_t ch)15 inline bool IsURLSlash(char16_t ch) {
16   return ch == '/' || ch == '\\';
17 }
IsURLSlash(char ch)18 inline bool IsURLSlash(char ch) {
19   return IsURLSlash(static_cast<char16_t>(ch));
20 }
21 
22 // Returns true if we should trim this character from the URL because it is a
23 // space or a control character.
ShouldTrimFromURL(char16_t ch)24 inline bool ShouldTrimFromURL(char16_t ch) {
25   return ch <= ' ';
26 }
ShouldTrimFromURL(char ch)27 inline bool ShouldTrimFromURL(char ch) {
28   return ShouldTrimFromURL(static_cast<char16_t>(ch));
29 }
30 
31 // Given an already-initialized begin index and length, this shrinks the range
32 // to eliminate "should-be-trimmed" characters. Note that the length does *not*
33 // indicate the length of untrimmed data from |*begin|, but rather the position
34 // in the input string (so the string starts at character |*begin| in the spec,
35 // and goes until |*len|).
36 template<typename CHAR>
37 inline void TrimURL(const CHAR* spec, int* begin, int* len,
38                     bool trim_path_end = true) {
39   // Strip leading whitespace and control characters.
40   while (*begin < *len && ShouldTrimFromURL(spec[*begin]))
41     (*begin)++;
42 
43   if (trim_path_end) {
44     // Strip trailing whitespace and control characters. We need the >i test
45     // for when the input string is all blanks; we don't want to back past the
46     // input.
47     while (*len > *begin && ShouldTrimFromURL(spec[*len - 1]))
48       (*len)--;
49   }
50 }
51 
52 // Counts the number of consecutive slashes starting at the given offset
53 // in the given string of the given length.
54 template<typename CHAR>
CountConsecutiveSlashes(const CHAR * str,int begin_offset,int str_len)55 inline int CountConsecutiveSlashes(const CHAR *str,
56                                    int begin_offset, int str_len) {
57   int count = 0;
58   while (begin_offset + count < str_len &&
59          IsURLSlash(str[begin_offset + count]))
60     ++count;
61   return count;
62 }
63 
64 // Internal functions in url_parse.cc that parse the path, that is, everything
65 // following the authority section. The input is the range of everything
66 // following the authority section, and the output is the identified ranges.
67 //
68 // This is designed for the file URL parser or other consumers who may do
69 // special stuff at the beginning, but want regular path parsing, it just
70 // maps to the internal parsing function for paths.
71 void ParsePathInternal(const char* spec,
72                        const Component& path,
73                        Component* filepath,
74                        Component* query,
75                        Component* ref);
76 void ParsePathInternal(const char16_t* spec,
77                        const Component& path,
78                        Component* filepath,
79                        Component* query,
80                        Component* ref);
81 
82 // Given a spec and a pointer to the character after the colon following the
83 // scheme, this parses it and fills in the structure, Every item in the parsed
84 // structure is filled EXCEPT for the scheme, which is untouched.
85 void ParseAfterScheme(const char* spec,
86                       int spec_len,
87                       int after_scheme,
88                       Parsed* parsed);
89 void ParseAfterScheme(const char16_t* spec,
90                       int spec_len,
91                       int after_scheme,
92                       Parsed* parsed);
93 
94 }  // namespace url
95 
96 #endif  // URL_URL_PARSE_INTERNAL_H_
97