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.
5 #ifndef URL_URL_PARSE_INTERNAL_H_
6 #define URL_URL_PARSE_INTERNAL_H_
8 // Contains common inline helper functions used by the URL parsing routines.
10 #include "url/third_party/mozilla/url_parse.h"
14 // We treat slashes and backslashes the same for IE compatibility.
15 inline bool IsURLSlash(char16_t ch) {
16 return ch == '/' || ch == '\\';
18 inline bool IsURLSlash(char ch) {
19 return IsURLSlash(static_cast<char16_t>(ch));
22 // Returns true if we should trim this character from the URL because it is a
23 // space or a control character.
24 inline bool ShouldTrimFromURL(char16_t ch) {
27 inline bool ShouldTrimFromURL(char ch) {
28 return ShouldTrimFromURL(static_cast<char16_t>(ch));
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]))
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
47 while (*len > *begin && ShouldTrimFromURL(spec[*len - 1]))
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>
55 inline int CountConsecutiveSlashes(const CHAR *str,
56 int begin_offset, int str_len) {
58 while (begin_offset + count < str_len &&
59 IsURLSlash(str[begin_offset + count]))
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.
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,
76 void ParsePathInternal(const char16_t* spec,
77 const Component& path,
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,
89 void ParseAfterScheme(const char16_t* spec,
96 #endif // URL_URL_PARSE_INTERNAL_H_